/
masksFakesGeneraters.js
463 lines (389 loc) · 17.9 KB
/
masksFakesGeneraters.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
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
// Generate a random date between a start point of millis after 01-Jan-1970 and a maximum set of milliseconds after that date
function fakeDateMillisFromEpoch(startMillis, maxMillis) {
return {
"$toDate": {"$add": [{"$dateFromString": {"dateString": "1970-01-01"}}, startMillis, {"$multiply": [{"$rand": {}}, maxMillis]}]}
};
}
// Generate a random date between now and a maximum number of milliseconds from now
function fakeDateAfterNow(maxMillisFromNow) {
return {
"$toDate": {"$add": ["$$NOW", {"$multiply": [{"$rand": {}}, maxMillisFromNow]}]}
};
}
// Generate a random date between a maximum number of milliseconds before now
function fakeDateBeforeNow(maxMillisBeforeNow) {
return {
"$toDate": {"$subtract": ["$$NOW", {"$multiply": [{"$rand": {}}, maxMillisBeforeNow]}]}
};
}
// Generate a whole number up to a maximum number of digits (any more than 15 are ignored)
function fakeNumber(numberOfDigits) {
return {
"$let": {
"vars": {
"amountOrZero": {"$max": [1 , numberOfDigits]},
},
"in": {
"$toLong": {"$substrCP": [{"$toString": {"$toDecimal": {"$rand": {}}}}, 2, {"$min": ["$$amountOrZero", 16]}]}
}
}
};
}
// Generate a whole number between a given minimum and maximum number (inclusive)
function fakeNumberBounded(minNumber, maxNumber) {
return {
"$toLong": {"$add": [minNumber, {"$floor": {"$multiply": [{"$rand": {}}, {"$subtract": [maxNumber, minNumber]}]}}]}
};
}
// Generate a text representation of whole number to a specific number of digits (characters) in length (max 30)
function fakePaddedNumberAsText(numberOfDigits) {
return {
"$let": {
"vars": {
"amountOrZero": {"$max": [0, numberOfDigits]},
},
"in": {
"$substrCP": [
{"$concat": [
{"$substrCP": [{"$toString": {"$toDecimal": {"$rand": {}}}}, 2, 16]},
{"$substrCP": [{"$toString": {"$toDecimal": {"$rand": {}}}}, 2, 16]},
"0000000000000000000000000000000000000000000000000000000000000001"
]},
0,
{"$min": ["$$amountOrZero", 30]}
]
}
}
};
}
// Generate a decimal number between 0.0 and 1.0 with up to 16 decimal places
function fakeDecimal() {
return {
"$toDecimal": {"$rand": {}}
};
}
// Generate a decimal number with up to a specified number of significant places (e.g. '3' places -> 736.274473638742)
function fakeDecimalSignificantPlaces(maxSignificantPlaces) {
return {
"$multiply": [{"$toDecimal": {"$rand": {}}}, {"$pow": [10, {"$min": [maxSignificantPlaces, 16]}]}]
};
}
// Generate a currency amount with just 2 decimal places and up to a specified number of significant places (e.g. '3' places -> 736.27)
function fakeMoneyAmountDecimal(maxSignificantPlaces) {
return {
"$round": [fakeDecimalSignificantPlaces(maxSignificantPlaces), 2]
};
}
// Generate a True or False value randomly
function fakeBoolean() {
return {
"$cond": {
"if": {"$lt": [{"$rand": {}}, 0.5]},
"then": true,
"else": false,
}
};
}
// Generate a True or False value randomly but where True is likely for a specified percentage of invocations (e.g. 40 -> 40% likely to be True)
function fakeBooleanWeighted(targetAvgPercentTrue) {
return {
"$cond": {
"if": {"$lte": [{"$rand": {}}, {"$divide": [targetAvgPercentTrue, 100]}]},
"then": true,
"else": false,
}
};
}
// Return the first value on avererage the specified percentage of invocations otherwise returning second value
function fakeOneOfTwoValuesWeighted(firstVal, secondVal, avgPercentFirstVal) {
return {
"$cond": {
"if": {"$lte": [{"$rand": {}}, {"$divide": [avgPercentFirstVal, 100]}]},
"then": firstVal,
"else": secondVal,
}
};
}
// Randomly return one value from a provided list
function fakeValueFromList(listOfValues) {
return {
"$let": {
"vars": {
"values": listOfValues,
},
"in": {
"$arrayElemAt": ["$$values", {"$floor": {"$multiply": [{"$size": "$$values"}, {"$rand": {}}]}}]
}
}
};
}
// Randomly return one value from a provided list but where values later in the list are more likely to be returned on average
function fakeValueFromListWeighted(listOfValues) {
return {
"$let": {
"vars": {
"values": listOfValues,
},
"in": {
"$let": {
"vars": {
"threshold": {
"$floor": {"$multiply": [
{"$reduce": {
"input": {"$range": [1, {"$add": [{"$size": "$$values"}, 1]}]},
"initialValue": 0,
"in": {
"$add": ["$$value", "$$this"]
}
}},
{"$rand": {}},
]}
}
},
"in": {
"$getField": {"field": "result", "input": { // $getField ONLY EXISTS AND WORKS IN MDB v5.0+
"$reduce": {
"input": {"$range": [0, {"$size": "$$values"}]},
"initialValue": {
"accumulator": 0,
"result": null,
},
"in": {
"accumulator": {"$add": ["$$value.accumulator", "$$this", 1]},
"result": {"$ifNull": ["$$value.result", {
"$cond": {
"if": {"$gte": [{"$add": ["$$value.accumulator", "$$this", 1]}, "$$threshold"]},
"then": {"$arrayElemAt": ["$$values", "$$this"]},
"else": "$$value.result"
}
}]},
}
}
}}
}
}
}
}
};
}
// Generate an array of sub-documents with the specified size, where each item is randomly taken from the input list
function fakeListOfSubDocs(numSumDocs, listOfValues) {
return {
"$map": {
"input": {"$range": [0, numSumDocs]},
"in": {
// Have to use $ifNull to do nothing cos 'in:' expects an object so need a no-op expression as object key
"$ifNull": ["$DUMMY", fakeValueFromList(listOfValues)],
}
}
};
}
// Generate string composed of the same character repeated the specified number of times
function fakeNSameChars(char, amount) {
return {
"$reduce": {
"input": {"$range": [0, amount]},
"initialValue": "",
"in": {"$concat": ["$$value", char]}
}
};
}
// Generate string composed of random English alphabet uppercase characters repeated the specified number of times
function fakeNAnyUpperChars(amount) {
return {
"$reduce": {
"input": {"$range": [0, amount]},
"initialValue": "",
"in": {"$concat": ["$$value", fakeValueFromList(["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"])]}
}
};
}
// Generate string composed of random English alphabet lowercase characters repeated the specified number of times
function fakeNAnyLowerChars(amount) {
return {"$toLower": fakeNAnyUpperChars(amount)};
}
// Generate a typical first name from an internal pre-defined list of common first names
function fakeFirstName() {
return fakeValueFromList(["Maria", "Nushi", "Mohammed", "Jose", "Muhammad", "Mohamed", "Wei", "Yan", "John", "David", "Li", "Abdul", "Ana", "Ying", "Michael", "Juan", "Anna", "Mary", "Daniel", "Luis", "Elena", "Marie", "Ibrahim", "Peter", "Sarah", "Xin", "Lin", "Olga"]);
}
// Generate a typical last name from an internal pre-defined list of common last names
function fakeLastName() {
return fakeValueFromList(["Wang", "Zhang", "Chen", "Singh", "Kumar", "Ali", "Nguyen", "Khan", "Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis", "Rodriguez", "Martinez", "Hernandez", "Lopez", "Gonzales", "Jackson", "Lee", "Perez", "Thompson"]);
}
// Generate a typical first name and last name from an internal pre-defined list of names
function fakeFirstAndLastName() {
return {"$concat": [fakeFirstName(), " ", fakeLastName()]};
}
// Generate a random email address with random chars for the email id @ one of a few fixed .com domains
function fakeEmailAddress(idChars) {
return {"$concat": [fakeNAnyLowerChars(idChars), "@", fakeValueFromList(["mymail.com", "fastmail.com", "acmemail.com"])]};
}
// Generate a random IPv4 address in text format of 'xxx.xxx.xxx.xxx'
function fakeIPAddress() {
return {"$concat": [{"$toString": fakeNumberBounded(0, 255)}, ".", {"$toString": fakeNumberBounded(0, 255)}, ".", {"$toString": fakeNumberBounded(0, 255)}, ".", {"$toString": fakeNumberBounded(0, 255)}]};
}
// Generate a typical street name from an internal pre-defined list of common street names
function fakeStreetName() {
return fakeValueFromList(["Station Road", "Park Road", "Church Street", "Victoria Road", "Green Lane", "Manor Road", "Rue de l'Église", "Grande Rue", "Rue du Moulin", "Rue du Château", "Hauptstraße", "Dorfstraße", "Schulstraße", "Bahnhofstraße", "Fourth Street", "Park Street", "Kerkstraat"]);
}
// Generate a typical town name from an internal pre-defined list of common town names
function fakeTownName() {
return fakeValueFromList(["Bamborourgh", "Forstford", "Irragin", "Shepshed", "Hardersfield", "Beckinsdale", "Swadlincote", "Lunaris", "Accreton", "Cumdivock", "Sirencester", "Ormkirk", "Blencogo", "Llaneybyder", "Haerndean", "Frostford", "Stamford", "Auchendinny", "Stathford"]);
}
// Randomly return the name of one of the countries in the world
function fakeCountryName() {
return fakeValueFromList(["Australia", "Austria", "Brazil", "Canada", "China", "Egypt", "France", "Germany", "Ghana", "Greece", "India", "Indonesia", "Italy", "Japan", "Malaysia", "Mexico", "Nigeria", "Russia", "Saudi Arabia", "Singapore", "South Africa", "Spain", "Turkey", "United Arab Emirates", "United Kingdom", "United States of America"]);
}
// Generate a random US-style zipcode/postcode (e.g. 10144)
function fakeZipCode() {
return fakeNumber(5);
}
// Generate a typical company name from an internal pre-defined list of common company names
function fakeCompanyName() {
return fakeValueFromList(["Wonka Industries", "Acme Corp.", "Stark Industries", "Gekko & Co", "Wayne Enterprises", "Cyberdyne Systems", "Genco Pura Olive Oil Company", "Bubba Gump", "Olivia Pope & Associates", "Krusty Krab", "Sterling Cooper", "Soylent", "Hooli", "Good Burger", "Globex Corporation", "Initech", "Umbrella Corporation", "Vehement Capital Partners", "Massive Dynamic"]);
}
// Generate a random place with a typical name, an id and some geo coordinates
function fakeLocationWithCoordinates() {
return fakeValueFromList([
{"name": "Zoo Station", "id": "327833622", "location": {"type": "Point", "coordinates": [-0.1, 51.5]}},
{"name": "Shelter Place", "id": "264329372", "location": {"type": "Point", "coordinates": [-73.99, 40.7]}},
{"name": "Happy Church", "id": "726383926", "location": {"type": "Point", "coordinates": [103.8, 1.33]}},
{"name": "Tiny Tower", "id": "918273645", "location": {"type": "Point", "coordinates": [151.2, -33.9]}},
{"name": "Cosy Cavern", "id": "136272621", "location": {"type": "Point", "coordinates": [-51.75, 64.17]}},
{"name": "Warm Temple", "id": "826473826", "location": {"type": "Point", "coordinates": [-66, -54.9]}},
{"name": "Peace Corner", "id": "448172639", "location": {"type": "Point", "coordinates": [-7, 62.2]}},
{"name": "Grassy Cove", "id": "371927611", "location": {"type": "Point", "coordinates": [-5.2, 50]}},
{"name": "Sub Sanctuary", "id": "484728162", "location": {"type": "Point", "coordinates": [-1.79, 53.3]}},
{"name": "High Hill", "id": "338817262", "location": {"type": "Point", "coordinates": [-3.27, 53.15]}},
]);
}
// Replace the first specified number of characters in a field's value with 'x's
function maskReplaceFirstPart(strOrNum, amount) {
return {
"$let": {
"vars": {
"text": {"$toString": strOrNum},
"amountOrZero": {"$max": [0, amount]},
},
"in": {
"$let": {
"vars": {
"length": {"$strLenCP": "$$text"},
"remainder": {"$subtract": [{"$strLenCP": {"$toString": "$$text"}}, "$$amountOrZero"]},
},
"in": {
"$concat": [fakeNSameChars("x", {"$min": ["$$amountOrZero", "$$length"]}), {"$substrCP": ["$$text", "$$amountOrZero", {"$max": ["$$remainder", 0]}]}]
}
}
}
}
}
}
// Replace the last specified number of characters in a field's value with 'x's
function maskReplaceLastPart(strOrNum, amount) {
return {
"$let": {
"vars": {
"text": {"$toString": strOrNum},
"amountOrZero": {"$max": [0, amount]},
},
"in": {
"$let": {
"vars": {
"length": {"$strLenCP": "$$text"},
"remainder": {"$subtract": [{"$strLenCP": "$$text"}, "$$amountOrZero"]},
},
"in": {
"$concat": [{"$substrCP": ["$$text", 0, {"$max": ["$$remainder", 0]}]}, fakeNSameChars("x", {"$min": ["$$amountOrZero", "$$length"]})]
}
}
}
}
}
}
// Replace all the characters in a field's value with 'x's
function maskReplaceAll(strOrNum) {
return fakeNSameChars("x", {"$strLenCP": {"$toString": strOrNum}})
}
// Change the value of a decimal number by adding or taking away a random amount up to a maximum percentage of its current value (e.g. change current value by + or - 10%)
function maskAlterDecimal(currentValue, percent) {
return {
"$let": {
"vars": {
"fraction": {"$divide": [percent, 100]},
},
"in": {
"$add": [{"$multiply": [{"$subtract": [{"$rand": {}}, 0.5]}, 2, "$$fraction", currentValue]}, currentValue]
}
}
}
}
// Change the value of a whole number by adding or taking away a random amount up to a maximum percentage of its current value, rounded (e.g. change current value by + or - 10%)
function maskAlterNumber(currentValue, percent) {
return {
"$toLong": {"$round": maskAlterDecimal(currentValue, percent)}
}
}
// Change the value of a datetime by adding or taking away a random amount up to a maximum percentage of its current value (e.g. change current value by + or - 10%)
function maskAlterDate(currentValue, maxChangeMillis) {
return {
"$add": [{"$multiply": [{"$subtract": [{"$rand": {}}, 0.5]}, 2, maxChangeMillis]}, currentValue]
}
}
// Return the same boolean value for a given percentage of time (e.g 40%), and for the rest of the time return the opposite value
function maskAlterBoolean(currentValue, percentSameValue) {
return {
"$let": {
"vars": {
"fraction": {"$divide": [percentSameValue, 100]},
},
"in": {
"$cond": {
"if": {"$lte": [{"$rand": {}}, "$$fraction"]},
"then": currentValue,
"else": {"$not": currentValue},
}
}
}
};
}
// Return the same value for a given percentage of time (e.g 40%), and for the rest of the time return a random value from the given list
function maskAlterValueFromList(currentValue, percentSameValue, otherValuesList) {
return {
"$let": {
"vars": {
"fraction": {"$divide": [percentSameValue, 100]},
},
"in": {
"$cond": {
"if": {"$lte": [{"$rand": {}}, "$$fraction"]},
"then": currentValue,
"else": fakeValueFromList(otherValuesList),
}
}
}
};
}
// Change on average a given percentage of the list members values to a random value from the provided alternative list
function maskAlterListFromList(currentList, percentSameValues, otherValuesList) {
return {
"$let": {
"vars": {
"fraction": {"$divide": [percentSameValues, 100]},
},
"in": {
"$map": {
"input": currentList,
"as": "item",
"in": {
"$cond": {
"if": {"$lte": [{"$rand": {}}, "$$fraction"]},
"then": "$$item",
"else": fakeValueFromList(otherValuesList),
}
}
}
}
}
};
}