From 9ee0924b0a5bdaa60d305328d4241912de8722f5 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 10:13:56 -0400 Subject: [PATCH 01/11] feat: pin number function --- src/finance.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/finance.ts b/src/finance.ts index 25d44046158..f3ddf4c913f 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -290,6 +290,22 @@ export class Finance { return cvv; } + /** + * Generates a random PIN number. + * + * @param digits The number of digits to generate. Defaults to 4. + * + * @example + * faker.finance.pin() // '506' + */ + pin(digits: number = 4): string { + let pin = ''; + for (let i = 0; i < digits; i++) { + pin += '#'; + } + return this.faker.helpers.replaceSymbols(pin); + } + /** * Generates a random ethereum Address. * From e190df8715118b79edf04ce359796cce8aaf3ecd Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 10:36:07 -0400 Subject: [PATCH 02/11] test: add finance.pin() tests --- test/finance.spec.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/finance.spec.ts b/test/finance.spec.ts index f5d4521adc6..5dd12df6b6d 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -20,6 +20,7 @@ const seedRuns = [ litecoinAddress: '3XbJMAAara64sSkA9HD24YHQWd1b', creditCardNumber: '3581-7755-1410-0486', creditCardCVV: '379', + pin: '3791', ethereumAddress: '0x8be4abdd39321ad7d3fe01ffce404f4d6db0906b', iban: 'GT30Y75110867098F1E3542612J4', bic: 'UYEOSCP1514', @@ -43,6 +44,7 @@ const seedRuns = [ litecoinAddress: 'Madhxs2jewAgkYgJi7No6Cn8JZar', creditCardNumber: '6011-6212-2540-3255-2392', creditCardCVV: '251', + pin: '2512', ethereumAddress: '0x5c346ba075bd57f5a62b82d72af39cbbb07a98cb', iban: 'FO7710540350900318', bic: 'OEFELYL1032', @@ -66,6 +68,7 @@ const seedRuns = [ litecoinAddress: 'MTMe8Z3EaFdLqmaGKP1LEEJQVriSZRZds', creditCardNumber: '4872190616276', creditCardCVV: '948', + pin: '9487', ethereumAddress: '0xeadb42f0e3f4a973fab0aeefce96dfcf49cd438d', iban: 'TN0382001124170679299069', bic: 'LXUEBTZ1', @@ -89,6 +92,7 @@ const functionNames = [ 'litecoinAddress', 'creditCardNumber', 'creditCardCVV', + 'pin', 'ethereumAddress', 'iban', 'bic', @@ -452,6 +456,39 @@ describe('finance', () => { }); }); + describe('pin()', () => { + it('should return a string', () => { + const pin = faker.finance.pin(); + expect(pin).toBeTypeOf('string'); + }); + + it('should have all characters be a digit', () => { + const pin = faker.finance.pin(); + expect(pin).toMatch(/^[0-9]+$/); + }); + + it('should default to 4 digits', () => { + const pin = faker.finance.pin(); + expect(pin).toHaveLength(4); + }); + + it('should return a pin with the specified length', () => { + let pin = faker.finance.pin(1); + expect(pin).toHaveLength(1); + pin = faker.finance.pin(5); + expect(pin).toHaveLength(5); + pin = faker.finance.pin(13); + expect(pin).toHaveLength(13); + pin = faker.finance.pin(57); + expect(pin).toHaveLength(57); + }); + + it('should not crash when digits parameter is 0', () => { + const pin = faker.finance.pin(0); + expect(pin).toHaveLength(0); + }); + }); + describe('ethereumAddress()', () => { it('should return a valid ethereum address', () => { const ethereumAddress = faker.finance.ethereumAddress(); From 0e7c02670f586a251ae31f7c15c75334aaf3a81f Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 11:13:16 -0400 Subject: [PATCH 03/11] docs: incorporate @xDivisionByZerox's fix --- src/finance.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/finance.ts b/src/finance.ts index f3ddf4c913f..f8dd0fcd57d 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -296,7 +296,7 @@ export class Finance { * @param digits The number of digits to generate. Defaults to 4. * * @example - * faker.finance.pin() // '506' + * faker.finance.pin() // '5067' */ pin(digits: number = 4): string { let pin = ''; From 1a1704599ea3e1aa846f7757f84a54367e1478ff Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 11:23:22 -0400 Subject: [PATCH 04/11] fix: add pkuczynski's simplification of function and tests --- src/finance.ts | 10 +++------- test/finance.spec.ts | 12 +++--------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/finance.ts b/src/finance.ts index f8dd0fcd57d..8c7975df2ab 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -293,17 +293,13 @@ export class Finance { /** * Generates a random PIN number. * - * @param digits The number of digits to generate. Defaults to 4. + * @param length The length of the PIN to generate. Defaults to 4. * * @example * faker.finance.pin() // '5067' */ - pin(digits: number = 4): string { - let pin = ''; - for (let i = 0; i < digits; i++) { - pin += '#'; - } - return this.faker.helpers.replaceSymbols(pin); + pin(length: number = 4): string { + return Array.from({ length }, () => this.faker.datatype.number(9)).join(''); } /** diff --git a/test/finance.spec.ts b/test/finance.spec.ts index 5dd12df6b6d..875f5f4c8da 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -467,23 +467,17 @@ describe('finance', () => { expect(pin).toMatch(/^[0-9]+$/); }); - it('should default to 4 digits', () => { + 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', () => { - let pin = faker.finance.pin(1); - expect(pin).toHaveLength(1); - pin = faker.finance.pin(5); + const pin = faker.finance.pin(5); expect(pin).toHaveLength(5); - pin = faker.finance.pin(13); - expect(pin).toHaveLength(13); - pin = faker.finance.pin(57); - expect(pin).toHaveLength(57); }); - it('should not crash when digits parameter is 0', () => { + it('should not crash when length parameter is 0', () => { const pin = faker.finance.pin(0); expect(pin).toHaveLength(0); }); From 42248c4e1aad3b517e57d21f26863797f180c32c Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 12:36:34 -0400 Subject: [PATCH 05/11] fix: make test name more clear Co-authored-by: Piotr Kuczynski --- test/finance.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/finance.spec.ts b/test/finance.spec.ts index 875f5f4c8da..452184963cf 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -462,7 +462,7 @@ describe('finance', () => { expect(pin).toBeTypeOf('string'); }); - it('should have all characters be a digit', () => { + it('should contain only digits', () => { const pin = faker.finance.pin(); expect(pin).toMatch(/^[0-9]+$/); }); From 00a0df50042700e6e8bf74ca9451517175620381 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 12:38:06 -0400 Subject: [PATCH 06/11] fix: provide example where length is given --- src/finance.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/finance.ts b/src/finance.ts index 8c7975df2ab..1e648990495 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -297,6 +297,7 @@ export class Finance { * * @example * faker.finance.pin() // '5067' + * faker.finance.pin(6) // '213789' */ pin(length: number = 4): string { return Array.from({ length }, () => this.faker.datatype.number(9)).join(''); From 21095888f44f27e1cf60e13577a00926e4fb2536 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 13:23:27 -0400 Subject: [PATCH 07/11] feat: throws error when length < 1, add test for error --- src/finance.ts | 4 ++++ test/finance.spec.ts | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/finance.ts b/src/finance.ts index 1e648990495..fa73e0ec555 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -294,12 +294,16 @@ export class Finance { * 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 Error('PIN length must be 1 or greater'); + } return Array.from({ length }, () => this.faker.datatype.number(9)).join(''); } diff --git a/test/finance.spec.ts b/test/finance.spec.ts index 452184963cf..b007bcf5e62 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -477,9 +477,10 @@ describe('finance', () => { expect(pin).toHaveLength(5); }); - it('should not crash when length parameter is 0', () => { - const pin = faker.finance.pin(0); - expect(pin).toHaveLength(0); + it('should throw an error when length is less than 1', () => { + expect(() => faker.finance.pin(-5)).toThrowError( + /^PIN length must be 1 or greater$/ + ); }); }); From 60b3f94ed710f5dfb5eaa20f60c5cf1d08cd3a1b Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 14:19:02 -0400 Subject: [PATCH 08/11] Update src/finance.ts Co-authored-by: Piotr Kuczynski --- src/finance.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/finance.ts b/src/finance.ts index fa73e0ec555..9914cc07655 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -302,7 +302,7 @@ export class Finance { */ pin(length: number = 4): string { if (length < 1) { - throw new Error('PIN length must be 1 or greater'); + throw new Error('minimum length is 1'); } return Array.from({ length }, () => this.faker.datatype.number(9)).join(''); } From d56b65e9284693edac25429f2c2247b66308f544 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 14:35:24 -0400 Subject: [PATCH 09/11] Update src/finance.ts Co-authored-by: ST-DDT --- src/finance.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/finance.ts b/src/finance.ts index 9914cc07655..3257c05776b 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -293,7 +293,7 @@ export class Finance { /** * Generates a random PIN number. * - * @param length The length of the PIN to generate. Defaults to 4. + * @param length The length of the PIN to generate. Defaults to `4`. * @throws Will throw an error if length is less than 1. * * @example From 5fbf2795714b5f18d9949837eb8dea8055969db0 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 27 Mar 2022 14:37:47 -0400 Subject: [PATCH 10/11] fix: new error message in test --- test/finance.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/finance.spec.ts b/test/finance.spec.ts index b007bcf5e62..d2b5d571b45 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -479,7 +479,7 @@ describe('finance', () => { it('should throw an error when length is less than 1', () => { expect(() => faker.finance.pin(-5)).toThrowError( - /^PIN length must be 1 or greater$/ + /^minimum length is 1$/ ); }); }); From a8abe453f25edd482ecd07c1039603d75f704596 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Tue, 5 Apr 2022 20:59:47 -0400 Subject: [PATCH 11/11] fix: add @Shinigami92's suggestion (change error to FakerError) --- src/finance.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/finance.ts b/src/finance.ts index ddd9c262c93..32f8454ecb6 100644 --- a/src/finance.ts +++ b/src/finance.ts @@ -303,7 +303,7 @@ export class Finance { */ pin(length: number = 4): string { if (length < 1) { - throw new Error('minimum length is 1'); + throw new FakerError('minimum length is 1'); } return Array.from({ length }, () => this.faker.datatype.number(9)).join(''); }