forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finance.js
330 lines (286 loc) · 8.96 KB
/
finance.js
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
/**
* @namespace faker.finance
*/
var Finance = function (faker) {
var ibanLib = require("./iban");
var Helpers = faker.helpers,
self = this;
/**
* account
*
* @method faker.finance.account
* @param {number} length
*/
self.account = function (length) {
length = length || 8;
var template = '';
for (var i = 0; i < length; i++) {
template = template + '#';
}
length = null;
return Helpers.replaceSymbolWithNumber(template);
};
/**
* accountName
*
* @method faker.finance.accountName
*/
self.accountName = function () {
return [Helpers.randomize(faker.definitions.finance.account_type), 'Account'].join(' ');
};
/**
* routingNumber
*
* @method faker.finance.routingNumber
*/
self.routingNumber = function () {
var routingNumber = Helpers.replaceSymbolWithNumber('########');
// Modules 10 straight summation.
var sum = 0;
for (var 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);
}
/**
* mask
*
* @method faker.finance.mask
* @param {number} length
* @param {boolean} parens
* @param {boolean} ellipsis
*/
self.mask = function (length, parens, ellipsis) {
//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
var template = '';
for (var i = 0; i < length; i++) {
template = template + '#';
}
//prefix with ellipsis
template = (ellipsis) ? ['...', template].join('') : template;
template = (parens) ? ['(', template, ')'].join('') : template;
//generate random numbers
template = Helpers.replaceSymbolWithNumber(template);
return template;
};
//min and max take in minimum and maximum amounts, dec is the decimal place you want rounded to, symbol is $, €, £, etc
//NOTE: this returns a string representation of the value, if you want a number use parseFloat and no symbol
/**
* amount
*
* @method faker.finance.amount
* @param {number} min
* @param {number} max
* @param {number} dec
* @param {string} symbol
*
* @return {string}
*/
self.amount = function (min, max, dec, symbol) {
min = min || 0;
max = max || 1000;
dec = dec === undefined ? 2 : dec;
symbol = symbol || '';
var randValue = faker.random.number({ max: max, min: min, precision: Math.pow(10, -dec) });
var stringNumber = symbol + randValue.toFixed(dec);
return symbol + randValue.toFixed(dec);
};
/**
* transactionType
*
* @method faker.finance.transactionType
*/
self.transactionType = function () {
return Helpers.randomize(faker.definitions.finance.transaction_type);
};
/**
* currencyCode
*
* @method faker.finance.currencyCode
*/
self.currencyCode = function () {
return faker.random.objectElement(faker.definitions.finance.currency)['code'];
};
/**
* currencyName
*
* @method faker.finance.currencyName
*/
self.currencyName = function () {
return faker.random.objectElement(faker.definitions.finance.currency, 'key');
};
/**
* currencySymbol
*
* @method faker.finance.currencySymbol
*/
self.currencySymbol = function () {
var symbol;
while (!symbol) {
symbol = faker.random.objectElement(faker.definitions.finance.currency)['symbol'];
}
return symbol;
};
/**
* bitcoinAddress
*
* @method faker.finance.bitcoinAddress
*/
self.bitcoinAddress = function () {
var addressLength = faker.random.number({ min: 25, max: 34 });
var address = faker.random.arrayElement(['1', '3']);
for (var i = 0; i < addressLength - 1; i++)
address += faker.random.arrayElement('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split(''));
return address;
}
/**
* litecoinAddress
*
* @method faker.finance.litecoinAddress
*/
self.litecoinAddress = function () {
var addressLength = faker.random.number({ min: 26, max: 33 });
var address = faker.random.arrayElement(['L', 'M', '3']);
for (var i = 0; i < addressLength - 1; i++)
address += faker.random.arrayElement('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split(''));
return address;
}
/**
* Credit card number
* @method faker.finance.creditCardNumber
* @param {string} provider | scheme
*/
self.creditCardNumber = function(provider){
provider = provider || "";
var format, formats;
var localeFormat = faker.definitions.finance.credit_card;
if (provider in localeFormat) {
formats = localeFormat[provider]; // there chould be multiple formats
if (typeof formats === "string") {
format = formats;
} else {
format = faker.random.arrayElement(formats);
}
} else if (provider.match(/#/)) { // The user chose an optional scheme
format = provider;
} else { // Choose a random provider
if (typeof localeFormat === 'string') {
format = localeFormat;
} else if( typeof localeFormat === "object") {
// Credit cards are in a object structure
formats = faker.random.objectElement(localeFormat, "value"); // There chould be multiple formats
if (typeof formats === "string") {
format = formats;
} else {
format = faker.random.arrayElement(formats);
}
}
}
format = format.replace(/\//g,"")
return Helpers.replaceCreditCardSymbols(format);
};
/**
* Credit card CVV
* @method faker.finance.creditCardCVV
*/
self.creditCardCVV = function() {
var cvv = "";
for (var i = 0; i < 3; i++) {
cvv += faker.random.number({max:9}).toString();
}
return cvv;
};
/**
* ethereumAddress
*
* @method faker.finance.ethereumAddress
*/
self.ethereumAddress = function () {
var address = faker.random.hexaDecimal(40).toLowerCase();
return address;
};
/**
* iban
*
* @method faker.finance.iban
*/
self.iban = function (formatted) {
var ibanFormat = faker.random.arrayElement(ibanLib.formats);
var s = "";
var count = 0;
for (var b = 0; b < ibanFormat.bban.length; b++) {
var bban = ibanFormat.bban[b];
var c = bban.count;
count += bban.count;
while (c > 0) {
if (bban.type == "a") {
s += faker.random.arrayElement(ibanLib.alpha);
} else if (bban.type == "c") {
if (faker.random.number(100) < 80) {
s += faker.random.number(9);
} else {
s += faker.random.arrayElement(ibanLib.alpha);
}
} else {
if (c >= 3 && faker.random.number(100) < 30) {
if (faker.random.boolean()) {
s += faker.random.arrayElement(ibanLib.pattern100);
c -= 2;
} else {
s += faker.random.arrayElement(ibanLib.pattern10);
c--;
}
} else {
s += faker.random.number(9);
}
}
c--;
}
s = s.substring(0, count);
}
var checksum = 98 - ibanLib.mod97(ibanLib.toDigitString(s + ibanFormat.country + "00"));
if (checksum < 10) {
checksum = "0" + checksum;
}
var iban = ibanFormat.country + checksum + s;
return formatted ? iban.match(/.{1,4}/g).join(" ") : iban;
};
/**
* bic
*
* @method faker.finance.bic
*/
self.bic = function () {
var vowels = ["A", "E", "I", "O", "U"];
var prob = faker.random.number(100);
return Helpers.replaceSymbols("???") +
faker.random.arrayElement(vowels) +
faker.random.arrayElement(ibanLib.iso3166) +
Helpers.replaceSymbols("?") + "1" +
(prob < 10 ?
Helpers.replaceSymbols("?" + faker.random.arrayElement(vowels) + "?") :
prob < 40 ?
Helpers.replaceSymbols("###") : "");
};
/**
* description
*
* @method faker.finance.transactionDescription
*/
self.transactionDescription = function() {
var account = Helpers.createTransaction().account
var card = faker.finance.mask();
var currency = faker.finance.currencyCode();
var amount = Helpers.createTransaction().amount
var transactionType = Helpers.createTransaction().type
var company = Helpers.createTransaction().business
return transactionType + " transaction at " + company + " using card ending with ***" + card + " for " + currency + " " + amount + " in account ***" + account
}
};
module['exports'] = Finance;