diff options
| author | Shinigami <[email protected]> | 2022-05-03 15:48:20 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-03 15:48:20 +0200 |
| commit | a2da7c496e9a3741d165ddfe6128b50837fec361 (patch) | |
| tree | 88d371bc19487bc8a34d9043035aed8e4fedd7d5 /src/modules/finance | |
| parent | cc46a0c19af2752b6210c24b715fcce20197b6d9 (diff) | |
| download | faker-a2da7c496e9a3741d165ddfe6128b50837fec361.tar.xz faker-a2da7c496e9a3741d165ddfe6128b50837fec361.zip | |
refactor!: reorganize src folder (#909)
Diffstat (limited to 'src/modules/finance')
| -rw-r--r-- | src/modules/finance/iban.ts | 1414 | ||||
| -rw-r--r-- | src/modules/finance/index.ts | 436 |
2 files changed, 1850 insertions, 0 deletions
diff --git a/src/modules/finance/iban.ts b/src/modules/finance/iban.ts new file mode 100644 index 00000000..9b4780c5 --- /dev/null +++ b/src/modules/finance/iban.ts @@ -0,0 +1,1414 @@ +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', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + ], + formats: [ + { + country: 'AL', + total: 28, + bban: [ + { + type: 'n', + count: 8, + }, + { + type: 'c', + count: 16, + }, + ], + format: 'ALkk bbbs sssx cccc cccc cccc cccc', + }, + { + country: 'AD', + total: 24, + bban: [ + { + type: 'n', + count: 8, + }, + { + type: 'c', + count: 12, + }, + ], + format: 'ADkk bbbb ssss cccc cccc cccc', + }, + { + country: 'AT', + total: 20, + bban: [ + { + type: 'n', + count: 5, + }, + { + type: 'n', + count: 11, + }, + ], + format: 'ATkk bbbb bccc cccc cccc', + }, + { + // Azerbaijan + // https://transferwise.com/fr/iban/azerbaijan + // Length 28 + // BBAN 2c,16n + // GEkk bbbb cccc cccc cccc cccc cccc + // b = National bank code (alpha) + // c = Account number + // example IBAN AZ21 NABZ 0000 0000 1370 1000 1944 + country: 'AZ', + total: 28, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 20, + }, + ], + format: 'AZkk bbbb cccc cccc cccc cccc cccc', + }, + { + country: 'BH', + total: 22, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'c', + count: 14, + }, + ], + format: 'BHkk bbbb cccc cccc cccc cc', + }, + { + country: 'BE', + total: 16, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'n', + count: 9, + }, + ], + format: 'BEkk bbbc cccc ccxx', + }, + { + country: 'BA', + total: 20, + bban: [ + { + type: 'n', + count: 6, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'BAkk bbbs sscc cccc ccxx', + }, + { + country: 'BR', + total: 29, + bban: [ + { + type: 'n', + count: 13, + }, + { + type: 'n', + count: 10, + }, + { + type: 'a', + count: 1, + }, + { + type: 'c', + count: 1, + }, + ], + format: 'BRkk bbbb bbbb ssss sccc cccc ccct n', + }, + { + country: 'BG', + total: 22, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 6, + }, + { + type: 'c', + count: 8, + }, + ], + format: 'BGkk bbbb ssss ddcc cccc cc', + }, + { + country: 'CR', + total: 22, + bban: [ + { + type: 'n', + count: 1, + }, + { + type: 'n', + count: 3, + }, + { + type: 'n', + count: 14, + }, + ], + format: 'CRkk xbbb cccc cccc cccc cc', + }, + { + country: 'HR', + total: 21, + bban: [ + { + type: 'n', + count: 7, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'HRkk bbbb bbbc cccc cccc c', + }, + { + country: 'CY', + total: 28, + bban: [ + { + type: 'n', + count: 8, + }, + { + type: 'c', + count: 16, + }, + ], + format: 'CYkk bbbs ssss cccc cccc cccc cccc', + }, + { + country: 'CZ', + total: 24, + bban: [ + { + type: 'n', + count: 10, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'CZkk bbbb ssss sscc cccc cccc', + }, + { + country: 'DK', + total: 18, + bban: [ + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'DKkk bbbb cccc cccc cc', + }, + { + country: 'DO', + total: 28, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 20, + }, + ], + format: 'DOkk bbbb cccc cccc cccc cccc cccc', + }, + { + country: 'TL', + total: 23, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'TLkk bbbc cccc cccc cccc cxx', + }, + { + country: 'EE', + total: 20, + bban: [ + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 12, + }, + ], + format: 'EEkk bbss cccc cccc cccx', + }, + { + country: 'FO', + total: 18, + bban: [ + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'FOkk bbbb cccc cccc cx', + }, + { + country: 'FI', + total: 18, + bban: [ + { + type: 'n', + count: 6, + }, + { + type: 'n', + count: 8, + }, + ], + format: 'FIkk bbbb bbcc cccc cx', + }, + { + country: 'FR', + total: 27, + bban: [ + { + type: 'n', + count: 10, + }, + { + type: 'c', + count: 11, + }, + { + type: 'n', + count: 2, + }, + ], + format: 'FRkk bbbb bggg ggcc cccc cccc cxx', + }, + { + country: 'GE', + total: 22, + bban: [ + { + type: 'a', + count: 2, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'GEkk bbcc cccc cccc cccc cc', + }, + { + country: 'DE', + total: 22, + bban: [ + { + type: 'n', + count: 8, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'DEkk bbbb bbbb cccc cccc cc', + }, + { + country: 'GI', + total: 23, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'c', + count: 15, + }, + ], + format: 'GIkk bbbb cccc cccc cccc ccc', + }, + { + country: 'GR', + total: 27, + bban: [ + { + type: 'n', + count: 7, + }, + { + type: 'c', + count: 16, + }, + ], + format: 'GRkk bbbs sssc cccc cccc cccc ccc', + }, + { + country: 'GL', + total: 18, + bban: [ + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'GLkk bbbb cccc cccc cc', + }, + { + country: 'GT', + total: 28, + bban: [ + { + type: 'c', + count: 4, + }, + { + type: 'c', + count: 4, + }, + { + type: 'c', + count: 16, + }, + ], + format: 'GTkk bbbb mmtt cccc cccc cccc cccc', + }, + { + country: 'HU', + total: 28, + bban: [ + { + type: 'n', + count: 8, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'HUkk bbbs sssk cccc cccc cccc cccx', + }, + { + country: 'IS', + total: 26, + bban: [ + { + type: 'n', + count: 6, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'ISkk bbbb sscc cccc iiii iiii ii', + }, + { + country: 'IE', + total: 22, + bban: [ + { + type: 'c', + count: 4, + }, + { + type: 'n', + count: 6, + }, + { + type: 'n', + count: 8, + }, + ], + format: 'IEkk aaaa bbbb bbcc cccc cc', + }, + { + country: 'IL', + total: 23, + bban: [ + { + type: 'n', + count: 6, + }, + { + type: 'n', + count: 13, + }, + ], + format: 'ILkk bbbn nncc cccc cccc ccc', + }, + { + country: 'IT', + total: 27, + bban: [ + { + type: 'a', + count: 1, + }, + { + type: 'n', + count: 10, + }, + { + type: 'c', + count: 12, + }, + ], + format: 'ITkk xaaa aabb bbbc cccc cccc ccc', + }, + { + country: 'JO', + total: 30, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 18, + }, + ], + format: 'JOkk bbbb nnnn cccc cccc cccc cccc cc', + }, + { + country: 'KZ', + total: 20, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'c', + count: 13, + }, + ], + format: 'KZkk bbbc cccc cccc cccc', + }, + { + country: 'XK', + total: 20, + bban: [ + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 12, + }, + ], + format: 'XKkk bbbb cccc cccc cccc', + }, + { + country: 'KW', + total: 30, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'c', + count: 22, + }, + ], + format: 'KWkk bbbb cccc cccc cccc cccc cccc cc', + }, + { + country: 'LV', + total: 21, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'c', + count: 13, + }, + ], + format: 'LVkk bbbb cccc cccc cccc c', + }, + { + country: 'LB', + total: 28, + bban: [ + { + type: 'n', + count: 4, + }, + { + type: 'c', + count: 20, + }, + ], + format: 'LBkk bbbb cccc cccc cccc cccc cccc', + }, + { + country: 'LI', + total: 21, + bban: [ + { + type: 'n', + count: 5, + }, + { + type: 'c', + count: 12, + }, + ], + format: 'LIkk bbbb bccc cccc cccc c', + }, + { + country: 'LT', + total: 20, + bban: [ + { + type: 'n', + count: 5, + }, + { + type: 'n', + count: 11, + }, + ], + format: 'LTkk bbbb bccc cccc cccc', + }, + { + country: 'LU', + total: 20, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'c', + count: 13, + }, + ], + format: 'LUkk bbbc cccc cccc cccc', + }, + { + country: 'MK', + total: 19, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'c', + count: 10, + }, + { + type: 'n', + count: 2, + }, + ], + format: 'MKkk bbbc cccc cccc cxx', + }, + { + country: 'MT', + total: 31, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 5, + }, + { + type: 'c', + count: 18, + }, + ], + format: 'MTkk bbbb ssss sccc cccc cccc cccc ccc', + }, + { + country: 'MR', + total: 27, + bban: [ + { + type: 'n', + count: 10, + }, + { + type: 'n', + count: 13, + }, + ], + format: 'MRkk bbbb bsss sscc cccc cccc cxx', + }, + { + country: 'MU', + total: 30, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 15, + }, + { + type: 'a', + count: 3, + }, + ], + format: 'MUkk bbbb bbss cccc cccc cccc 000d dd', + }, + { + country: 'MC', + total: 27, + bban: [ + { + type: 'n', + count: 10, + }, + { + type: 'c', + count: 11, + }, + { + type: 'n', + count: 2, + }, + ], + format: 'MCkk bbbb bsss sscc cccc cccc cxx', + }, + { + country: 'MD', + total: 24, + bban: [ + { + type: 'c', + count: 2, + }, + { + type: 'c', + count: 18, + }, + ], + format: 'MDkk bbcc cccc cccc cccc cccc', + }, + { + country: 'ME', + total: 22, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'n', + count: 15, + }, + ], + format: 'MEkk bbbc cccc cccc cccc xx', + }, + { + country: 'NL', + total: 18, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'NLkk bbbb cccc cccc cc', + }, + { + country: 'NO', + total: 15, + bban: [ + { + type: 'n', + count: 4, + }, + { + type: 'n', + count: 7, + }, + ], + format: 'NOkk bbbb cccc ccx', + }, + { + country: 'PK', + total: 24, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'PKkk bbbb cccc cccc cccc cccc', + }, + { + country: 'PS', + total: 29, + bban: [ + { + type: 'c', + count: 4, + }, + { + type: 'n', + count: 9, + }, + { + type: 'n', + count: 12, + }, + ], + format: 'PSkk bbbb xxxx xxxx xccc cccc cccc c', + }, + { + country: 'PL', + total: 28, + bban: [ + { + type: 'n', + count: 8, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'PLkk bbbs sssx cccc cccc cccc cccc', + }, + { + country: 'PT', + total: 25, + bban: [ + { + type: 'n', + count: 8, + }, + { + type: 'n', + count: 13, + }, + ], + format: 'PTkk bbbb ssss cccc cccc cccx x', + }, + { + country: 'QA', + total: 29, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'c', + count: 21, + }, + ], + format: 'QAkk bbbb cccc cccc cccc cccc cccc c', + }, + { + country: 'RO', + total: 24, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'c', + count: 16, + }, + ], + format: 'ROkk bbbb cccc cccc cccc cccc', + }, + { + country: 'SM', + total: 27, + bban: [ + { + type: 'a', + count: 1, + }, + { + type: 'n', + count: 10, + }, + { + type: 'c', + count: 12, + }, + ], + format: 'SMkk xaaa aabb bbbc cccc cccc ccc', + }, + { + country: 'SA', + total: 24, + bban: [ + { + type: 'n', + count: 2, + }, + { + type: 'c', + count: 18, + }, + ], + format: 'SAkk bbcc cccc cccc cccc cccc', + }, + { + country: 'RS', + total: 22, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'n', + count: 15, + }, + ], + format: 'RSkk bbbc cccc cccc cccc xx', + }, + { + country: 'SK', + total: 24, + bban: [ + { + type: 'n', + count: 10, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'SKkk bbbb ssss sscc cccc cccc', + }, + { + country: 'SI', + total: 19, + bban: [ + { + type: 'n', + count: 5, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'SIkk bbss sccc cccc cxx', + }, + { + country: 'ES', + total: 24, + bban: [ + { + type: 'n', + count: 10, + }, + { + type: 'n', + count: 10, + }, + ], + format: 'ESkk bbbb gggg xxcc cccc cccc', + }, + { + country: 'SE', + total: 24, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'n', + count: 17, + }, + ], + format: 'SEkk bbbc cccc cccc cccc cccc', + }, + { + country: 'CH', + total: 21, + bban: [ + { + type: 'n', + count: 5, + }, + { + type: 'c', + count: 12, + }, + ], + format: 'CHkk bbbb bccc cccc cccc c', + }, + { + country: 'TN', + total: 24, + bban: [ + { + type: 'n', + count: 5, + }, + { + type: 'n', + count: 15, + }, + ], + format: 'TNkk bbss sccc cccc cccc cccc', + }, + { + country: 'TR', + total: 26, + bban: [ + { + type: 'n', + count: 5, + }, + { + type: 'n', + count: 1, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'TRkk bbbb bxcc cccc cccc cccc cc', + }, + { + country: 'AE', + total: 23, + bban: [ + { + type: 'n', + count: 3, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'AEkk bbbc cccc cccc cccc ccc', + }, + { + country: 'GB', + total: 22, + bban: [ + { + type: 'a', + count: 4, + }, + { + type: 'n', + count: 6, + }, + { + type: 'n', + count: 8, + }, + ], + format: 'GBkk bbbb ssss sscc cccc cc', + }, + { + country: 'VG', + total: 24, + bban: [ + { + type: 'c', + count: 4, + }, + { + type: 'n', + count: 16, + }, + ], + format: 'VGkk bbbb cccc cccc cccc cccc', + }, + ], + iso3166: [ + 'AD', + 'AE', + 'AF', + 'AG', + 'AI', + 'AL', + 'AM', + 'AO', + 'AQ', + 'AR', + 'AS', + 'AT', + 'AU', + 'AW', + 'AX', + 'AZ', + 'BA', + 'BB', + 'BD', + 'BE', + 'BF', + 'BG', + 'BH', + 'BI', + 'BJ', + 'BL', + 'BM', + 'BN', + 'BO', + 'BQ', + 'BR', + 'BS', + 'BT', + 'BV', + 'BW', + 'BY', + 'BZ', + 'CA', + 'CC', + 'CD', + 'CF', + 'CG', + 'CH', + 'CI', + 'CK', + 'CL', + 'CM', + 'CN', + 'CO', + 'CR', + 'CU', + 'CV', + 'CW', + 'CX', + 'CY', + 'CZ', + 'DE', + 'DJ', + 'DK', + 'DM', + 'DO', + 'DZ', + 'EC', + 'EE', + 'EG', + 'EH', + 'ER', + 'ES', + 'ET', + 'FI', + 'FJ', + 'FK', + 'FM', + 'FO', + 'FR', + 'GA', + 'GB', + 'GD', + 'GE', + 'GF', + 'GG', + 'GH', + 'GI', + 'GL', + 'GM', + 'GN', + 'GP', + 'GQ', + 'GR', + 'GS', + 'GT', + 'GU', + 'GW', + 'GY', + 'HK', + 'HM', + 'HN', + 'HR', + 'HT', + 'HU', + 'ID', + 'IE', + 'IL', + 'IM', + 'IN', + 'IO', + 'IQ', + 'IR', + 'IS', + 'IT', + 'JE', + 'JM', + 'JO', + 'JP', + 'KE', + 'KG', + 'KH', + 'KI', + 'KM', + 'KN', + 'KP', + 'KR', + 'KW', + 'KY', + 'KZ', + 'LA', + 'LB', + 'LC', + 'LI', + 'LK', + 'LR', + 'LS', + 'LT', + 'LU', + 'LV', + 'LY', + 'MA', + 'MC', + 'MD', + 'ME', + 'MF', + 'MG', + 'MH', + 'MK', + 'ML', + 'MM', + 'MN', + 'MO', + 'MP', + 'MQ', + 'MR', + 'MS', + 'MT', + 'MU', + 'MV', + 'MW', + 'MX', + 'MY', + 'MZ', + 'NA', + 'NC', + 'NE', + 'NF', + 'NG', + 'NI', + 'NL', + 'NO', + 'NP', + 'NR', + 'NU', + 'NZ', + 'OM', + 'PA', + 'PE', + 'PF', + 'PG', + 'PH', + 'PK', + 'PL', + 'PM', + 'PN', + 'PR', + 'PS', + 'PT', + 'PW', + 'PY', + 'QA', + 'RE', + 'RO', + 'RS', + 'RU', + 'RW', + 'SA', + 'SB', + 'SC', + 'SD', + 'SE', + 'SG', + 'SH', + 'SI', + 'SJ', + 'SK', + 'SL', + 'SM', + 'SN', + 'SO', + 'SR', + 'SS', + 'ST', + 'SV', + 'SX', + 'SY', + 'SZ', + 'TC', + 'TD', + 'TF', + 'TG', + 'TH', + 'TJ', + 'TK', + 'TL', + 'TM', + 'TN', + 'TO', + 'TR', + 'TT', + 'TV', + 'TW', + 'TZ', + 'UA', + 'UG', + 'UM', + 'US', + 'UY', + 'UZ', + 'VA', + 'VC', + 'VE', + 'VG', + 'VI', + 'VN', + 'VU', + 'WF', + 'WS', + 'XK', + 'YE', + 'YT', + 'ZA', + '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; diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts new file mode 100644 index 00000000..a74dceba --- /dev/null +++ b/src/modules/finance/index.ts @@ -0,0 +1,436 @@ +import type { Faker } from '../..'; +import { FakerError } from '../../errors/faker-error'; +import iban from './iban'; + +/** + * Module to generate finance related entries. + */ +export class Finance { + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Finance.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } + + /** + * Generates a random account number. + * + * @param length The length of the account number. Defaults to `8`. + * + * @example + * faker.finance.account() // 92842238 + * faker.finance.account(5) // 32564 + */ + account(length?: number): string { + length = length || 8; + let template = ''; + + for (let i = 0; i < length; i++) { + template += '#'; + } + length = null; + return this.faker.helpers.replaceSymbolWithNumber(template); + } + + /** + * Generates a random account name. + * + * @example + * faker.finance.accountName() // 'Personal Loan Account' + */ + accountName(): string { + return [ + this.faker.helpers.arrayElement( + this.faker.definitions.finance.account_type + ), + 'Account', + ].join(' '); + } + + /** + * Generates a random routing number. + * + * @example + * faker.finance.routingNumber() // '522814402' + */ + routingNumber(): string { + const routingNumber = + this.faker.helpers.replaceSymbolWithNumber('########'); + + // Modules 10 straight summation. + let sum = 0; + + for (let i = 0; i < routingNumber.length; i += 3) { + sum += Number(routingNumber[i]) * 3; + sum += Number(routingNumber[i + 1]) * 7; + sum += Number(routingNumber[i + 2]) || 0; + } + + return `${routingNumber}${Math.ceil(sum / 10) * 10 - sum}`; + } + + /** + * Generates a random masked number. + * + * @param length The length of the unmasked number. Defaults to `4`. + * @param parens Whether to use surrounding parenthesis. Defaults to `true`. + * @param ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`. + * + * @example + * faker.finance.mask() // '(...9711)' + * faker.finance.mask(3) // '(...342)' + * faker.finance.mask(3, false) // '...236' + * faker.finance.mask(3, false, false) // '298' + */ + mask(length?: number, parens?: boolean, ellipsis?: boolean): string { + // set defaults + length = length || 4; + parens = parens == null ? true : parens; + ellipsis = ellipsis == null ? true : ellipsis; + + // create a template for length + let template = ''; + + for (let i = 0; i < length; i++) { + template = `${template}#`; + } + + //prefix with ellipsis + template = ellipsis ? ['...', template].join('') : template; + + template = parens ? ['(', template, ')'].join('') : template; + + //generate random numbers + template = this.faker.helpers.replaceSymbolWithNumber(template); + + return template; + } + + /** + * Generates a random amount between the given bounds (inclusive). + * + * @param min The lower bound for the amount. Defaults to `0`. + * @param max The upper bound for the amount. Defaults to `1000`. + * @param dec The number of decimal places for the amount. Defaults to `2`. + * @param symbol The symbol used to prefix the amount. Defaults to `''`. + * @param autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`. + * + * @example + * faker.finance.amount() // '617.87' + * faker.finance.amount(5, 10) // '5.53' + * faker.finance.amount(5, 10, 0) // '8' + * faker.finance.amount(5, 10, 2, '$') // '$5.85' + * faker.finance.amount(5, 10, 5, '', true) // '9,75067' + */ + amount( + min: number = 0, + max: number = 1000, + dec: number = 2, + symbol: string = '', + autoFormat?: boolean + ): string { + const randValue = this.faker.datatype.number({ + max, + min, + precision: Math.pow(10, -dec), + }); + + let formattedString: string; + if (autoFormat) { + formattedString = randValue.toLocaleString(undefined, { + minimumFractionDigits: dec, + }); + } else { + formattedString = randValue.toFixed(dec); + } + + return symbol + formattedString; + } + + /** + * Returns a random transaction type. + * + * @example + * faker.finance.transactionType() // 'payment' + */ + transactionType(): string { + return this.faker.helpers.arrayElement( + this.faker.definitions.finance.transaction_type + ); + } + + /** + * Returns a random currency code. + * (The short text/abbreviation for the currency (e.g. `US Dollar` -> `USD`)) + * + * @example + * faker.finance.currencyCode() // 'USD' + */ + currencyCode(): string { + return this.faker.helpers.objectValue( + this.faker.definitions.finance.currency + )['code']; + } + + /** + * Returns a random currency name. + * + * @example + * faker.finance.currencyName() // 'US Dollar' + */ + currencyName(): string { + return this.faker.helpers.objectKey( + this.faker.definitions.finance.currency + ) as string; + } + + /** + * Returns a random currency symbol. + * + * @example + * faker.finance.currencySymbol() // '$' + */ + currencySymbol(): string { + let symbol: string; + while (!symbol) { + symbol = this.faker.helpers.objectValue( + this.faker.definitions.finance.currency + )['symbol']; + } + return symbol; + } + + /** + * Generates a random bitcoin address. + * + * @example + * faker.finance.bitcoinAddress() // '3ySdvCkTLVy7gKD4j6JfSaf5d' + */ + bitcoinAddress(): string { + const addressLength = this.faker.datatype.number({ min: 25, max: 34 }); + + let address = this.faker.helpers.arrayElement(['1', '3']); + + for (let i = 0; i < addressLength - 1; i++) + address += this.faker.helpers.arrayElement( + '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split('') + ); + + return address; + } + + /** + * Generates a random litecoin address. + * + * @example + * faker.finance.litecoinAddress() // 'MoQaSTGWBRXkWfyxKbNKuPrAWGELzcW' + */ + litecoinAddress(): string { + const addressLength = this.faker.datatype.number({ min: 26, max: 33 }); + + let address = this.faker.helpers.arrayElement(['L', 'M', '3']); + + for (let i = 0; i < addressLength - 1; i++) + address += this.faker.helpers.arrayElement( + '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split('') + ); + + return address; + } + + /** + * Generates a random credit card number. + * + * @param issuer The name of the issuer (case insensitive) or the format used to generate one. + * + * @example + * faker.finance.creditCardNumber() // '4427163488668' + * faker.finance.creditCardNumber('visa') // '4882664999003' + * faker.finance.creditCardNumber('63[7-9]#-####-####-###L') // '6375-3265-4676-6644' + */ + creditCardNumber(issuer = ''): string { + let format: string; + const localeFormat = this.faker.definitions.finance.credit_card; + const normalizedIssuer = issuer.toLowerCase(); + if (normalizedIssuer in localeFormat) { + format = this.faker.helpers.arrayElement(localeFormat[normalizedIssuer]); + } else if (issuer.match(/#/)) { + // The user chose an optional scheme + format = issuer; + } else { + // Choose a random issuer + // Credit cards are in an object structure + const formats = this.faker.helpers.objectValue(localeFormat); // There could be multiple formats + format = this.faker.helpers.arrayElement(formats); + } + format = format.replace(/\//g, ''); + return this.faker.helpers.replaceCreditCardSymbols(format); + } + + /** + * Generates a random credit card CVV. + * + * @example + * faker.finance.creditCardCVV() // '506' + */ + creditCardCVV(): string { + let cvv = ''; + for (let i = 0; i < 3; i++) { + cvv += this.faker.datatype.number({ max: 9 }).toString(); + } + return cvv; + } + + /** + * Returns a random credit card issuer. + * + * @example + * faker.finance.creditCardIssuer() // 'discover' + */ + creditCardIssuer(): string { + return this.faker.helpers.objectKey( + this.faker.definitions.finance.credit_card + ) as string; + } + + /** + * 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 FakerError('minimum length is 1'); + } + return Array.from({ length }, () => this.faker.datatype.number(9)).join(''); + } + + /** + * Generates a random ethereum Address. + * + * @example + * faker.finance.ethereumAddress() // '0xf03dfeecbafc5147241cc4c4ca20b3c9dfd04c4a' + */ + ethereumAddress(): string { + const address = this.faker.datatype.hexadecimal(40).toLowerCase(); + return address; + } + + /** + * Generates a random iban. + * + * @param formatted Return a formatted version of the generated IBAN. Defaults to `false`. + * @param countryCode The country code from which you want to generate an IBAN, if none is provided a random country will be used. + * @throws Will throw an error if the passed country code is not supported. + * + * @example + * faker.finance.iban() // 'TR736918640040966092800056' + * faker.finance.iban(true) // 'FR20 8008 2330 8984 74S3 Z620 224' + * faker.finance.iban(true, 'DE') // 'DE84 1022 7075 0900 1170 01' + */ + iban(formatted: boolean = false, countryCode?: string): string { + const ibanFormat = countryCode + ? iban.formats.find((f) => f.country === countryCode) + : this.faker.helpers.arrayElement(iban.formats); + + if (!ibanFormat) { + throw new FakerError(`Country code ${countryCode} not supported.`); + } + + let s = ''; + let count = 0; + for (const bban of ibanFormat.bban) { + let c = bban.count; + count += bban.count; + while (c > 0) { + if (bban.type === 'a') { + s += this.faker.helpers.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.helpers.arrayElement(iban.alpha); + } + } else { + if (c >= 3 && this.faker.datatype.number(100) < 30) { + if (this.faker.datatype.boolean()) { + s += this.faker.helpers.arrayElement(iban.pattern100); + c -= 2; + } else { + s += this.faker.helpers.arrayElement(iban.pattern10); + c--; + } + } else { + s += this.faker.datatype.number(9); + } + } + c--; + } + s = s.substring(0, count); + } + let checksum: string | number = + 98 - iban.mod97(iban.toDigitString(`${s}${ibanFormat.country}00`)); + + if (checksum < 10) { + checksum = `0${checksum}`; + } + + const result = `${ibanFormat.country}${checksum}${s}`; + + return formatted ? result.match(/.{1,4}/g).join(' ') : result; + } + + /** + * Generates a random bic. + * + * @example + * faker.finance.bic() // 'WYAUPGX1432' + */ + bic(): string { + const vowels = ['A', 'E', 'I', 'O', 'U']; + const prob = this.faker.datatype.number(100); + + return [ + this.faker.helpers.replaceSymbols('???'), + this.faker.helpers.arrayElement(vowels), + this.faker.helpers.arrayElement(iban.iso3166), + this.faker.helpers.replaceSymbols('?'), + '1', + prob < 10 + ? this.faker.helpers.replaceSymbols( + `?${this.faker.helpers.arrayElement(vowels)}?` + ) + : prob < 40 + ? this.faker.helpers.replaceSymbols('###') + : '', + ].join(''); + } + + /** + * Generates a random transaction description. + * + * @example + * faker.finance.transactionDescription() + * // 'invoice transaction at Kilback - Durgan using card ending with ***(...4316) for UAH 783.82 in account ***16168663' + */ + transactionDescription(): string { + const transaction = this.faker.helpers.createTransaction(); + const account = transaction.account; + const amount = transaction.amount; + const transactionType = transaction.type; + const company = transaction.business; + const card = this.mask(); + const currency = this.currencyCode(); + + return `${transactionType} transaction at ${company} using card ending with ***${card} for ${currency} ${amount} in account ***${account}`; + } +} |
