Skip to content

Commit fe97c29

Browse files
authored
feat(finance): branch code option in bic() (#1378)
1 parent ea8d873 commit fe97c29

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/modules/finance/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,19 +429,30 @@ export class FinanceModule {
429429
/**
430430
* Generates a random SWIFT/BIC code based on the [ISO-9362](https://en.wikipedia.org/wiki/ISO_9362) format.
431431
*
432+
* @param options Options object.
433+
* @param options.includeBranchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to a random boolean value.
434+
*
432435
* @example
433-
* faker.finance.bic() // 'WYAUPGX1432'
436+
* faker.finance.bic() // 'WYAUPGX1'
437+
* faker.finance.bic({ includeBranchCode: true }) // 'KCAUPGR1432'
438+
* faker.finance.bic({ includeBranchCode: false }) // 'XDAFQGT7'
434439
*
435440
* @since 4.0.0
436441
*/
437-
bic(): string {
442+
bic(
443+
options: {
444+
includeBranchCode?: boolean;
445+
} = {}
446+
): string {
447+
const { includeBranchCode = this.faker.datatype.boolean() } = options;
448+
438449
const bankIdentifier = this.faker.random.alpha({
439450
count: 4,
440451
casing: 'upper',
441452
});
442453
const countryCode = this.faker.helpers.arrayElement(iban.iso3166);
443454
const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' });
444-
const branchCode = this.faker.datatype.boolean()
455+
const branchCode = includeBranchCode
445456
? this.faker.datatype.boolean()
446457
? this.faker.random.alphaNumeric(3, { casing: 'upper' })
447458
: 'XXX'

test/__snapshots__/finance.spec.ts.snap

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ exports[`finance > 42 > amount > with min 1`] = `"380.79"`;
1616

1717
exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98160"`;
1818

19-
exports[`finance > 42 > bic 1`] = `"JUYEPSSLXXX"`;
19+
exports[`finance > 42 > bic > noArgs 1`] = `"UYETSCLL"`;
20+
21+
exports[`finance > 42 > bic > with branch code 1`] = `"JUYEPSSL5G5"`;
2022

2123
exports[`finance > 42 > bitcoinAddress 1`] = `"3XbJMAAara64sSkA9HD24YHQWd1bZb"`;
2224

@@ -80,7 +82,9 @@ exports[`finance > 1211 > amount > with min 1`] = `"929.24"`;
8082

8183
exports[`finance > 1211 > amount > with min and max and dec and symbol 1`] = `"$47.14081"`;
8284

83-
exports[`finance > 1211 > bic 1`] = `"YLXUDE4Z"`;
85+
exports[`finance > 1211 > bic > noArgs 1`] = `"LXUFBTZ15O7"`;
86+
87+
exports[`finance > 1211 > bic > with branch code 1`] = `"YLXUDE4ZXXX"`;
8488

8589
exports[`finance > 1211 > bitcoinAddress 1`] = `"1TMe8Z3EaFdLqmaGKP1LEEJQVriSZRZdsAUc9n"`;
8690

@@ -144,7 +148,9 @@ exports[`finance > 1337 > amount > with min 1`] = `"269.40"`;
144148

145149
exports[`finance > 1337 > amount > with min and max and dec and symbol 1`] = `"$20.48098"`;
146150

147-
exports[`finance > 1337 > bic 1`] = `"GOEFFIJG"`;
151+
exports[`finance > 1337 > bic > noArgs 1`] = `"OEFHLYG1"`;
152+
153+
exports[`finance > 1337 > bic > with branch code 1`] = `"GOEFFIJGXXX"`;
148154

149155
exports[`finance > 1337 > bitcoinAddress 1`] = `"3adhxs2jewAgkYgJi7No6Cn8JZar"`;
150156

test/finance.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ describe('finance', () => {
2626
'litecoinAddress',
2727
'creditCardCVV',
2828
'ethereumAddress',
29-
'bic',
3029
'transactionDescription'
3130
);
3231

@@ -45,6 +44,10 @@ describe('finance', () => {
4544
.it('with min and max and dec and symbol', 10, 50, 5, '$');
4645
});
4746

47+
t.describe('bic', (t) => {
48+
t.it('noArgs').it('with branch code', { includeBranchCode: true });
49+
});
50+
4851
t.describe('iban', (t) => {
4952
t.it('noArgs')
5053
.it('with formatted', true)
@@ -481,13 +484,21 @@ describe('finance', () => {
481484
});
482485

483486
describe('bic()', () => {
484-
it('should return a random yet formally correct BIC number', () => {
487+
it('should return a BIC number', () => {
485488
const bic = faker.finance.bic();
486489

487490
expect(bic).toBeTypeOf('string');
488491
expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/);
489492
expect(ibanLib.iso3166).toContain(bic.substring(4, 6));
490493
});
494+
495+
it('should return a BIC number with branch code', () => {
496+
const bic = faker.finance.bic({ includeBranchCode: true });
497+
498+
expect(bic).toBeTypeOf('string');
499+
expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}[A-Z0-9]{3}$/);
500+
expect(ibanLib.iso3166).toContain(bic.substring(4, 6));
501+
});
491502
});
492503

493504
describe('transactionDescription()', () => {

0 commit comments

Comments
 (0)