-
-
Notifications
You must be signed in to change notification settings - Fork 939
/
Copy pathfinance.ts
446 lines (406 loc) · 12.5 KB
/
finance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import type { Faker } from '.';
import type { Helpers } from './helpers';
import ibanLib from './iban';
/**
* Module to generate finance related entries.
*/
export class Finance {
readonly ibanLib = ibanLib;
readonly Helpers: Helpers;
constructor(private readonly faker: Faker) {
this.Helpers = this.faker.helpers;
// 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.Helpers.replaceSymbolWithNumber(template);
}
/**
* Generates a random account name.
*
* @example
* faker.finance.accountName() // 'Personal Loan Account'
*/
accountName(): string {
return [
this.Helpers.randomize(this.faker.definitions.finance.account_type),
'Account',
].join(' ');
}
/**
* Generates a random routing number.
*
* @example
* faker.finance.routingNumber() // '522814402'
*/
routingNumber(): string {
const routingNumber = this.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 == 0 || !length || typeof length == 'undefined' ? 4 : length;
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.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.Helpers.randomize(
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.random.objectElement(
this.faker.definitions.finance.currency
)['code'];
}
/**
* Returns a random currency name.
*
* @example
* faker.finance.currencyName() // 'US Dollar'
*/
currencyName(): string {
return this.faker.random.objectElement(
this.faker.definitions.finance.currency,
'key'
);
}
/**
* Returns a random currency symbol.
*
* @example
* faker.finance.currencySymbol() // '$'
*/
currencySymbol(): string {
let symbol: string;
while (!symbol) {
symbol = this.faker.random.objectElement(
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.random.arrayElement(['1', '3']);
for (let i = 0; i < addressLength - 1; i++)
address += this.faker.random.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.random.arrayElement(['L', 'M', '3']);
for (let i = 0; i < addressLength - 1; i++)
address += this.faker.random.arrayElement(
'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split('')
);
return address;
}
/**
* Generates a random credit card number.
*
* @param provider The (lowercase) name of the provider 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(provider = ''): string {
let format: string;
let formats: string | string[];
const localeFormat = this.faker.definitions.finance.credit_card;
if (provider in localeFormat) {
formats = localeFormat[provider]; // there could be multiple formats
if (typeof formats === 'string') {
format = formats;
} else {
format = this.faker.random.arrayElement(formats);
}
} else if (provider.match(/#/)) {
// The user chose an optional scheme
format = provider;
} else {
// Choose a random provider
// TODO ST-DDT 2022-01-30: #375 This is impossible to access
if (typeof localeFormat === 'string') {
format = localeFormat;
} else if (typeof localeFormat === 'object') {
// Credit cards are in a object structure
formats = this.faker.random.objectElement(localeFormat, 'value'); // There could be multiple formats
if (typeof formats === 'string') {
format = formats;
} else {
format = this.faker.random.arrayElement(formats);
}
}
}
format = format.replace(/\//g, '');
return this.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;
}
/**
* 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 {
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);
}
if (!ibanFormat) {
throw new Error('Country code ' + countryCode + ' not supported.');
}
let s = '';
let count = 0;
for (let b = 0; b < ibanFormat.bban.length; b++) {
const bban = ibanFormat.bban[b];
let c = bban.count;
count += bban.count;
while (c > 0) {
if (bban.type == 'a') {
s += this.faker.random.arrayElement(this.ibanLib.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);
}
} else {
if (c >= 3 && this.faker.datatype.number(100) < 30) {
if (this.faker.datatype.boolean()) {
s += this.faker.random.arrayElement(this.ibanLib.pattern100);
c -= 2;
} else {
s += this.faker.random.arrayElement(this.ibanLib.pattern10);
c--;
}
} else {
s += this.faker.datatype.number(9);
}
}
c--;
}
s = s.substring(0, count);
}
let checksum: string | number =
98 -
this.ibanLib.mod97(
this.ibanLib.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;
}
/**
* 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.Helpers.replaceSymbols('???') +
this.faker.random.arrayElement(vowels) +
this.faker.random.arrayElement(this.ibanLib.iso3166) +
this.Helpers.replaceSymbols('?') +
'1' +
(prob < 10
? this.Helpers.replaceSymbols(
'?' + this.faker.random.arrayElement(vowels) + '?'
)
: prob < 40
? this.Helpers.replaceSymbols('###')
: '')
);
}
/**
* 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.Helpers.createTransaction();
const account = transaction.account;
const amount = transaction.amount;
const transactionType = transaction.type;
const company = transaction.business;
const card = this.faker.finance.mask();
const currency = this.faker.finance.currencyCode();
return (
transactionType +
' transaction at ' +
company +
' using card ending with ***' +
card +
' for ' +
currency +
' ' +
amount +
' in account ***' +
account
);
}
}