-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-properties.js
More file actions
39 lines (27 loc) · 913 Bytes
/
parser-properties.js
File metadata and controls
39 lines (27 loc) · 913 Bytes
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
const ObjectPath = require("object-path")
parseProperties = (props, currentData, allData, opts = {}) => {
const newResponse = {}
for(const prop of props ) {
const { to, value, from , properties , middleware = [], required, self } = prop
let value2
if (properties) {
const Root = ObjectPath.get(currentData, from)
value2 = Root.map(currentData => (
parseProperties(properties, currentData, allData, opts)
))
} else {
value2 = from ? ObjectPath.get(currentData, from) : value
if(!value2 && required)
throw new Error(`${to} is required`)
middleware.forEach(funcName => {
value2 = (opts.middleware[funcName] || global[funcName])(value2, currentData, allData)
})
}
if(self)
ObjectPath.set(data, self, value2)
else
ObjectPath.set(newResponse, to, value2)
}
return newResponse
}
module.exports = parseProperties