Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: faker.finance.pin() #695

Merged
merged 13 commits into from
Apr 6, 2022
17 changes: 17 additions & 0 deletions src/finance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,23 @@ export class Finance {
return cvv;
}

/**
* Generates a random PIN number.
*
* @param length The length of the PIN to generate. Defaults to `4`.
* @throws Will throw an error if length is less than 1.
import-brain marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* faker.finance.pin() // '5067'
import-brain marked this conversation as resolved.
Show resolved Hide resolved
* faker.finance.pin(6) // '213789'
*/
pin(length: number = 4): string {
if (length < 1) {
throw new FakerError('minimum length is 1');
}
return Array.from({ length }, () => this.faker.datatype.number(9)).join('');
}

/**
* Generates a random ethereum Address.
*
Expand Down
32 changes: 32 additions & 0 deletions test/finance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const seedRuns = [
litecoinAddress: '3XbJMAAara64sSkA9HD24YHQWd1b',
creditCardNumber: '3581-7755-1410-0486',
creditCardCVV: '379',
pin: '3791',
ethereumAddress: '0x8be4abdd39321ad7d3fe01ffce404f4d6db0906b',
iban: 'GT30Y75110867098F1E3542612J4',
bic: 'UYEOSCP1514',
Expand All @@ -44,6 +45,7 @@ const seedRuns = [
litecoinAddress: 'Madhxs2jewAgkYgJi7No6Cn8JZar',
creditCardNumber: '6011-6212-2540-3255-2392',
creditCardCVV: '251',
pin: '2512',
ethereumAddress: '0x5c346ba075bd57f5a62b82d72af39cbbb07a98cb',
iban: 'FO7710540350900318',
bic: 'OEFELYL1032',
Expand All @@ -67,6 +69,7 @@ const seedRuns = [
litecoinAddress: 'MTMe8Z3EaFdLqmaGKP1LEEJQVriSZRZds',
creditCardNumber: '4872190616276',
creditCardCVV: '948',
pin: '9487',
ethereumAddress: '0xeadb42f0e3f4a973fab0aeefce96dfcf49cd438d',
iban: 'TN0382001124170679299069',
bic: 'LXUEBTZ1',
Expand All @@ -90,6 +93,7 @@ const functionNames = [
'litecoinAddress',
'creditCardNumber',
'creditCardCVV',
'pin',
'ethereumAddress',
'iban',
'bic',
Expand Down Expand Up @@ -453,6 +457,34 @@ describe('finance', () => {
});
});

describe('pin()', () => {
it('should return a string', () => {
const pin = faker.finance.pin();
expect(pin).toBeTypeOf('string');
});

it('should contain only digits', () => {
const pin = faker.finance.pin();
expect(pin).toMatch(/^[0-9]+$/);
});

it('should default to a length of 4', () => {
const pin = faker.finance.pin();
expect(pin).toHaveLength(4);
});

it('should return a pin with the specified length', () => {
const pin = faker.finance.pin(5);
expect(pin).toHaveLength(5);
});

it('should throw an error when length is less than 1', () => {
expect(() => faker.finance.pin(-5)).toThrowError(
/^minimum length is 1$/
);
});
});

describe('ethereumAddress()', () => {
it('should return a valid ethereum address', () => {
const ethereumAddress = faker.finance.ethereumAddress();
Expand Down