-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit-into-overlay.js
More file actions
32 lines (28 loc) · 1.01 KB
/
split-into-overlay.js
File metadata and controls
32 lines (28 loc) · 1.01 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
const jsonPathWhere = require('./json-path-where')
const jsonPointerToArray = require('./json-pointer-to-array')
const jsonPointerToJsonPath = require('./json-pointer-to-json-path')
const get = require('./get')
module.exports = function splitIntoOverlay(input, {targets=[], fields=[], where=[]} = {}) {
let overlay = {overlays: '1.0.0'}
targets = Array.isArray(targets) ? targets : [targets]
where = Array.isArray(where) ? where : [where]
if(!targets.length)
return overlay
let paths = jsonPathWhere(input, targets, where)
let actions = paths.map(path => {
let update
if(!fields.length) {
update = get(input, jsonPointerToArray(path))
} else {
update = fields.reduce((acc, field) => {
acc[field] = get(input, [...jsonPointerToArray(path), field])
return acc
}, {})
}
return {
target: jsonPointerToJsonPath(path),
update,
}
})
return {...overlay, actions}
}