-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (62 loc) · 1.62 KB
/
index.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
import crypto from 'crypto';
import { predicates, objects, teams, collections } from 'friendly-words';
const all = [
...predicates,
...objects,
...teams,
...collections,
];
export const rand = (arr) => {
const r = Math.floor(Math.random() * arr.length);
return arr[ r ];
}
export const string = (options = {}) => {
const { min, max } = options;
if(min === undefined) {
return rand(objects);
}
let result = rand(all);
while(result.length < min) {
result += ' ' + rand(all);
}
if(max === undefined) {
return result;
}
return result.substring(0,max);
}
export const bool = (options = {}) => {
return rand([false,true]);
}
export const int = (options = {}) => {
let { min, max } = options;
if(min === undefined) { min = 0; }
if(max === undefined) { max = 10000; }
return min + Math.floor(Math.random() * (max-min));
}
export const firstname = (options = {}) => {
return rand(predicates);
}
export const lastname = (options = {}) => {
return rand(collections);
}
const emailProviders = [
'gmail.com',
'hotmail.com',
'yahoo.com',
'outlook.com',
];
export const email = (options = {}) => {
return firstname() + '.' + lastname() + '@' + rand(emailProviders);
}
export const uuid = (options = {}) => {
return crypto.randomUUID();
}
export const date = (options = {}) => {
let { min, max } = options;
if(min === undefined) { min = '1970-01-01'; }
if(max === undefined) { max = '2050-01-01'; }
//TODO: validate yyyy-mm-dd
const dmin = new Date(min).getTime();
const dmax = new Date(max).getTime();
return new Date(dmin + Math.floor(Math.random()*(dmax - dmin))).toISOString();
}