-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (53 loc) · 1.72 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
const firstnames = require('./data/firstnames')
const lastnames = require('./data/lastnames')
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'
const domains = require('./data/domains')
const { v4: uuid } = require('uuid')
const pickRandom = require('./helpers/pickRandom')
const nArray = require('./helpers/nArray')
const factories = {
firstname: () => {
return pickRandom(firstnames)
},
lastname: () => {
return pickRandom(lastnames)
},
uuid: () => {
return uuid()
},
email: () => {
let randomString = ''
nArray(10).forEach(i => randomString += pickRandom(characters)) // generate 10 random characters
const randomDomain = pickRandom(domains)
return randomString + '@' + randomDomain
},
phoneNumber: (options) => {
const numbers = nArray(10)
let randomPhoneNumber = ''
nArray(options.length).forEach(i => randomPhoneNumber += pickRandom(numbers)) // generate n random numbers as a string
return randomPhoneNumber
},
countryCode: () => {
const numbers = nArray(10)
let randomPhoneNumber = ''
nArray(2).forEach(i => randomPhoneNumber += pickRandom(numbers))
return randomPhoneNumber
}
}
module.exports = (template, n = 1) => {
const records = []
nArray(n).forEach(i => {
const randomValues = {}
Object.entries(template).forEach(([ field, options ]) => {
const { type, enumeration, ordered } = options
if (enumeration) {
if (ordered) randomValues[field] = enumeration[(i + enumeration.length) % enumeration.length]
else randomValues[field] = pickRandom(enumeration)
} else {
randomValues[field] = factories[type](options)
}
})
records.push(randomValues)
})
return n === 1 ? records[0] : records
}