From 20f33e6640551b1d95059207ae2a54ba9115690c Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Wed, 6 Apr 2022 03:56:39 -0400 Subject: [PATCH] feat: faker.finance.pin() (#695) --- src/finance.ts | 17 +++++++++++++++++ test/finance.spec.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/finance.ts b/src/finance.ts index 5581ad4eac4..32f8454ecb6 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -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. * diff --git a/test/finance.spec.ts b/test/finance.spec.ts index ce529fe2951..57ef3029160 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -21,6 +21,7 @@ const seedRuns = [ litecoinAddress: '3XbJMAAara64sSkA9HD24YHQWd1b', creditCardNumber: '3581-7755-1410-0486', creditCardCVV: '379', + pin: '3791', ethereumAddress: '0x8be4abdd39321ad7d3fe01ffce404f4d6db0906b', iban: 'GT30Y75110867098F1E3542612J4', bic: 'UYEOSCP1514', @@ -44,6 +45,7 @@ const seedRuns = [ litecoinAddress: 'Madhxs2jewAgkYgJi7No6Cn8JZar', creditCardNumber: '6011-6212-2540-3255-2392', creditCardCVV: '251', + pin: '2512', ethereumAddress: '0x5c346ba075bd57f5a62b82d72af39cbbb07a98cb', iban: 'FO7710540350900318', bic: 'OEFELYL1032', @@ -67,6 +69,7 @@ const seedRuns = [ litecoinAddress: 'MTMe8Z3EaFdLqmaGKP1LEEJQVriSZRZds', creditCardNumber: '4872190616276', creditCardCVV: '948', + pin: '9487', ethereumAddress: '0xeadb42f0e3f4a973fab0aeefce96dfcf49cd438d', iban: 'TN0382001124170679299069', bic: 'LXUEBTZ1', @@ -90,6 +93,7 @@ const functionNames = [ 'litecoinAddress', 'creditCardNumber', 'creditCardCVV', + 'pin', 'ethereumAddress', 'iban', 'bic', @@ -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();