aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPiotr Kuczynski <[email protected]>2022-04-06 10:29:46 +0200
committerGitHub <[email protected]>2022-04-06 08:29:46 +0000
commit3e5aaf10b09df284bc286123238866855aff9090 (patch)
tree7271cf3acdd1256171cce7153aa7cbe6f436b64f /src
parentbc1a9a47082eeaafd612b98073bb2f8517897795 (diff)
downloadfaker-3e5aaf10b09df284bc286123238866855aff9090.tar.xz
faker-3e5aaf10b09df284bc286123238866855aff9090.zip
chore: IBAN library typed and moved to utils (#785)
Diffstat (limited to 'src')
-rw-r--r--src/finance.ts41
-rw-r--r--src/utils/iban.ts (renamed from src/iban.ts)45
2 files changed, 46 insertions, 40 deletions
diff --git a/src/finance.ts b/src/finance.ts
index 32f8454e..54c6a78b 100644
--- a/src/finance.ts
+++ b/src/finance.ts
@@ -1,13 +1,12 @@
import type { Faker } from '.';
import { FakerError } from './errors/faker-error';
import type { Helpers } from './helpers';
-import ibanLib from './iban';
+import iban from './utils/iban';
/**
* Module to generate finance related entries.
*/
export class Finance {
- readonly ibanLib = ibanLib;
readonly Helpers: Helpers;
constructor(private readonly faker: Faker) {
@@ -332,19 +331,9 @@ export class Finance {
* faker.finance.iban(true, 'DE') // 'DE84 1022 7075 0900 1170 01'
*/
iban(formatted: boolean = false, countryCode?: string): string {
- let ibanFormat: {
- bban: Array<{ type: string; count: number }>;
- country: string;
- total?: number;
- format?: string;
- };
- if (countryCode) {
- const findFormat = (currentFormat) =>
- currentFormat.country === countryCode;
- ibanFormat = this.ibanLib.formats.find(findFormat);
- } else {
- ibanFormat = this.faker.random.arrayElement(this.ibanLib.formats);
- }
+ const ibanFormat = countryCode
+ ? iban.formats.find((f) => f.country === countryCode)
+ : this.faker.random.arrayElement(iban.formats);
if (!ibanFormat) {
throw new FakerError('Country code ' + countryCode + ' not supported.');
@@ -357,20 +346,20 @@ export class Finance {
count += bban.count;
while (c > 0) {
if (bban.type === 'a') {
- s += this.faker.random.arrayElement(this.ibanLib.alpha);
+ s += this.faker.random.arrayElement(iban.alpha);
} else if (bban.type === 'c') {
if (this.faker.datatype.number(100) < 80) {
s += this.faker.datatype.number(9);
} else {
- s += this.faker.random.arrayElement(this.ibanLib.alpha);
+ s += this.faker.random.arrayElement(iban.alpha);
}
} else {
if (c >= 3 && this.faker.datatype.number(100) < 30) {
if (this.faker.datatype.boolean()) {
- s += this.faker.random.arrayElement(this.ibanLib.pattern100);
+ s += this.faker.random.arrayElement(iban.pattern100);
c -= 2;
} else {
- s += this.faker.random.arrayElement(this.ibanLib.pattern10);
+ s += this.faker.random.arrayElement(iban.pattern10);
c--;
}
} else {
@@ -382,15 +371,15 @@ export class Finance {
s = s.substring(0, count);
}
let checksum: string | number =
- 98 -
- this.ibanLib.mod97(
- this.ibanLib.toDigitString(`${s}${ibanFormat.country}00`)
- );
+ 98 - iban.mod97(iban.toDigitString(`${s}${ibanFormat.country}00`));
+
if (checksum < 10) {
checksum = `0${checksum}`;
}
- const iban = `${ibanFormat.country}${checksum}${s}`;
- return formatted ? iban.match(/.{1,4}/g).join(' ') : iban;
+
+ const result = `${ibanFormat.country}${checksum}${s}`;
+
+ return formatted ? result.match(/.{1,4}/g).join(' ') : result;
}
/**
@@ -405,7 +394,7 @@ export class Finance {
return (
this.Helpers.replaceSymbols('???') +
this.faker.random.arrayElement(vowels) +
- this.faker.random.arrayElement(this.ibanLib.iso3166) +
+ this.faker.random.arrayElement(iban.iso3166) +
this.Helpers.replaceSymbols('?') +
'1' +
(prob < 10
diff --git a/src/iban.ts b/src/utils/iban.ts
index 2e67f281..9b4780c5 100644
--- a/src/iban.ts
+++ b/src/utils/iban.ts
@@ -1,4 +1,19 @@
-export = {
+interface Iban {
+ alpha: string[];
+ formats: Array<{
+ bban: Array<{ type: string; count: number }>;
+ country: string;
+ format?: string;
+ total?: number;
+ }>;
+ iso3166: string[];
+ mod97: (digitStr: string) => number;
+ pattern10: string[];
+ pattern100: string[];
+ toDigitString: (str: string) => string;
+}
+
+const iban: Iban = {
alpha: [
'A',
'B',
@@ -27,19 +42,6 @@ export = {
'Y',
'Z',
],
- pattern10: ['01', '02', '03', '04', '05', '06', '07', '08', '09'],
- pattern100: ['001', '002', '003', '004', '005', '006', '007', '008', '009'],
- toDigitString: (str: string): string =>
- str.replace(/[A-Z]/gi, (match) =>
- String(match.toUpperCase().charCodeAt(0) - 55)
- ),
- mod97: (digitStr: string): number => {
- let m = 0;
- for (let i = 0; i < digitStr.length; i++) {
- m = (m * 10 + +digitStr[i]) % 97;
- }
- return m;
- },
formats: [
{
country: 'AL',
@@ -1394,4 +1396,19 @@ export = {
'ZM',
'ZW',
],
+ mod97: (digitStr) => {
+ let m = 0;
+ for (let i = 0; i < digitStr.length; i++) {
+ m = (m * 10 + +digitStr[i]) % 97;
+ }
+ return m;
+ },
+ pattern10: ['01', '02', '03', '04', '05', '06', '07', '08', '09'],
+ pattern100: ['001', '002', '003', '004', '005', '006', '007', '008', '009'],
+ toDigitString: (str) =>
+ str.replace(/[A-Z]/gi, (match) =>
+ String(match.toUpperCase().charCodeAt(0) - 55)
+ ),
};
+
+export default iban;