Skip to content

Commit

Permalink
feat: faker.finance.pin() (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
import-brain committed Apr 6, 2022
1 parent 40c9d5a commit 20f33e6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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.
*
* @example
* faker.finance.pin() // '5067'
* 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

0 comments on commit 20f33e6

Please sign in to comment.