Skip to content

Commit 5a397e0

Browse files
authored
fix(finance.bic): remove hardcoded elements and simplify function (#1171)
1 parent aafab45 commit 5a397e0

3 files changed

Lines changed: 19 additions & 28 deletions

File tree

src/modules/finance/index.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -390,29 +390,25 @@ export class Finance {
390390
}
391391

392392
/**
393-
* Generates a random bic.
393+
* Generates a random SWIFT/BIC code based on the [ISO-9362](https://en.wikipedia.org/wiki/ISO_9362) format.
394394
*
395395
* @example
396396
* faker.finance.bic() // 'WYAUPGX1432'
397397
*/
398398
bic(): string {
399-
const vowels = ['A', 'E', 'I', 'O', 'U'];
400-
const prob = this.faker.datatype.number(100);
401-
402-
return [
403-
this.faker.helpers.replaceSymbols('???'),
404-
this.faker.helpers.arrayElement(vowels),
405-
this.faker.helpers.arrayElement(iban.iso3166),
406-
this.faker.helpers.replaceSymbols('?'),
407-
'1',
408-
prob < 10
409-
? this.faker.helpers.replaceSymbols(
410-
`?${this.faker.helpers.arrayElement(vowels)}?`
411-
)
412-
: prob < 40
413-
? this.faker.helpers.replaceSymbols('###')
414-
: '',
415-
].join('');
399+
const bankIdentifier = this.faker.random.alpha({
400+
count: 4,
401+
casing: 'upper',
402+
});
403+
const countryCode = this.faker.helpers.arrayElement(iban.iso3166);
404+
const locationCode = this.faker.random.alphaNumeric(2, { casing: 'upper' });
405+
const branchCode = this.faker.datatype.boolean()
406+
? this.faker.datatype.boolean()
407+
? this.faker.random.alphaNumeric(3, { casing: 'upper' })
408+
: 'XXX'
409+
: '';
410+
411+
return `${bankIdentifier}${countryCode}${locationCode}${branchCode}`;
416412
}
417413

418414
/**

test/__snapshots__/finance.spec.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ 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`] = `"UYEOSCP1514"`;
19+
exports[`finance > 42 > bic 1`] = `"JUYEPSSLXXX"`;
2020

2121
exports[`finance > 42 > bitcoinAddress 1`] = `"3XbJMAAara64sSkA9HD24YHQWd1b"`;
2222

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

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

83-
exports[`finance > 1211 > bic 1`] = `"LXUEBTZ1"`;
83+
exports[`finance > 1211 > bic 1`] = `"YLXUDE4Z"`;
8484

8585
exports[`finance > 1211 > bitcoinAddress 1`] = `"1TMe8Z3EaFdLqmaGKP1LEEJQVriSZRZdsA"`;
8686

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

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

147-
exports[`finance > 1337 > bic 1`] = `"OEFELYL1032"`;
147+
exports[`finance > 1337 > bic 1`] = `"GOEFFIJG"`;
148148

149149
exports[`finance > 1337 > bitcoinAddress 1`] = `"3adhxs2jewAgkYgJi7No6Cn8JZa"`;
150150

test/finance.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,15 +482,10 @@ describe('finance', () => {
482482
describe('bic()', () => {
483483
it('should return a random yet formally correct BIC number', () => {
484484
const bic = faker.finance.bic();
485-
const expr = new RegExp(
486-
`^[A-Z]{4}(${ibanLib.iso3166.join(
487-
'|'
488-
)})[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3})?\$`,
489-
'i'
490-
);
491485

492486
expect(bic).toBeTypeOf('string');
493-
expect(bic).toMatch(expr);
487+
expect(bic).toMatch(/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/);
488+
expect(ibanLib.iso3166).toContain(bic.substring(4, 6));
494489
});
495490
});
496491

0 commit comments

Comments
 (0)