-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
244 lines (199 loc) · 6.99 KB
/
index.js
File metadata and controls
244 lines (199 loc) · 6.99 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
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
const express = require('express')
const path = require('path')
const fs = require('fs')
const app = express()
app.use(express.urlencoded({extended: true}))
app.use(express.json()) // To parse the incoming requests with JSON payloads
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
app.use(express.static(path.join(__dirname + '/public')))
app.post('/api', (req, res) => {
const body = req.body
console.log(body)
let result = manageData(body)
res.send(result)
})
function manageData(response) {
let myData = {
data: [ ]
}
let autoincrement = { }
for (let i = 0; i < response.info['quantity']; i++) {
let newElement = { }
// Guardarà el nom actual (permet sincronitzar noms i correus)
let currentName = { }
currentName = receivedName({name: true, firstSurname: true, lastSurname: true})
for (const element in response.data) {
const type = response.data[element].type
let extra = response.data[element]
switch(type) {
case 'Name':
currentName = receivedName(extra)
newElement[element] = currentName.completeName
break
case 'Number':
newElement[element] = receivedNumber(extra, element, autoincrement)
break
case 'Street':
newElement[element] = receivedStreet()
break
case 'Email':
newElement[element] = receivedEmail(currentName)
break
case 'Phone (house)':
newElement[element] = receivedPhoneHouse()
break
case 'Phone (mobile)':
newElement[element] = receivedPhoneMobile()
break
case 'DNI':
newElement[element] = receivedDNI()
break
case 'Date':
newElement[element] = receivedDate(extra)
break
case 'Lorem':
newElement[element] = receivedLorem(extra)
break
default:
let found = false
for (const custom in response.custom) {
console.log(response.custom)
if (element == custom) {
found = true
newElement[element] = receivedCustom(element, response.custom)
//console.log(extra)
}
}
if (!found) newElement[element] = receivedError()
break
}
}
console.log(newElement)
myData.data.push(newElement)
}
return myData
}
function receivedName(extra) {
let dataToReturn = {
name: randomName('name'),
firstSurname: randomName('surname'),
lastSurname: randomName('surname'),
completeName: ""
}
console.log(extra)
if (extra.name) {
dataToReturn.completeName += dataToReturn.name + " "
}
if (extra.firstSurname) {
dataToReturn.completeName += dataToReturn.firstSurname + " "
}
if (extra.lastSurname) {
dataToReturn.completeName += dataToReturn.lastSurname + " "
}
dataToReturn.completeName = dataToReturn.completeName.slice(0, -1)
return dataToReturn
}
function receivedNumber(extra, element, autoincrement) {
// El valor mínim serà, si se n'ha donat un, aquest.
// Si no, revisarem: és autoincrement? Llavors 1. Del contrari, 0
let min = extra.min
? parseInt(extra.min)
: extra.autoincrement
? 1
: 0
// El valor màxim serà, si se n'ha donat un, aquest.
// Si no, serà 100
let max = extra.max ? parseInt(extra.max) : 100
if (extra.autoincrement) {
if (autoincrement[element]) {
return autoincrement[`${element}`]++
}
else {
autoincrement[`${element}`] = min + 1
return min
}
}
return randomNumber(min, max)
}
function receivedStreet() {
let data = fs.readFileSync(path.join(__dirname, './data/street.json'))
try {
const streets = JSON.parse(data)
random = Math.floor(Math.random() * streets.length)
return streets[random]
} catch (err) {
console.log("Something went wrong...")
}
}
function receivedEmail(currentName) {
return `${currentName.name.toLowerCase().replace(/\s/g, "")}@gmail.com`
}
function receivedPhoneHouse() {
let phone = "9"
for (let i = 0; i < 8; i++) {
phone += randomNumber(0, 9)
}
return parseInt(phone)
}
function receivedPhoneMobile() {
let phone = "6"
for (let i = 0; i < 8; i++) {
phone += randomNumber(0, 9)
}
return parseInt(phone)
}
function receivedDate(extra) {
let min = extra.min ? new Date(extra.min) : new Date('1991-01-01')
let max = extra.max ? new Date(extra.max) : new Date()
let format = extra.format ? extra.format : 'YYYY-MM-DD'
console.log(new Date(+min + Math.random() * (max - min)))
return new Date(+min + Math.random() * (max - min)).toISOString().slice(0, 10)
}
function receivedLorem(extra) {
let min = extra.min ? parseInt(extra.min) : 10
let max = extra.max ? parseInt(extra.max) : 500
let data = String(fs.readFileSync(path.join(__dirname, './data/lorem.txt')))
return data.slice(0, min + Math.random() * (max - min))
}
function receivedError() { return "Something went wrong!" }
function receivedDNI() {
let dni = ""
for (let i = 0; i < 8; i++) {
dni += Math.floor(Math.random() * 10)
}
dni += randomLetter().toUpperCase()
return dni
}
function receivedCustom(name, customValues) {
let randomInt = randomNumber(0, customValues[`${name}`].length - 1)
return customValues[`${name}`][randomInt]
}
function randomName(type) {
let data
if (type == 'name') data = fs.readFileSync(path.join(__dirname, './data/first-name.json'))
else data = fs.readFileSync(path.join(__dirname, './data/last-name.json'))
try {
const names = JSON.parse(data)
random = Math.floor(Math.random() * names.length)
let actualName = names[random]
let words = actualName.split(" ")
for(let i = 0; i < words.length; i++) {
words[i] = words[i][0].toUpperCase() + words[i].slice(1).toLowerCase()
}
return words.join(" ")
} catch (err) {
console.log("Something went wrong...")
}
}
function randomLetter() {
const alphabet = "abcdefghijklmnopqrstuvwxyz"
return alphabet[Math.floor(Math.random() * alphabet.length)]
}
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min +1)) + min
}
app.listen(81)