-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (64 loc) · 1.64 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
const sanitizeString = (data, model) => {
let newData = data
const props = model.split('.')
for (let i = 0; i < props.length; i++) {
if (newData[props[i]]) {
newData = newData[props[i]]
} else {
newData = null
break
}
}
return newData
}
const sanitizeArray = (data, model) => {
let newData = []
if (data && data.length) {
data.forEach((item) => {
newData.push(sanitizeObject(item, model[0]))
})
}
return newData
}
const sanitizeObject = (data, model) => {
const newData = {}
if (data) {
for(const prop in model) {
if (typeof model[prop] === 'function') {
newData[prop] = model[prop](data[prop])
} else if (model[prop] === true) {
newData[prop] = data[prop]
} else if (typeof model[prop] === 'string') {
newData[prop] = sanitizeString(data, model[prop])
} else if (Array.isArray(model[prop])) {
if (data[prop]) {
newData[prop] = sanitizeArray(data[prop], model[prop])
} else {
newData[prop] = []
}
} else if (typeof model[prop] === 'object') {
if (data[prop]) {
newData[prop] = sanitizeObject(data[prop], model[prop])
} else {
newData[prop] = null
}
}
}
}
return newData
}
const main = (data, model) => {
if (!data || !model) return data
let Model = model
if (typeof model === 'string') {
Model = require(`../../sanitizer/${model}`)
}
if (Array.isArray(data)) {
return sanitizeArray(data, Model)
} else if (typeof data === 'object') {
return sanitizeObject(data, Model)
} else {
return data
}
}
module.exports = main