-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitelisted.js
50 lines (47 loc) · 2 KB
/
whitelisted.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
function whitelisted(obj, whitelist, preKey = null) {
const cleanObject = (obj) => {
if (!obj || typeof obj != 'object') return obj
let newObj = {}
Object.keys(obj).forEach( k => {
const key = preKey ? preKey + '.' + k : k
const everything = !!whitelist.find( i => i.includes(key + '.' + '*'))
if (!!obj[k] && typeof obj[k] === 'object' && !everything) {
if (Array.isArray(obj[k])) {
if (whitelist.find( i => i.includes(key)))
newObj[k] = whitelisted(obj[k], whitelist, key)
} else {
Object.keys(obj[k]).forEach( k2 => {
const kk = k + '.' + k2
const key = preKey ? preKey + '.' + kk : kk
const everything2 = !!whitelist.find( i => i.includes(key + '.' + '*'))
if (typeof obj[k][k2] === 'object' && !everything2) {
if (whitelist.find( i => i.includes(key) )) {
newObj[k] = newObj[k] || {}
newObj[k][k2] = whitelisted(obj[k][k2], whitelist, key)
}
} else {
const str = k + '.' + k2
if (whitelist.includes(str) || everything2) {
newObj[k] = newObj[k] || {}
newObj[k][k2] = obj[k][k2]
}
}
})
}
} else {
const kk = preKey ? preKey + '.' + k : k
if (whitelist.includes(kk) || everything) newObj[k] = obj[k]
}
})
return newObj
}
if (typeof obj == 'object') {
if (Array.isArray(obj))
return obj.map( o => cleanObject(o))
else
return cleanObject(obj)
} else {
return null
}
}
module.exports = whitelisted