-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.js
More file actions
143 lines (131 loc) · 3.42 KB
/
handler.js
File metadata and controls
143 lines (131 loc) · 3.42 KB
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
const faker = require('faker')
let proxy = {
number(min, max) {
return faker.datatype.number({
min: parseFloat(min),
max: parseFloat(max) || undefined
})
},
password: faker.internet.password,
uuid: faker.datatype.uuid,
boolean: faker.datatype.boolean,
float(precision = 0.01) {
return faker.datatype.float({ precision: parseFloat(precision) })
},
date(start = 0, end = new Date()) {
return faker.date.between(start, end)
},
random() {
return faker.random.arrayElement(arguments)
},
time(start = 0, end = 1) {
return new Date(faker.date.between(start, end)).toLocaleTimeString()
},
phone(format = 'xxxxx xxxxxx') {
return format.replace(/(x)/g, function(value) {
return Math.round(Math.random() * 9)
})
},
lorem(count = 3) {
return faker.lorem.words(count)
},
image() {
return faker.image.image()
},
shortid() {
return faker.datatype
.number({ min: 0, max: 1, precision: 0.00000000000000001 })
.toString(36)
.substr(2, 16)
}
}
function fake(str, user) {
// return the whole user object for a quick shortcut
if (str === '{user}') {
return user
}
return str.replace(/{(.[^{}]+)}/g, function(value) {
let matches = arguments[1].match(/(["'])(?:(?=(\\?))\2.)*?\1|([^\s])+/g)
let split = matches[0].split('.')
let functionName = split[0]
let attr = matches.slice(1)
if (functionName.indexOf('faker') === 0) {
try {
return functionName.split('.').reduce((o, i) => o[i], { faker })()
} catch (error) {
return '[Error: faker function not found]'
}
}
// convert numbers and strings
for (a in attr) {
let isNum = !isNaN(attr[a])
attr[a] = isNum ? parseFloat(attr[a]) : attr[a]
if (!isNum) {
let quoted = attr[a].match(/^["']{1}((?:(?=(\\?))\2.)*?\1+)["']{1}$/m)
if (quoted) {
attr[a] = quoted[1]
}
}
}
if (user[functionName]) {
return user[functionName]
} else if (proxy[functionName]) {
return proxy[functionName].apply(null, attr)
} else {
return value
}
})
}
function format(obj) {
let user = faker.helpers.contextualCard()
let last = faker.name.lastName()
user = {
...user,
...user.address,
name: user.name + ' ' + last,
firstName: user.name,
lastName: last,
title: faker.name.prefix(),
email: faker.internet.email(user.name, last),
company: user.company.name,
domain: faker.internet.domainName(),
country: faker.address.country(),
countryCode: faker.address.countryCode(),
timeZone: faker.address.timeZone(),
creditCard: faker.finance.creditCardNumber(),
color: faker.internet.color()
}
user.address = `${user.address.suite} ${user.address.street}`
delete user.phone
if (obj._repeat) {
let arr = []
const { _repeat, ..._clean } = obj
for (let i = 0; i < obj._repeat; i++) {
arr.push(format({ ..._clean }))
}
obj = arr
}
for (var i in obj) {
if (typeof obj[i] === 'object') {
obj[i] = format(obj[i])
} else {
obj[i] = fake(obj[i], user)
}
}
return obj
}
const handler = (body) => {
if (!body) {
return { error: 'No body to play with' }
}
// seed
if (body._seed !== undefined) {
faker.seed(parseFloat(body._seed))
delete body._seed
}
// locale
faker.locale = body._locale || 'en'
delete body._locale
return format(body)
}
module.exports = handler