From 61a5706c2e299156bb2a790e6f4116bcfc3a47b0 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sat, 10 Sep 2022 12:19:10 -0400 Subject: [PATCH 1/9] feat(finance): branchCode option in bic() --- src/modules/finance/index.ts | 19 ++++++++++++------- test/__snapshots__/datatype.spec.ts.snap | 6 ------ test/__snapshots__/finance.spec.ts.snap | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index c6f799764a5..cd57489e40a 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -395,23 +395,28 @@ export class Finance { /** * Generates a random SWIFT/BIC code based on the [ISO-9362](https://en.wikipedia.org/wiki/ISO_9362) format. * + * @param options Options object. + * @param options.branchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to `false`. + * * @example * faker.finance.bic() // 'WYAUPGX1432' */ - bic(): string { + bic( + options: { + branchCode?: boolean; + } = {} + ): string { + const { branchCode = false } = options; const bankIdentifier = this.faker.random.alpha({ count: 4, casing: 'upper', }); const countryCode = this.faker.helpers.arrayElement(iban.iso3166); const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' }); - const branchCode = this.faker.datatype.boolean() - ? this.faker.datatype.boolean() - ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) - : 'XXX' - : ''; - return `${bankIdentifier}${countryCode}${locationCode}${branchCode}`; + return `${bankIdentifier}${countryCode}${locationCode}${ + branchCode ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) : '' + }`; } /** diff --git a/test/__snapshots__/datatype.spec.ts.snap b/test/__snapshots__/datatype.spec.ts.snap index 7980ec5c2d3..0c92b8fee86 100644 --- a/test/__snapshots__/datatype.spec.ts.snap +++ b/test/__snapshots__/datatype.spec.ts.snap @@ -76,8 +76,6 @@ exports[`datatype > 42 > hexadecimal > with casing 1`] = `"0x8"`; exports[`datatype > 42 > hexadecimal > with length 1`] = `"0x8BE4ABdd39321aD7d3fe01FfCE404F4d6db0906bd8"`; -exports[`datatype > 42 > hexadecimal > with length and casing 1`] = `"0x8be4abdd39321ad7d3fe"`; - exports[`datatype > 42 > hexadecimal > with length, prefix, and casing 1`] = `"0x8be4abdd39321ad7d3fe"`; exports[`datatype > 42 > hexadecimal > with prefix 1`] = `"0x8"`; @@ -194,8 +192,6 @@ exports[`datatype > 1211 > hexadecimal > with casing 1`] = `"0xe"`; exports[`datatype > 1211 > hexadecimal > with length 1`] = `"0xEaDB42F0e3f4A973fAB0AeefCE96DFCF49cD438dF9"`; -exports[`datatype > 1211 > hexadecimal > with length and casing 1`] = `"0xeadb42f0e3f4a973fab0"`; - exports[`datatype > 1211 > hexadecimal > with length, prefix, and casing 1`] = `"0xeadb42f0e3f4a973fab0"`; exports[`datatype > 1211 > hexadecimal > with prefix 1`] = `"0xE"`; @@ -312,8 +308,6 @@ exports[`datatype > 1337 > hexadecimal > with casing 1`] = `"0x5"`; exports[`datatype > 1337 > hexadecimal > with length 1`] = `"0x5c346ba075bd57F5A62B82d72AF39CBBB07a98cbA8"`; -exports[`datatype > 1337 > hexadecimal > with length and casing 1`] = `"0x5c346ba075bd57f5a62b"`; - exports[`datatype > 1337 > hexadecimal > with length, prefix, and casing 1`] = `"0x5c346ba075bd57f5a62b"`; exports[`datatype > 1337 > hexadecimal > with prefix 1`] = `"0x5"`; diff --git a/test/__snapshots__/finance.spec.ts.snap b/test/__snapshots__/finance.spec.ts.snap index 1cc36e29923..665b4438ea5 100644 --- a/test/__snapshots__/finance.spec.ts.snap +++ b/test/__snapshots__/finance.spec.ts.snap @@ -16,7 +16,7 @@ exports[`finance > 42 > amount > with min 1`] = `"380.79"`; exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98160"`; -exports[`finance > 42 > bic 1`] = `"JUYEPSSLXXX"`; +exports[`finance > 42 > bic 1`] = `"JUYEPSSL"`; exports[`finance > 42 > bitcoinAddress 1`] = `"3XbJMAAara64sSkA9HD24YHQWd1b"`; From ff5f158221d6f8f8dd45840738072d428fa9351a Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 18 Sep 2022 10:54:29 -0400 Subject: [PATCH 2/9] docs: add jsdocs for `branchCode` option --- src/modules/finance/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index cd57489e40a..8ca0d8c74e0 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -399,7 +399,9 @@ export class Finance { * @param options.branchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to `false`. * * @example - * faker.finance.bic() // 'WYAUPGX1432' + * faker.finance.bic() // 'WYAUPGX1' + * faker.finance.bic({ branchCode: true }) // 'KCAUPGR1432' + * faker.finance.bic({ branchCode: false }) // 'XDAFQGT7' */ bic( options: { From 2b6cc3a63756d818702d606d800c9e2c71c53b92 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 18 Sep 2022 11:07:09 -0400 Subject: [PATCH 3/9] test: add tests for `branchCode` option --- test/finance.spec.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/finance.spec.ts b/test/finance.spec.ts index afc671a7628..b1b0d99c28c 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -480,11 +480,19 @@ describe('finance', () => { }); describe('bic()', () => { - it('should return a random yet formally correct BIC number', () => { + it('should return a BIC number', () => { const bic = faker.finance.bic(); expect(bic).toBeTypeOf('string'); - expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/); + expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}$/); + expect(ibanLib.iso3166).toContain(bic.substring(4, 6)); + }); + + it('should return a BIC number with branch code', () => { + const bic = faker.finance.bic({ branchCode: true }); + + expect(bic).toBeTypeOf('string'); + expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}[A-Z0-9]{3}$/); expect(ibanLib.iso3166).toContain(bic.substring(4, 6)); }); }); From ff7ea362ab2d192eebeafe0f2d44a98dfc1f345f Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Sun, 18 Sep 2022 11:24:33 -0400 Subject: [PATCH 4/9] test: add seeded tests with args --- test/__snapshots__/finance.spec.ts.snap | 12 ++++++++++++ test/finance.spec.ts | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/test/__snapshots__/finance.spec.ts.snap b/test/__snapshots__/finance.spec.ts.snap index 665b4438ea5..6f72ab1ac64 100644 --- a/test/__snapshots__/finance.spec.ts.snap +++ b/test/__snapshots__/finance.spec.ts.snap @@ -16,6 +16,10 @@ exports[`finance > 42 > amount > with min 1`] = `"380.79"`; exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98160"`; +exports[`finance > 42 > bic > noArgs 1`] = `"JUYEPSSL"`; + +exports[`finance > 42 > bic > with branch code 1`] = `"JUYEPSSLL5G"`; + exports[`finance > 42 > bic 1`] = `"JUYEPSSL"`; exports[`finance > 42 > bitcoinAddress 1`] = `"3XbJMAAara64sSkA9HD24YHQWd1b"`; @@ -80,6 +84,10 @@ exports[`finance > 1211 > amount > with min 1`] = `"929.24"`; exports[`finance > 1211 > amount > with min and max and dec and symbol 1`] = `"$47.14081"`; +exports[`finance > 1211 > bic > noArgs 1`] = `"YLXUDE4Z"`; + +exports[`finance > 1211 > bic > with branch code 1`] = `"YLXUDE4Z1O5"`; + exports[`finance > 1211 > bic 1`] = `"YLXUDE4Z"`; exports[`finance > 1211 > bitcoinAddress 1`] = `"1TMe8Z3EaFdLqmaGKP1LEEJQVriSZRZdsA"`; @@ -144,6 +152,10 @@ exports[`finance > 1337 > amount > with min 1`] = `"269.40"`; exports[`finance > 1337 > amount > with min and max and dec and symbol 1`] = `"$20.48098"`; +exports[`finance > 1337 > bic > noArgs 1`] = `"GOEFFIJG"`; + +exports[`finance > 1337 > bic > with branch code 1`] = `"GOEFFIJG1B8"`; + exports[`finance > 1337 > bic 1`] = `"GOEFFIJG"`; exports[`finance > 1337 > bitcoinAddress 1`] = `"3adhxs2jewAgkYgJi7No6Cn8JZa"`; diff --git a/test/finance.spec.ts b/test/finance.spec.ts index b1b0d99c28c..82adf52e202 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -25,7 +25,6 @@ describe('finance', () => { 'litecoinAddress', 'creditCardCVV', 'ethereumAddress', - 'bic', 'transactionDescription' ); @@ -44,6 +43,10 @@ describe('finance', () => { .it('with min and max and dec and symbol', 10, 50, 5, '$'); }); + t.describe('bic', (t) => { + t.it('noArgs').it('with branch code', { branchCode: true }); + }); + t.describe('iban', (t) => { t.it('noArgs') .it('with formatted', true) From cbf61a4dccefa20742078ca6cb0396d338905bd4 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Mon, 19 Sep 2022 19:02:55 -0400 Subject: [PATCH 5/9] chore: `branchCode` -> `includeBranchCode` --- src/modules/finance/index.ts | 14 ++++++++------ test/finance.spec.ts | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index a1b77b13011..2dff7ac80a3 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -430,21 +430,21 @@ export class FinanceModule { * Generates a random SWIFT/BIC code based on the [ISO-9362](https://en.wikipedia.org/wiki/ISO_9362) format. * * @param options Options object. - * @param options.branchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to `false`. + * @param options.includeBranchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to `false`. * * @example * faker.finance.bic() // 'WYAUPGX1' - * faker.finance.bic({ branchCode: true }) // 'KCAUPGR1432' - * faker.finance.bic({ branchCode: false }) // 'XDAFQGT7' + * faker.finance.bic({ includeBranchCode: true }) // 'KCAUPGR1432' + * faker.finance.bic({ includeBranchCode: false }) // 'XDAFQGT7' * * @since 4.0.0 */ bic( options: { - branchCode?: boolean; + includeBranchCode?: boolean; } = {} ): string { - const { branchCode = false } = options; + const { includeBranchCode = false } = options; const bankIdentifier = this.faker.random.alpha({ count: 4, casing: 'upper', @@ -453,7 +453,9 @@ export class FinanceModule { const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' }); return `${bankIdentifier}${countryCode}${locationCode}${ - branchCode ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) : '' + includeBranchCode + ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) + : '' }`; } diff --git a/test/finance.spec.ts b/test/finance.spec.ts index 82adf52e202..7253a09fd0a 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -44,7 +44,7 @@ describe('finance', () => { }); t.describe('bic', (t) => { - t.it('noArgs').it('with branch code', { branchCode: true }); + t.it('noArgs').it('with branch code', { includeBranchCode: true }); }); t.describe('iban', (t) => { @@ -492,7 +492,7 @@ describe('finance', () => { }); it('should return a BIC number with branch code', () => { - const bic = faker.finance.bic({ branchCode: true }); + const bic = faker.finance.bic({ includeBranchCode: true }); expect(bic).toBeTypeOf('string'); expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}[A-Z0-9]{3}$/); From 5389b3a26eac54617744cf99e71dbb3d78a4eefe Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Mon, 19 Sep 2022 19:08:50 -0400 Subject: [PATCH 6/9] chore: set default to rand bool, XXX generic case --- src/modules/finance/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 2dff7ac80a3..9632ef6ac36 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -430,7 +430,7 @@ export class FinanceModule { * Generates a random SWIFT/BIC code based on the [ISO-9362](https://en.wikipedia.org/wiki/ISO_9362) format. * * @param options Options object. - * @param options.includeBranchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to `false`. + * @param options.includeBranchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to a random boolean value. * * @example * faker.finance.bic() // 'WYAUPGX1' @@ -444,7 +444,7 @@ export class FinanceModule { includeBranchCode?: boolean; } = {} ): string { - const { includeBranchCode = false } = options; + const { includeBranchCode = this.faker.datatype.boolean() } = options; const bankIdentifier = this.faker.random.alpha({ count: 4, casing: 'upper', @@ -454,7 +454,9 @@ export class FinanceModule { return `${bankIdentifier}${countryCode}${locationCode}${ includeBranchCode - ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) + ? this.faker.datatype.boolean() + ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) + : 'XXX' : '' }`; } From 820aca1768ef89a26320be87d5571cfe56b8fa9f Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Tue, 20 Sep 2022 07:11:42 -0400 Subject: [PATCH 7/9] test: make tests pass --- test/__snapshots__/finance.spec.ts.snap | 18 ++++++------------ test/finance.spec.ts | 4 +++- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/test/__snapshots__/finance.spec.ts.snap b/test/__snapshots__/finance.spec.ts.snap index 6f72ab1ac64..5cd7be9a70e 100644 --- a/test/__snapshots__/finance.spec.ts.snap +++ b/test/__snapshots__/finance.spec.ts.snap @@ -16,11 +16,9 @@ exports[`finance > 42 > amount > with min 1`] = `"380.79"`; exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98160"`; -exports[`finance > 42 > bic > noArgs 1`] = `"JUYEPSSL"`; +exports[`finance > 42 > bic > noArgs 1`] = `"UYETSCLL"`; -exports[`finance > 42 > bic > with branch code 1`] = `"JUYEPSSLL5G"`; - -exports[`finance > 42 > bic 1`] = `"JUYEPSSL"`; +exports[`finance > 42 > bic > with branch code 1`] = `"JUYEPSSL5G5"`; exports[`finance > 42 > bitcoinAddress 1`] = `"3XbJMAAara64sSkA9HD24YHQWd1b"`; @@ -84,11 +82,9 @@ exports[`finance > 1211 > amount > with min 1`] = `"929.24"`; exports[`finance > 1211 > amount > with min and max and dec and symbol 1`] = `"$47.14081"`; -exports[`finance > 1211 > bic > noArgs 1`] = `"YLXUDE4Z"`; - -exports[`finance > 1211 > bic > with branch code 1`] = `"YLXUDE4Z1O5"`; +exports[`finance > 1211 > bic > noArgs 1`] = `"LXUFBTZ15O7"`; -exports[`finance > 1211 > bic 1`] = `"YLXUDE4Z"`; +exports[`finance > 1211 > bic > with branch code 1`] = `"YLXUDE4ZXXX"`; exports[`finance > 1211 > bitcoinAddress 1`] = `"1TMe8Z3EaFdLqmaGKP1LEEJQVriSZRZdsA"`; @@ -152,11 +148,9 @@ exports[`finance > 1337 > amount > with min 1`] = `"269.40"`; exports[`finance > 1337 > amount > with min and max and dec and symbol 1`] = `"$20.48098"`; -exports[`finance > 1337 > bic > noArgs 1`] = `"GOEFFIJG"`; - -exports[`finance > 1337 > bic > with branch code 1`] = `"GOEFFIJG1B8"`; +exports[`finance > 1337 > bic > noArgs 1`] = `"OEFHLYG1"`; -exports[`finance > 1337 > bic 1`] = `"GOEFFIJG"`; +exports[`finance > 1337 > bic > with branch code 1`] = `"GOEFFIJGXXX"`; exports[`finance > 1337 > bitcoinAddress 1`] = `"3adhxs2jewAgkYgJi7No6Cn8JZa"`; diff --git a/test/finance.spec.ts b/test/finance.spec.ts index 7253a09fd0a..6294569dc58 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -487,7 +487,9 @@ describe('finance', () => { const bic = faker.finance.bic(); expect(bic).toBeTypeOf('string'); - expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}$/); + bic.length === 8 + ? expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}$/) + : expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}[A-Z0-9]{3}$/); expect(ibanLib.iso3166).toContain(bic.substring(4, 6)); }); From 6e5626b3adcb95cf0a2bbbe7b7fa1c8d469813f4 Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Tue, 20 Sep 2022 11:13:47 -0400 Subject: [PATCH 8/9] test: refactor test regex to not use ternary --- test/finance.spec.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/finance.spec.ts b/test/finance.spec.ts index 6294569dc58..a2103e48ddf 100644 --- a/test/finance.spec.ts +++ b/test/finance.spec.ts @@ -487,9 +487,7 @@ describe('finance', () => { const bic = faker.finance.bic(); expect(bic).toBeTypeOf('string'); - bic.length === 8 - ? expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}$/) - : expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}[A-Z0-9]{3}$/); + expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/); expect(ibanLib.iso3166).toContain(bic.substring(4, 6)); }); From 54c4d40df4a0b8cea81121b5c6ad20dae970c05a Mon Sep 17 00:00:00 2001 From: Eric Cheng Date: Tue, 20 Sep 2022 11:14:34 -0400 Subject: [PATCH 9/9] refactor: move condition check to separate var --- src/modules/finance/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 9632ef6ac36..7ff005057a8 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -445,20 +445,20 @@ export class FinanceModule { } = {} ): string { const { includeBranchCode = this.faker.datatype.boolean() } = options; + const bankIdentifier = this.faker.random.alpha({ count: 4, casing: 'upper', }); const countryCode = this.faker.helpers.arrayElement(iban.iso3166); const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' }); + const branchCode = includeBranchCode + ? this.faker.datatype.boolean() + ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) + : 'XXX' + : ''; - return `${bankIdentifier}${countryCode}${locationCode}${ - includeBranchCode - ? this.faker.datatype.boolean() - ? this.faker.random.alphaNumeric(3, { casing: 'upper' }) - : 'XXX' - : '' - }`; + return `${bankIdentifier}${countryCode}${locationCode}${branchCode}`; } /**