-
Notifications
You must be signed in to change notification settings - Fork 0
/
patchSection.js
39 lines (33 loc) · 954 Bytes
/
patchSection.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
// dependencies
// constants
// library
async function patchSection (original, patch) {
for (const field in patch) {
if (field === 'mappings') {
for (const mapping of patch.mappings) {
let existingMapping
if (original.mappings) {
([existingMapping] = original.mappings.filter(
p => p.connection === mapping.connection &&
p.kind === mapping.kind &&
(p.field === mapping.field || (!p.field && mapping.field === 'id'))
))
} else {
original.mappings = []
}
if (existingMapping) {
existingMapping.field = mapping.field
existingMapping.key = mapping.key
existingMapping.matched_via = mapping.matched_via
} else {
original.mappings.push(mapping)
}
}
} else {
original[field] = patch[field]
}
}
return original
}
// exports
module.exports = patchSection