-
-
Notifications
You must be signed in to change notification settings - Fork 953
/
Copy pathindex.ts
1018 lines (955 loc) · 28.9 KB
/
index.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../../internal/module-base';
import type { BitcoinAddressFamilyType, BitcoinNetworkType } from './bitcoin';
import {
BitcoinAddressFamily,
BitcoinAddressSpecs,
BitcoinNetwork,
} from './bitcoin';
import iban from './iban';
/**
* The possible definitions related to currency entries.
*/
export interface Currency {
/**
* The full name for the currency (e.g. `US Dollar`).
*/
name: string;
/**
* The code/short text/abbreviation for the currency (e.g. `USD`).
*/
code: string;
/**
* The symbol for the currency (e.g. `$`).
*/
symbol: string;
/**
* The ISO 4217 numeric code for the currency (e.g. `840`).
*/
numericCode: string;
}
/**
* Puts a space after every 4 characters.
*
* @internal
*
* @param iban The iban to pretty print.
*/
export function prettyPrintIban(iban: string): string {
let pretty = '';
for (let i = 0; i < iban.length; i += 4) {
pretty += `${iban.substring(i, i + 4)} `;
}
return pretty.trimEnd();
}
/**
* Module to generate finance and money related entries.
*
* ### Overview
*
* For a random amount, use [`amount()`](https://fakerjs.dev/api/finance.html#amount).
*
* For traditional bank accounts, use: [`accountNumber()`](https://fakerjs.dev/api/finance.html#accountnumber), [`accountName()`](https://fakerjs.dev/api/finance.html#accountname), [`bic()`](https://fakerjs.dev/api/finance.html#bic), [`iban()`](https://fakerjs.dev/api/finance.html#iban), [`pin()`](https://fakerjs.dev/api/finance.html#pin) and [`routingNumber()`](https://fakerjs.dev/api/finance.html#routingnumber).
*
* For credit card related methods, use: [`creditCardNumber()`](https://fakerjs.dev/api/finance.html#creditcardnumber), [`creditCardCVV()`](https://fakerjs.dev/api/finance.html#creditcardcvv), [`creditCardIssuer()`](https://fakerjs.dev/api/finance.html#creditcardissuer), [`transactionDescription()`](https://fakerjs.dev/api/finance.html#transactiondescription) and [`transactionType()`](https://fakerjs.dev/api/finance.html#transactiontype).
*
* For blockchain related methods, use: [`bitcoinAddress()`](https://fakerjs.dev/api/finance.html#bitcoinaddress), [`ethereumAddress()`](https://fakerjs.dev/api/finance.html#ethereumaddress) and [`litecoinAddress()`](https://fakerjs.dev/api/finance.html#litecoinaddress).
*/
export class FinanceModule extends ModuleBase {
/**
* Generates a random account number.
*
* @param length The length of the account number. Defaults to `8`.
*
* @see faker.string.numeric(): For generating the number with greater control.
*
* @example
* faker.finance.accountNumber() // '92842238'
* faker.finance.accountNumber(5) // '32564'
*
* @since 8.0.0
*/
accountNumber(length?: number): string;
/**
* Generates a random account number.
*
* @param options An options object.
* @param options.length The length of the account number. Defaults to `8`.
*
* @see faker.string.numeric(): For generating the number with greater control.
*
* @example
* faker.finance.accountNumber() // '92842238'
* faker.finance.accountNumber({ length: 5 }) // '32564'
*
* @since 8.0.0
*/
accountNumber(options?: {
/**
* The length of the account number.
*
* @default 8
*/
length?: number;
}): string;
/**
* Generates a random account number.
*
* @param optionsOrLength An options object or the length of the account number.
* @param optionsOrLength.length The length of the account number. Defaults to `8`.
*
* @see faker.string.numeric(): For generating the number with greater control.
*
* @example
* faker.finance.accountNumber() // '92842238'
* faker.finance.accountNumber(5) // '28736'
* faker.finance.accountNumber({ length: 5 }) // '32564'
*
* @since 8.0.0
*/
accountNumber(
optionsOrLength?:
| number
| {
/**
* The length of the account number.
*
* @default 8
*/
length?: number;
}
): string;
/**
* Generates a random account number.
*
* @param options An options object or the length of the account number.
* @param options.length The length of the account number. Defaults to `8`.
*
* @see faker.string.numeric(): For generating the number with greater control.
*
* @example
* faker.finance.accountNumber() // '92842238'
* faker.finance.accountNumber(5) // '28736'
* faker.finance.accountNumber({ length: 5 }) // '32564'
*
* @since 8.0.0
*/
accountNumber(
options:
| number
| {
/**
* The length of the account number.
*
* @default 8
*/
length?: number;
} = {}
): string {
if (typeof options === 'number') {
options = { length: options };
}
const { length = 8 } = options;
return this.faker.string.numeric({ length, allowLeadingZeros: true });
}
/**
* Generates a random account name.
*
* @example
* faker.finance.accountName() // 'Personal Loan Account'
*
* @since 2.0.1
*/
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'
*
* @since 5.0.0
*/
routingNumber(): string {
const routingNumber = this.faker.string.numeric({
length: 8,
allowLeadingZeros: true,
});
// 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`.
*
* @example
* faker.finance.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber(3) // '(...342)'
*
* @since 8.0.0
*
* @deprecated Use `faker.finance.iban().replace(/(?<=.{4})\w(?=.{2})/g, '*')` or a similar approach instead.
*/
maskedNumber(length?: number): string;
/**
* Generates a random masked number.
*
* @param options An options object.
* @param options.length The length of the unmasked number. Defaults to `4`.
* @param options.parens Whether to use surrounding parenthesis. Defaults to `true`.
* @param options.ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
*
* @example
* faker.finance.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber({ length: 3 }) // '(...342)'
* faker.finance.maskedNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskedNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 8.0.0
*
* @deprecated Use `faker.finance.iban().replace(/(?<=.{4})\w(?=.{2})/g, '*')` or a similar approach instead.
*/
maskedNumber(options?: {
/**
* The length of the unmasked number.
*
* @default 4
*/
length?: number;
/**
* Whether to use surrounding parenthesis.
*
* @default true
*/
parens?: boolean;
/**
* Whether to prefix the numbers with an ellipsis.
*
* @default true
*/
ellipsis?: boolean;
}): string;
/**
* Generates a random masked number.
*
* @param optionsOrLength An options object or the length of the unmask number.
* @param optionsOrLength.length The length of the unmasked number. Defaults to `4`.
* @param optionsOrLength.parens Whether to use surrounding parenthesis. Defaults to `true`.
* @param optionsOrLength.ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
*
* @example
* faker.finance.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber(3) // '(...342)'
* faker.finance.maskedNumber({ length: 3 }) // '(...342)'
* faker.finance.maskedNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskedNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 8.0.0
*
* @deprecated Use `faker.finance.iban().replace(/(?<=.{4})\w(?=.{2})/g, '*')` or a similar approach instead.
*/
maskedNumber(
optionsOrLength?:
| number
| {
/**
* The length of the unmasked number.
*
* @default 4
*/
length?: number;
/**
* Whether to use surrounding parenthesis.
*
* @default true
*/
parens?: boolean;
/**
* Whether to prefix the numbers with an ellipsis.
*
* @default true
*/
ellipsis?: boolean;
}
): string;
/**
* Generates a random masked number.
*
* @param options An options object.
* @param options.length The length of the unmasked number. Defaults to `4`.
* @param options.parens Whether to use surrounding parenthesis. Defaults to `true`.
* @param options.ellipsis Whether to prefix the numbers with an ellipsis. Defaults to `true`.
*
* @example
* faker.finance.maskedNumber() // '(...9711)'
* faker.finance.maskedNumber(3) // '(...342)'
* faker.finance.maskedNumber({ length: 3 }) // '(...342)'
* faker.finance.maskedNumber({ length: 3, parens: false }) // '...236'
* faker.finance.maskedNumber({ length: 3, parens: false, ellipsis: false }) // '298'
*
* @since 8.0.0
*
* @deprecated Use `faker.finance.iban().replace(/(?<=.{4})\w(?=.{2})/g, '*')` or a similar approach instead.
*/
maskedNumber(
options:
| number
| {
/**
* The length of the unmasked number.
*
* @default 4
*/
length?: number;
/**
* Whether to use surrounding parenthesis.
*
* @default true
*/
parens?: boolean;
/**
* Whether to prefix the numbers with an ellipsis.
*
* @default true
*/
ellipsis?: boolean;
} = {}
): string {
deprecated({
deprecated: 'faker.finance.maskedNumber()',
proposed:
"faker.finance.iban().replace(/(?<=.{4})\\w(?=.{2})/g, '*') or a similar approach",
since: '9.3.0',
until: '10.0.0',
});
if (typeof options === 'number') {
options = { length: options };
}
const { ellipsis = true, length = 4, parens = true } = options;
let template = this.faker.string.numeric({ length });
if (ellipsis) {
template = `...${template}`;
}
if (parens) {
template = `(${template})`;
}
return template;
}
/**
* Generates a random amount between the given bounds (inclusive).
*
* @param options An options object.
* @param options.min The lower bound for the amount. Defaults to `0`.
* @param options.max The upper bound for the amount. Defaults to `1000`.
* @param options.dec The number of decimal places for the amount. Defaults to `2`.
* @param options.symbol The symbol used to prefix the amount. Defaults to `''`.
* @param options.autoFormat If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
*
* @see faker.number.float(): For generating the amount with greater control.
*
* @example
* faker.finance.amount() // '617.87'
* faker.finance.amount({ min: 5, max: 10 }) // '5.53'
* faker.finance.amount({ min: 5, max: 10, dec: 0 }) // '8'
* faker.finance.amount({ min: 5, max: 10, dec: 2, symbol: '$' }) // '$5.85'
* faker.finance.amount({ min: 5, max: 10, dec: 5, symbol: '', autoFormat: true }) // '9,75067'
*
* @since 2.0.1
*/
amount(
options: {
/**
* The lower bound for the amount.
*
* @default 0
*/
min?: number;
/**
* The upper bound for the amount.
*
* @default 1000
*/
max?: number;
/**
* The number of decimal places for the amount.
*
* @default 2
*/
dec?: number;
/**
* The symbol used to prefix the amount.
*
* @default ''
*/
symbol?: string;
/**
* If true this method will use `Number.toLocaleString()`. Otherwise it will use `Number.toFixed()`.
*
* @default false
*/
autoFormat?: boolean;
} = {}
): string {
const {
autoFormat = false,
dec = 2,
max = 1000,
min = 0,
symbol = '',
} = options;
const randValue = this.faker.number.float({
max,
min,
fractionDigits: dec,
});
const formattedString = autoFormat
? randValue.toLocaleString(undefined, { minimumFractionDigits: dec })
: randValue.toFixed(dec);
return symbol + formattedString;
}
/**
* Returns a random transaction type.
*
* @example
* faker.finance.transactionType() // 'payment'
*
* @since 2.0.1
*/
transactionType(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.finance.transaction_type
);
}
/**
* Returns a random currency object, containing `code`, `name`, `symbol`, and `numericCode` properties.
*
* @see faker.finance.currencyCode(): For generating specifically the currency code.
* @see faker.finance.currencyName(): For generating specifically the currency name.
* @see faker.finance.currencySymbol(): For generating specifically the currency symbol.
* @see faker.finance.currencyNumericCode(): For generating specifically the currency numeric code.
*
* @example
* faker.finance.currency() // { code: 'USD', name: 'US Dollar', symbol: '$', numericCode: '840' }
*
* @since 8.0.0
*/
currency(): Currency {
return this.faker.helpers.arrayElement(
this.faker.definitions.finance.currency
);
}
/**
* Returns a random currency code.
* (The short text/abbreviation for the currency (e.g. `US Dollar` -> `USD`))
*
* @example
* faker.finance.currencyCode() // 'USD'
*
* @since 2.0.1
*/
currencyCode(): string {
return this.currency().code;
}
/**
* Returns a random currency name.
*
* @example
* faker.finance.currencyName() // 'US Dollar'
*
* @since 2.0.1
*/
currencyName(): string {
return this.currency().name;
}
/**
* Returns a random currency symbol.
*
* @example
* faker.finance.currencySymbol() // '$'
*
* @since 2.0.1
*/
currencySymbol(): string {
let symbol: string;
do {
symbol = this.currency().symbol;
} while (symbol.length === 0);
return symbol;
}
/**
* Returns a random currency numeric code.
* (The ISO 4217 numerical code for a currency (e.g. `US Dollar` -> `840` ))
*
* @example
* faker.finance.currencyNumericCode() // '840'
*
* @since 9.6.0
*/
currencyNumericCode(): string {
return this.currency().numericCode;
}
/**
* Generates a random Bitcoin address.
*
* @param options An optional options object.
* @param options.type The bitcoin address type (`'legacy'`, `'sewgit'`, `'bech32'` or `'taproot'`). Defaults to a random address type.
* @param options.network The bitcoin network (`'mainnet'` or `'testnet'`). Defaults to `'mainnet'`.
*
* @example
* faker.finance.bitcoinAddress() // '1TeZEFLmGPLEQrSRdAcnZLoWwYeiHwmRog'
* faker.finance.bitcoinAddress({ type: 'bech32' }) // 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'
* faker.finance.bitcoinAddress({ type: 'bech32', network: 'testnet' }) // 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx'
*
* @since 3.1.0
*/
bitcoinAddress(
options: {
/**
* The bitcoin address type (`'legacy'`, `'sewgit'`, `'bech32'` or `'taproot'`).
*
* @default faker.helpers.arrayElement(['legacy','sewgit','bech32','taproot'])
*/
type?: BitcoinAddressFamilyType;
/**
* The bitcoin network (`'mainnet'` or `'testnet'`).
*
* @default 'mainnet'
*/
network?: BitcoinNetworkType;
} = {}
): string {
const {
type = this.faker.helpers.enumValue(BitcoinAddressFamily),
network = BitcoinNetwork.Mainnet,
} = options;
const addressSpec = BitcoinAddressSpecs[type];
const addressPrefix = addressSpec.prefix[network];
const addressLength = this.faker.number.int(addressSpec.length);
const address = this.faker.string.alphanumeric({
length: addressLength - addressPrefix.length,
casing: addressSpec.casing,
exclude: addressSpec.exclude,
});
return addressPrefix + address;
}
/**
* Generates a random Litecoin address.
*
* @example
* faker.finance.litecoinAddress() // 'MoQaSTGWBRXkWfyxKbNKuPrAWGELzcW'
*
* @since 5.0.0
*/
litecoinAddress(): string {
const addressLength = this.faker.number.int({ min: 26, max: 33 });
const address =
this.faker.string.fromCharacters('LM3') +
this.faker.string.fromCharacters(
'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
addressLength - 1
);
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() // '4427163488662'
* faker.finance.creditCardNumber('visa') // '4882664999007'
* faker.finance.creditCardNumber('63[7-9]#-####-####-###L') // '6375-3265-4676-6646'
*
* @since 5.0.0
*/
creditCardNumber(issuer?: string): string;
/**
* Generates a random credit card number.
*
* @param options An options object.
* @param options.issuer The name of the issuer (case-insensitive) or the format used to generate one. Defaults to `''`.
*
* @example
* faker.finance.creditCardNumber() // '4427163488662'
* faker.finance.creditCardNumber({ issuer: 'visa' }) // '4882664999007'
* faker.finance.creditCardNumber({ issuer: '63[7-9]#-####-####-###L' }) // '6375-3265-4676-6646'
*
* @since 5.0.0
*/
creditCardNumber(options?: {
/**
* The name of the issuer (case-insensitive) or the format used to generate one.
*
* @default ''
*/
issuer?: string;
}): string;
/**
* Generates a random credit card number.
*
* @param options An options object, the issuer or a custom format.
* @param options.issuer The name of the issuer (case-insensitive) or the format used to generate one. Defaults to `''`.
*
* @example
* faker.finance.creditCardNumber() // '4427163488662'
* faker.finance.creditCardNumber({ issuer: 'visa' }) // '4882664999007'
* faker.finance.creditCardNumber({ issuer: '63[7-9]#-####-####-###L' }) // '6375-3265-4676-6646'
* faker.finance.creditCardNumber('visa') // '1226423499765'
*
* @since 5.0.0
*/
creditCardNumber(
options?:
| string
| {
/**
* The name of the issuer (case-insensitive) or the format used to generate one.
*
* @default ''
*/
issuer?: string;
}
): string;
/**
* Generates a random credit card number.
*
* @param options An options object, the issuer or a custom format.
* @param options.issuer The name of the issuer (case-insensitive) or the format used to generate one.
*
* @example
* faker.finance.creditCardNumber() // '4427163488662'
* faker.finance.creditCardNumber({ issuer: 'visa' }) // '4882664999007'
* faker.finance.creditCardNumber({ issuer: '63[7-9]#-####-####-###L' }) // '6375-3265-4676-6646'
* faker.finance.creditCardNumber('visa') // '1226423499765'
*
* @since 5.0.0
*/
creditCardNumber(
options:
| string
| {
/**
* The name of the issuer (case-insensitive) or the format used to generate one.
*
* @default ''
*/
issuer?: string;
} = {}
): string {
if (typeof options === 'string') {
options = { issuer: options };
}
const { issuer = '' } = options;
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.includes('#')) {
// 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.replaceAll('/', '');
return this.faker.helpers.replaceCreditCardSymbols(format);
}
/**
* Generates a random credit card CVV.
*
* @example
* faker.finance.creditCardCVV() // '506'
*
* @since 5.0.0
*/
creditCardCVV(): string {
return this.faker.string.numeric({ length: 3, allowLeadingZeros: true });
}
/**
* Returns a random credit card issuer.
*
* @example
* faker.finance.creditCardIssuer() // 'discover'
*
* @since 6.3.0
*/
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.
*
* @see faker.string.numeric(): For generating the pin with greater control.
*
* @example
* faker.finance.pin() // '5067'
* faker.finance.pin(6) // '213789'
*
* @since 6.2.0
*/
pin(length?: number): string;
/**
* Generates a random PIN number.
*
* @param options An options object.
* @param options.length The length of the PIN to generate. Defaults to `4`.
*
* @throws Will throw an error if length is less than 1.
*
* @see faker.string.numeric(): For generating the pin with greater control.
*
* @example
* faker.finance.pin() // '5067'
* faker.finance.pin({ length: 6 }) // '213789'
*
* @since 6.2.0
*/
pin(options?: {
/**
* The length of the PIN to generate.
*
* @default 4
*/
length?: number;
}): string;
/**
* Generates a random PIN number.
*
* @param options An options object or the length of the PIN.
* @param options.length The length of the PIN to generate. Defaults to `4`.
*
* @throws Will throw an error if length is less than 1.
*
* @see faker.string.numeric(): For generating the pin with greater control.
*
* @example
* faker.finance.pin() // '5067'
* faker.finance.pin({ length: 6 }) // '213789'
* faker.finance.pin(6) // '213789'
*
* @since 6.2.0
*/
pin(
options?:
| number
| {
/**
* The length of the PIN to generate.
*
* @default 4
*/
length?: number;
}
): string;
/**
* Generates a random PIN number.
*
* @param options An options object or the length of the PIN.
* @param options.length The length of the PIN to generate. Defaults to `4`.
*
* @throws Will throw an error if length is less than 1.
*
* @see faker.string.numeric(): For generating the pin with greater control.
*
* @example
* faker.finance.pin() // '5067'
* faker.finance.pin({ length: 6 }) // '213789'
* faker.finance.pin(6) // '213789'
*
* @since 6.2.0
*/
pin(
options:
| number
| {
/**
* The length of the PIN to generate.
*
* @default 4
*/
length?: number;
} = {}
): string {
if (typeof options === 'number') {
options = { length: options };
}
const { length = 4 } = options;
if (length < 1) {
throw new FakerError('minimum length is 1');
}
return this.faker.string.numeric({ length, allowLeadingZeros: true });
}
/**
* Creates a random, non-checksum Ethereum address.
*
* To generate a checksummed Ethereum address (with specific per character casing), wrap this method in a custom method and use third-party libraries to transform the result.
*
* @example
* faker.finance.ethereumAddress() // '0xf03dfeecbafc5147241cc4c4ca20b3c9dfd04c4a'
*
* @since 5.0.0
*/
ethereumAddress(): string {
const address = this.faker.string.hexadecimal({
length: 40,
casing: 'lower',
});
return address;
}
/**
* Generates a random IBAN.
*
* Please note that the generated IBAN might be invalid due to randomly generated bank codes/other country specific validation rules.
*
* @param options An options object.
* @param options.formatted Return a formatted version of the generated IBAN. Defaults to `false`.
* @param options.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({ formatted: true }) // 'FR20 8008 2330 8984 74S3 Z620 224'
* faker.finance.iban({ formatted: true, countryCode: 'DE' }) // 'DE84 1022 7075 0900 1170 01'
*
* @since 4.0.0
*/
iban(
options: {
/**
* Return a formatted version of the generated IBAN.
*
* @default false
*/
formatted?: boolean;
/**
* The country code from which you want to generate an IBAN,
* if none is provided a random country will be used.
*/
countryCode?: string;
} = {}
): string {
const { countryCode, formatted = false } = options;
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.boolean(0.8)) {
s += this.faker.number.int(9);
} else {
s += this.faker.helpers.arrayElement(iban.alpha);
}
} else {
if (c >= 3 && this.faker.datatype.boolean(0.3)) {
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.number.int(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 ? prettyPrintIban(result) : result;
}
/**
* Generates a random SWIFT/BIC code based on the [ISO-9362](https://en.wikipedia.org/wiki/ISO_9362) format.
*
* @param options Options object.
* @param options.includeBranchCode Whether to include a three-digit branch code at the end of the generated code. Defaults to a random boolean value.
*
* @example
* faker.finance.bic() // 'WYAUPGX1'
* faker.finance.bic({ includeBranchCode: true }) // 'KCAUPGR1432'
* faker.finance.bic({ includeBranchCode: false }) // 'XDAFQGT7'
*
* @since 4.0.0
*/
bic(
options: {
/**
* Whether to include a three-digit branch code at the end of the generated code.
*
* @default faker.datatype.boolean()
*/
includeBranchCode?: boolean;
} = {}
): string {
const { includeBranchCode = this.faker.datatype.boolean() } = options;
const bankIdentifier = this.faker.string.alpha({
length: 4,
casing: 'upper',
});
const countryCode = this.faker.helpers.arrayElement(iban.iso3166);
const locationCode = this.faker.string.alphanumeric({
length: 2,
casing: 'upper',
});
const branchCode = includeBranchCode
? this.faker.datatype.boolean()
? this.faker.string.alphanumeric({ length: 3, casing: 'upper' })
: 'XXX'
: '';