forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
376 lines (349 loc) · 11.2 KB
/
helpers.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
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
/**
*
* @namespace faker.helpers
*/
var Helpers = function (faker) {
var self = this;
/**
* backword-compatibility
*
* @method faker.helpers.randomize
* @param {array} array
*/
self.randomize = function (array) {
array = array || ["a", "b", "c"];
return faker.random.arrayElement(array);
};
/**
* slugifies string
*
* @method faker.helpers.slugify
* @param {string} string
*/
self.slugify = function (string) {
string = string || "";
return string.replace(/ /g, '-').replace(/[^\w\.\-]+/g, '');
};
/**
* parses string for a symbol and replace it with a random number from 1-10
*
* @method faker.helpers.replaceSymbolWithNumber
* @param {string} string
* @param {string} symbol defaults to `"#"`
*/
self.replaceSymbolWithNumber = function (string, symbol) {
string = string || "";
// default symbol is '#'
if (symbol === undefined) {
symbol = '#';
}
var str = '';
for (var i = 0; i < string.length; i++) {
if (string.charAt(i) == symbol) {
str += faker.random.number(9);
} else if (string.charAt(i) == "!"){
str += faker.random.number({min: 2, max: 9});
} else {
str += string.charAt(i);
}
}
return str;
};
/**
* parses string for symbols (numbers or letters) and replaces them appropriately (# will be replaced with number,
* ? with letter and * will be replaced with number or letter)
*
* @method faker.helpers.replaceSymbols
* @param {string} string
*/
self.replaceSymbols = function (string) {
string = string || "";
var 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']
var str = '';
for (var i = 0; i < string.length; i++) {
if (string.charAt(i) == "#") {
str += faker.random.number(9);
} else if (string.charAt(i) == "?") {
str += faker.random.arrayElement(alpha);
} else if (string.charAt(i) == "*") {
str += faker.random.boolean() ? faker.random.arrayElement(alpha) : faker.random.number(9);
} else {
str += string.charAt(i);
}
}
return str;
};
/**
* replace symbols in a credit card schems including Luhn checksum
*
* @method faker.helpers.replaceCreditCardSymbols
* @param {string} string
* @param {string} symbol
*/
self.replaceCreditCardSymbols = function(string, symbol) {
symbol = symbol || "#";
// Function calculating the Luhn checksum of a number string
var getCheckBit = function(number) {
number.reverse();
number = number.map(function(num, index){
if(index%2 === 0) {
num *= 2;
if(num>9) {
num -= 9;
}
}
return num;
});
var sum = number.reduce(function(prev,curr){return prev + curr;});
return sum % 10;
};
string = string || "";
string = faker.helpers.regexpStyleStringParse(string); // replace [4-9] with a random number in range etc...
string = faker.helpers.replaceSymbolWithNumber(string, symbol); // replace ### with random numbers
var numberList = string.replace(/\D/g,"").split("").map(function(num){return parseInt(num);});
var checkNum = getCheckBit(numberList);
return string.replace("L",checkNum);
};
/** string repeat helper, alternative to String.prototype.repeat.... See PR #382
*
* @method faker.helpers.repeatString
* @param {string} string
* @param {number} num
*/
self.repeatString = function(string,num) {
if(typeof num ==="undefined") {
num = 0;
}
var text = "";
for(var i = 0; i < num; i++){
text += string.toString();
}
return text;
};
/**
* parse string paterns in a similar way to RegExp
*
* e.g. "#{3}test[1-5]" -> "###test4"
*
* @method faker.helpers.regexpStyleStringParse
* @param {string} string
*/
self.regexpStyleStringParse = function(string){
string = string || "";
// Deal with range repeat `{min,max}`
var RANGE_REP_REG = /(.)\{(\d+)\,(\d+)\}/;
var REP_REG = /(.)\{(\d+)\}/;
var RANGE_REG = /\[(\d+)\-(\d+)\]/;
var min, max, tmp, repetitions;
var token = string.match(RANGE_REP_REG);
while(token !== null){
min = parseInt(token[2]);
max = parseInt(token[3]);
// switch min and max
if(min>max) {
tmp = max;
max = min;
min = tmp;
}
repetitions = faker.random.number({min:min,max:max});
string = string.slice(0,token.index) + faker.helpers.repeatString(token[1], repetitions) + string.slice(token.index+token[0].length);
token = string.match(RANGE_REP_REG);
}
// Deal with repeat `{num}`
token = string.match(REP_REG);
while(token !== null){
repetitions = parseInt(token[2]);
string = string.slice(0,token.index)+ faker.helpers.repeatString(token[1], repetitions) + string.slice(token.index+token[0].length);
token = string.match(REP_REG);
}
// Deal with range `[min-max]` (only works with numbers for now)
//TODO: implement for letters e.g. [0-9a-zA-Z] etc.
token = string.match(RANGE_REG);
while(token !== null){
min = parseInt(token[1]); // This time we are not capturing the char befor `[]`
max = parseInt(token[2]);
// switch min and max
if(min>max) {
tmp = max;
max = min;
min = tmp;
}
string = string.slice(0,token.index) +
faker.random.number({min:min, max:max}).toString() +
string.slice(token.index+token[0].length);
token = string.match(RANGE_REG);
}
return string;
};
/**
* takes an array and returns it randomized
*
* @method faker.helpers.shuffle
* @param {array} o
*/
self.shuffle = function (o) {
if (typeof o === 'undefined' || o.length === 0) {
return [];
}
o = o || ["a", "b", "c"];
for (var j, x, i = o.length-1; i; j = faker.random.number(i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
/**
* mustache
*
* @method faker.helpers.mustache
* @param {string} str
* @param {object} data
*/
self.mustache = function (str, data) {
if (typeof str === 'undefined') {
return '';
}
for(var p in data) {
var re = new RegExp('{{' + p + '}}', 'g')
str = str.replace(re, data[p]);
}
return str;
};
/**
* createCard
*
* @method faker.helpers.createCard
*/
self.createCard = function () {
return {
"name": faker.name.findName(),
"username": faker.internet.userName(),
"email": faker.internet.email(),
"address": {
"streetA": faker.address.streetName(),
"streetB": faker.address.streetAddress(),
"streetC": faker.address.streetAddress(true),
"streetD": faker.address.secondaryAddress(),
"city": faker.address.city(),
"state": faker.address.state(),
"country": faker.address.country(),
"zipcode": faker.address.zipCode(),
"geo": {
"lat": faker.address.latitude(),
"lng": faker.address.longitude()
}
},
"phone": faker.phone.phoneNumber(),
"website": faker.internet.domainName(),
"company": {
"name": faker.company.companyName(),
"catchPhrase": faker.company.catchPhrase(),
"bs": faker.company.bs()
},
"posts": [
{
"words": faker.lorem.words(),
"sentence": faker.lorem.sentence(),
"sentences": faker.lorem.sentences(),
"paragraph": faker.lorem.paragraph()
},
{
"words": faker.lorem.words(),
"sentence": faker.lorem.sentence(),
"sentences": faker.lorem.sentences(),
"paragraph": faker.lorem.paragraph()
},
{
"words": faker.lorem.words(),
"sentence": faker.lorem.sentence(),
"sentences": faker.lorem.sentences(),
"paragraph": faker.lorem.paragraph()
}
],
"accountHistory": [faker.helpers.createTransaction(), faker.helpers.createTransaction(), faker.helpers.createTransaction()]
};
};
/**
* contextualCard
*
* @method faker.helpers.contextualCard
*/
self.contextualCard = function () {
var name = faker.name.firstName(),
userName = faker.internet.userName(name);
return {
"name": name,
"username": userName,
"avatar": faker.internet.avatar(),
"email": faker.internet.email(userName),
"dob": faker.date.past(50, new Date("Sat Sep 20 1992 21:35:02 GMT+0200 (CEST)")),
"phone": faker.phone.phoneNumber(),
"address": {
"street": faker.address.streetName(true),
"suite": faker.address.secondaryAddress(),
"city": faker.address.city(),
"zipcode": faker.address.zipCode(),
"geo": {
"lat": faker.address.latitude(),
"lng": faker.address.longitude()
}
},
"website": faker.internet.domainName(),
"company": {
"name": faker.company.companyName(),
"catchPhrase": faker.company.catchPhrase(),
"bs": faker.company.bs()
}
};
};
/**
* userCard
*
* @method faker.helpers.userCard
*/
self.userCard = function () {
return {
"name": faker.name.findName(),
"username": faker.internet.userName(),
"email": faker.internet.email(),
"address": {
"street": faker.address.streetName(true),
"suite": faker.address.secondaryAddress(),
"city": faker.address.city(),
"zipcode": faker.address.zipCode(),
"geo": {
"lat": faker.address.latitude(),
"lng": faker.address.longitude()
}
},
"phone": faker.phone.phoneNumber(),
"website": faker.internet.domainName(),
"company": {
"name": faker.company.companyName(),
"catchPhrase": faker.company.catchPhrase(),
"bs": faker.company.bs()
}
};
};
/**
* createTransaction
*
* @method faker.helpers.createTransaction
*/
self.createTransaction = function(){
return {
"amount" : faker.finance.amount(),
"date" : new Date(2012, 1, 2), //TODO: add a ranged date method
"business": faker.company.companyName(),
"name": [faker.finance.accountName(), faker.finance.mask()].join(' '),
"type" : self.randomize(faker.definitions.finance.transaction_type),
"account" : faker.finance.account()
};
};
return self;
};
/*
String.prototype.capitalize = function () { //v1.0
return this.replace(/\w+/g, function (a) {
return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
});
};
*/
module['exports'] = Helpers;