diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/iban.ts | 1414 | ||||
| -rw-r--r-- | src/utils/mersenne.ts | 320 | ||||
| -rw-r--r-- | src/utils/types.ts | 9 | ||||
| -rw-r--r-- | src/utils/unique.ts | 158 | ||||
| -rw-r--r-- | src/utils/user-agent.ts | 345 |
5 files changed, 9 insertions, 2237 deletions
diff --git a/src/utils/iban.ts b/src/utils/iban.ts deleted file mode 100644 index 9b4780c5..00000000 --- a/src/utils/iban.ts +++ /dev/null @@ -1,1414 +0,0 @@ -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/utils/mersenne.ts b/src/utils/mersenne.ts deleted file mode 100644 index 5859f3ae..00000000 --- a/src/utils/mersenne.ts +++ /dev/null @@ -1,320 +0,0 @@ -/** - * Copyright (c) 2022 Faker - * - * This is a version of the original source code migrated to TypeScript and - * modified by the Faker team. - * - * Check LICENSE for more details on copyright. - * - * ----------------------------------------------------------------------------- - * - * Copyright (c) 2006 Y. Okada - * - * This program is a JavaScript version of Mersenne Twister, with concealment - * and encapsulation in class, an almost straight conversion from the original - * program, mt19937ar.c, translated by Y. Okada on July 17, 2006, and modified - * a little at July 20, 2006, but there are not any substantial differences. - * - * In this program, procedure descriptions and comments of original source code - * were not removed. - * - * Lines commented with //c// were originally descriptions of c procedure. - * And a few following lines are appropriate JavaScript descriptions. - * - * Lines commented with /* and *\/ are original comments. - * Lines commented with // are additional comments in this JavaScript version. - * - * Before using this version, create at least one instance of - * MersenneTwister19937 class, and initialize the each state, given below - * in C comments, of all the instances. - * - * ----------------------------------------------------------------------------- - * - * A C-program for MT19937, with initialization improved 2002/1/26. - * Coded by Takuji Nishimura and Makoto Matsumoto. - * - * Before using, initialize the state by using init_genrand(seed) - * or init_by_array(init_key, key_length). - * - * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. The names of its contributors may not be used to endorse or promote - * products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - * - * Any feedback is very welcome. - * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html - * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) - */ - -export default class MersenneTwister19937 { - private readonly N = 624; - private readonly M = 397; - private readonly MATRIX_A = 0x9908b0df; // constant vector a - private readonly UPPER_MASK = 0x80000000; // most significant w-r bits - private readonly LOWER_MASK = 0x7fffffff; // least significant r bits - private mt: number[] = new Array(this.N); // the array for the state vector - private mti = this.N + 1; // mti==N+1 means mt[N] is not initialized - - /** - * Returns a 32-bits unsigned integer from an operand to which applied a bit - * operator. - * - * @param n1 number to unsign - */ - private unsigned32(n1: number): number { - return n1 < 0 ? (n1 ^ this.UPPER_MASK) + this.UPPER_MASK : n1; - } - - /** - * Emulates lowerflow of a c 32-bits unsigned integer variable, instead of - * the operator -. These both arguments must be non-negative integers - * expressible using unsigned 32 bits. - * - * @param n1 dividend - * @param n2 divisor - */ - private subtraction32(n1: number, n2: number): number { - return n1 < n2 - ? this.unsigned32((0x100000000 - (n2 - n1)) & 0xffffffff) - : n1 - n2; - } - - /** - * Emulates overflow of a c 32-bits unsigned integer variable, instead of the operator +. - * these both arguments must be non-negative integers expressible using unsigned 32 bits. - * - * @param n1 number one for addition - * @param n2 number two for addition - */ - private addition32(n1: number, n2: number): number { - return this.unsigned32((n1 + n2) & 0xffffffff); - } - - /** - * Emulates overflow of a c 32-bits unsigned integer variable, instead of the operator *. - * These both arguments must be non-negative integers expressible using unsigned 32 bits. - * - * @param n1 number one for multiplication - * @param n2 number two for multiplication - */ - private multiplication32(n1: number, n2: number): number { - let sum = 0; - for (let i = 0; i < 32; ++i) { - if ((n1 >>> i) & 0x1) { - sum = this.addition32(sum, this.unsigned32(n2 << i)); - } - } - return sum; - } - - /** - * Initializes mt[N] with a seed. - * - * @param seed the seed to use - */ - initGenrand(seed: number): void { - this.mt[0] = this.unsigned32(seed & 0xffffffff); - for (this.mti = 1; this.mti < this.N; this.mti++) { - this.mt[this.mti] = - //(1812433253 * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); - this.addition32( - this.multiplication32( - 1812433253, - this.unsigned32( - this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30) - ) - ), - this.mti - ); - - /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ - /* In the previous versions, MSBs of the seed affect */ - /* only MSBs of the array mt[]. */ - /* 2002/01/09 modified by Makoto Matsumoto */ - this.mt[this.mti] = this.unsigned32(this.mt[this.mti] & 0xffffffff); - } - } - - /** - * Initialize by an array with array-length. - * - * @param initKey is the array for initializing keys - * @param keyLength is its length - */ - initByArray(initKey: number[], keyLength: number): void { - this.initGenrand(19650218); - let i = 1; - let j = 0; - let k = this.N > keyLength ? this.N : keyLength; - for (; k; k--) { - // mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525)) + init_key[j] + j; - this.mt[i] = this.addition32( - this.addition32( - this.unsigned32( - this.mt[i] ^ - this.multiplication32( - this.unsigned32(this.mt[i - 1] ^ (this.mt[i - 1] >>> 30)), - 1664525 - ) - ), - initKey[j] - ), - j - ); - // mt[i] &= 0xffffffff; for WORDSIZE > 32 machines - this.mt[i] = this.unsigned32(this.mt[i] & 0xffffffff); - i++; - j++; - if (i >= this.N) { - this.mt[0] = this.mt[this.N - 1]; - i = 1; - } - if (j >= keyLength) { - j = 0; - } - } - for (k = this.N - 1; k; k--) { - // mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941)) - i - this.mt[i] = this.subtraction32( - this.unsigned32( - this.mt[i] ^ - this.multiplication32( - this.unsigned32(this.mt[i - 1] ^ (this.mt[i - 1] >>> 30)), - 1566083941 - ) - ), - i - ); - // mt[i] &= 0xffffffff; for WORDSIZE > 32 machines - this.mt[i] = this.unsigned32(this.mt[i] & 0xffffffff); - i++; - if (i >= this.N) { - this.mt[0] = this.mt[this.N - 1]; - i = 1; - } - } - this.mt[0] = 0x80000000; // MSB is 1; assuring non-zero initial array - } - - // moved outside of genrandInt32() by jwatte 2010-11-17; generate less garbage - private mag01 = [0x0, this.MATRIX_A]; - - /** - * Generates a random number on [0,2^32]-interval - */ - genrandInt32(): number { - let y: number; - - if (this.mti >= this.N) { - // generate N words at one time - let kk: number; - - // if initGenrand() has not been called a default initial seed is used - if (this.mti === this.N + 1) { - this.initGenrand(5489); - } - - for (kk = 0; kk < this.N - this.M; kk++) { - y = this.unsigned32( - (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK) - ); - - this.mt[kk] = this.unsigned32( - this.mt[kk + this.M] ^ (y >>> 1) ^ this.mag01[y & 0x1] - ); - } - - for (; kk < this.N - 1; kk++) { - y = this.unsigned32( - (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK) - ); - - this.mt[kk] = this.unsigned32( - this.mt[kk + (this.M - this.N)] ^ (y >>> 1) ^ this.mag01[y & 0x1] - ); - } - - y = this.unsigned32( - (this.mt[this.N - 1] & this.UPPER_MASK) | (this.mt[0] & this.LOWER_MASK) - ); - - this.mt[this.N - 1] = this.unsigned32( - this.mt[this.M - 1] ^ (y >>> 1) ^ this.mag01[y & 0x1] - ); - - this.mti = 0; - } - - y = this.mt[this.mti++]; - - // Tempering - y = this.unsigned32(y ^ (y >>> 11)); - y = this.unsigned32(y ^ ((y << 7) & 0x9d2c5680)); - y = this.unsigned32(y ^ ((y << 15) & 0xefc60000)); - y = this.unsigned32(y ^ (y >>> 18)); - - return y; - } - - /** - * Generates a random number on [0,2^32]-interval - */ - genrandInt31(): number { - return this.genrandInt32() >>> 1; - } - - /** - * Generates a random number on [0,1]-real-interval - */ - genrandReal1(): number { - return this.genrandInt32() * (1.0 / 4294967295.0); // divided by 2^32-1 - } - - /** - * Generates a random number on [0,1)-real-interval - */ - genrandReal2(): number { - return this.genrandInt32() * (1.0 / 4294967296.0); // divided by 2^32 - } - - /** - * Generates a random number on (0,1)-real-interval - */ - genrandReal3(): number { - return (this.genrandInt32() + 0.5) * (1.0 / 4294967296.0); // divided by 2^32 - } - - /** - * Generates a random number on [0,1) with 53-bit resolution - */ - genrandRes53(): number { - const a = this.genrandInt32() >>> 5, - b = this.genrandInt32() >>> 6; - return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0); - } - // These real versions are due to Isaku Wada, 2002/01/09 -} diff --git a/src/utils/types.ts b/src/utils/types.ts index 705d02d1..65c87ebf 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1,4 +1,13 @@ /** + * Type that provides auto-suggestions but also any string. + * + * @see https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609 + */ +export type LiteralUnion<T extends U, U = string> = + | T + | (U & { zz_IGNORE_ME?: never }); + +/** * Type that represents a single method/function name of the given type. */ export type MethodOf< diff --git a/src/utils/unique.ts b/src/utils/unique.ts deleted file mode 100644 index df1b827e..00000000 --- a/src/utils/unique.ts +++ /dev/null @@ -1,158 +0,0 @@ -import { FakerError } from '../errors/faker-error'; - -export type RecordKey = string | number | symbol; - -/** - * Global store of unique values. - * This means that faker should *never* return duplicate values across all API methods when using `Faker.unique` without passing `options.store`. - */ -const GLOBAL_UNIQUE_STORE: Record<RecordKey, RecordKey> = {}; - -/** - * Global exclude list of results. - * Defaults to nothing excluded. - */ -const GLOBAL_UNIQUE_EXCLUDE: RecordKey[] = []; - -/** - * Uniqueness compare function. - * Default behavior is to check value as key against object hash. - * - * @param obj The object to check. - * @param key The key to check. - */ -function defaultCompare( - obj: Record<RecordKey, RecordKey>, - key: RecordKey -): 0 | -1 { - if (obj[key] === undefined) { - return -1; - } - return 0; -} - -/** - * Logs the given code as an error and throws it. - * Also logs a message for helping the user. - * - * @param startTime The time the execution started. - * @param now The current time. - * @param code The error code. - * @param store The store of unique entries. - * @param currentIterations Current iteration or retries of `unique.exec` (current loop depth). - * - * @throws The given error code with additional text. - */ -function errorMessage( - startTime: number, - now: number, - code: string, - store: Record<RecordKey, RecordKey>, - currentIterations: number -): never { - console.error('Error', code); - console.log( - `Found ${Object.keys(store).length} unique entries before throwing error. -retried: ${currentIterations} -total time: ${now - startTime}ms` - ); - throw new FakerError( - `${code} for uniqueness check. - -May not be able to generate any more unique values with current settings. -Try adjusting maxTime or maxRetries parameters for faker.unique().` - ); -} - -/** - * Generates a unique result using the results of the given method. - * Used unique entries will be stored internally and filtered from subsequent calls. - * - * @template Method The type of the method to execute. - * @param method The method used to generate the values. - * @param args The arguments used to call the method. - * @param options The optional options used to configure this method. - * @param options.startTime The time this execution stared. Defaults to `new Date().getTime()`. - * @param options.maxTime The time in milliseconds this method may take before throwing an error. Defaults to `50`. - * @param options.maxRetries The total number of attempts to try before throwing an error. Defaults to `50`. - * @param options.currentIterations The current attempt. Defaults to `0`. - * @param options.exclude The value or values that should be excluded/skipped. Defaults to `[]`. - * @param options.compare The function used to determine whether a value was already returned. Defaults to check the existence of the key. - * @param options.store The store of unique entries. Defaults to `GLOBAL_UNIQUE_STORE`. - */ -export function exec<Method extends (...parameters) => RecordKey>( - method: Method, - args: Parameters<Method>, - options: { - startTime?: number; - maxTime?: number; - maxRetries?: number; - currentIterations?: number; - exclude?: RecordKey | RecordKey[]; - compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1; - store?: Record<RecordKey, RecordKey>; - } = {} -): ReturnType<Method> { - const now = new Date().getTime(); - - const { - startTime = new Date().getTime(), - maxTime = 50, - maxRetries = 50, - compare = defaultCompare, - store = GLOBAL_UNIQUE_STORE, - } = options; - let { exclude = GLOBAL_UNIQUE_EXCLUDE } = options; - options.currentIterations = options.currentIterations ?? 0; - - // Support single exclude argument as string - if (!Array.isArray(exclude)) { - exclude = [exclude]; - } - - // if (options.currentIterations > 0) { - // console.log('iterating', options.currentIterations) - // } - - // console.log(now - startTime) - if (now - startTime >= maxTime) { - return errorMessage( - startTime, - now, - `Exceeded maxTime: ${maxTime}`, - store, - options.currentIterations - ); - } - - if (options.currentIterations >= maxRetries) { - return errorMessage( - startTime, - now, - `Exceeded maxRetries: ${maxRetries}`, - store, - options.currentIterations - ); - } - - // Execute the provided method to find a potential satisfied value. - const result: ReturnType<Method> = method.apply(this, args); - - // If the result has not been previously found, add it to the found array and return the value as it's unique. - if (compare(store, result) === -1 && exclude.indexOf(result) === -1) { - store[result] = result; - options.currentIterations = 0; - return result; - } else { - // console.log('conflict', result); - options.currentIterations++; - return exec(method, args, { - ...options, - startTime, - maxTime, - maxRetries, - compare, - exclude, - }); - } -} diff --git a/src/utils/user-agent.ts b/src/utils/user-agent.ts deleted file mode 100644 index b3438cbd..00000000 --- a/src/utils/user-agent.ts +++ /dev/null @@ -1,345 +0,0 @@ -/** - * Copyright (c) 2022 Faker - * - * This is a version of the original code migrated to TypeScript and modified - * by the Faker team. - * - * Check LICENSE for more details about the copyright. - * - * ----------------------------------------------------------------------------- - * - * Copyright (c) 2012-2014 Jeffrey Mealo - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * ----------------------------------------------------------------------------- - * - * Based loosely on Luka Pusic's PHP Script: - * http://360percents.com/posts/php-random-user-agent-generator/ - * - * The license for that script is as follows: - * - * "THE BEER-WARE LICENSE" (Revision 42): - * - * <[email protected]> wrote this file. As long as you retain this notice you - * can do whatever you want with this stuff. If we meet some day, and you think - * this stuff is worth it, you can buy me a beer in return. Luka Pusic - */ - -import type { Faker } from '..'; - -export type Arch = 'lin' | 'mac' | 'win'; - -export function generate(faker: Faker): string { - function rnd( - a?: string[] | number | Record<string, number>, - b?: number - ): string | number { - //calling rnd() with no arguments is identical to rnd(0, 100) - a = a || 0; - b = b || 100; - - if (typeof b === 'number' && typeof a === 'number') { - // 9/2018 - Added faker random to ensure mersenne and seed - return faker.datatype.number({ min: a, max: b }); - } - - if (Array.isArray(a)) { - //returns a random element from array (a), even weighting - return faker.helpers.arrayElement(a); - } - - if (a && typeof a === 'object') { - //returns a random key from the passed object; keys are weighted by the decimal probability in their value - return ((obj) => { - const rand = (rnd(0, 100) as number) / 100; - let min = 0; - let max = 0; - let return_val: string; - - for (const key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - max = obj[key] + min; - return_val = key; - if (rand >= min && rand <= max) { - break; - } - min = min + obj[key]; - } - } - - return return_val; - })(a); - } - - throw new TypeError( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `Invalid arguments passed to rnd. (${b ? `${a}, ${b}` : a})` - ); - } - - function randomLang(): string | number { - return rnd([ - 'AB', - 'AF', - 'AN', - 'AR', - 'AS', - 'AZ', - 'BE', - 'BG', - 'BN', - 'BO', - 'BR', - 'BS', - 'CA', - 'CE', - 'CO', - 'CS', - 'CU', - 'CY', - 'DA', - 'DE', - 'EL', - 'EN', - 'EO', - 'ES', - 'ET', - 'EU', - 'FA', - 'FI', - 'FJ', - 'FO', - 'FR', - 'FY', - 'GA', - 'GD', - 'GL', - 'GV', - 'HE', - 'HI', - 'HR', - 'HT', - 'HU', - 'HY', - 'ID', - 'IS', - 'IT', - 'JA', - 'JV', - 'KA', - 'KG', - 'KO', - 'KU', - 'KW', - 'KY', - 'LA', - 'LB', - 'LI', - 'LN', - 'LT', - 'LV', - 'MG', - 'MK', - 'MN', - 'MO', - 'MS', - 'MT', - 'MY', - 'NB', - 'NE', - 'NL', - 'NN', - 'NO', - 'OC', - 'PL', - 'PT', - 'RM', - 'RO', - 'RU', - 'SC', - 'SE', - 'SK', - 'SL', - 'SO', - 'SQ', - 'SR', - 'SV', - 'SW', - 'TK', - 'TR', - 'TY', - 'UK', - 'UR', - 'UZ', - 'VI', - 'VO', - 'YI', - 'ZH', - ]); - } - - function randomBrowserAndOS(): Array<string | number> { - const browser = rnd({ - chrome: 0.45132810566, - iexplorer: 0.27477061836, - firefox: 0.19384170608, - safari: 0.06186781118, - opera: 0.01574236955, - }); - const os = { - chrome: { win: 0.89, mac: 0.09, lin: 0.02 }, - firefox: { win: 0.83, mac: 0.16, lin: 0.01 }, - opera: { win: 0.91, mac: 0.03, lin: 0.06 }, - safari: { win: 0.04, mac: 0.96 }, - iexplorer: ['win'], - }; - - return [browser, rnd(os[browser])]; - } - - function randomProc(arch: Arch): string | number { - const procs = { - lin: ['i686', 'x86_64'], - mac: { Intel: 0.48, PPC: 0.01, 'U; Intel': 0.48, 'U; PPC': 0.01 }, - win: ['', 'WOW64', 'Win64; x64'], - }; - return rnd(procs[arch]); - } - - function randomRevision(dots: number): string { - let return_val = ''; - //generate a random revision - //dots = 2 returns .x.y where x & y are between 0 and 9 - for (let x = 0; x < dots; x++) { - return_val += `.${rnd(0, 9)}`; - } - return return_val; - } - - const version_string = { - net() { - return [rnd(1, 4), rnd(0, 9), rnd(10000, 99999), rnd(0, 9)].join('.'); - }, - nt() { - return `${rnd(5, 6)}.${rnd(0, 3)}`; - }, - ie() { - return rnd(7, 11); - }, - trident() { - return `${rnd(3, 7)}.${rnd(0, 1)}`; - }, - osx(delim?: string) { - return [10, rnd(5, 10), rnd(0, 9)].join(delim || '.'); - }, - chrome() { - return [rnd(13, 39), 0, rnd(800, 899), 0].join('.'); - }, - presto() { - return `2.9.${rnd(160, 190)}`; - }, - presto2() { - return `${rnd(10, 12)}.00`; - }, - safari() { - return `${rnd(531, 538)}.${rnd(0, 2)}.${rnd(0, 2)}`; - }, - }; - - const browser = { - firefox(arch: Arch): string { - //https://developer.mozilla.org/en-US/docs/Gecko_user_agent_string_reference - const firefox_ver = `${rnd(5, 15)}${randomRevision(2)}`, - gecko_ver = `Gecko/20100101 Firefox/${firefox_ver}`, - proc = randomProc(arch), - os_ver = - arch === 'win' - ? `(Windows NT ${version_string.nt()}${proc ? `; ${proc}` : ''}` - : arch === 'mac' - ? `(Macintosh; ${proc} Mac OS X ${version_string.osx()}` - : `(X11; Linux ${proc}`; - - return `Mozilla/5.0 ${os_ver}; rv:${firefox_ver.slice( - 0, - -2 - )}) ${gecko_ver}`; - }, - - iexplorer(): string { - const ver = version_string.ie(); - - if (ver >= 11) { - //http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx - return `Mozilla/5.0 (Windows NT 6.${rnd(1, 3)}; Trident/7.0; ${rnd([ - 'Touch; ', - '', - ])}rv:11.0) like Gecko`; - } - - //http://msdn.microsoft.com/en-us/library/ie/ms537503(v=vs.85).aspx - return `Mozilla/5.0 (compatible; MSIE ${ver}.0; Windows NT ${version_string.nt()}; Trident/${version_string.trident()}${ - rnd(0, 1) === 1 ? `; .NET CLR ${version_string.net()}` : '' - })`; - }, - - opera(arch: Arch): string { - //http://www.opera.com/docs/history/ - const presto_ver = ` Presto/${version_string.presto()} Version/${version_string.presto2()})`, - os_ver = - arch === 'win' - ? `(Windows NT ${version_string.nt()}; U; ${randomLang()}${presto_ver}` - : arch === 'lin' - ? `(X11; Linux ${randomProc(arch)}; U; ${randomLang()}${presto_ver}` - : `(Macintosh; Intel Mac OS X ${version_string.osx()} U; ${randomLang()} Presto/${version_string.presto()} Version/${version_string.presto2()})`; - - return `Opera/${rnd(9, 14)}.${rnd(0, 99)} ${os_ver}`; - }, - - safari(arch: Arch): string { - const safari = version_string.safari(), - ver = `${rnd(4, 7)}.${rnd(0, 1)}.${rnd(0, 10)}`, - os_ver = - arch === 'mac' - ? `(Macintosh; ${randomProc('mac')} Mac OS X ${version_string.osx( - '_' - )} rv:${rnd(2, 6)}.0; ${randomLang()}) ` - : `(Windows; U; Windows NT ${version_string.nt()})`; - - return `Mozilla/5.0 ${os_ver}AppleWebKit/${safari} (KHTML, like Gecko) Version/${ver} Safari/${safari}`; - }, - - chrome(arch: Arch): string { - const safari = version_string.safari(), - os_ver = - arch === 'mac' - ? `(Macintosh; ${randomProc('mac')} Mac OS X ${version_string.osx( - '_' - )}) ` - : arch === 'win' - ? `(Windows; U; Windows NT ${version_string.nt()})` - : `(X11; Linux ${randomProc(arch)}`; - - return `Mozilla/5.0 ${os_ver} AppleWebKit/${safari} (KHTML, like Gecko) Chrome/${version_string.chrome()} Safari/${safari}`; - }, - }; - - const random = randomBrowserAndOS(); - return browser[random[0]](random[1]); -} |
