forked from github/safe-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeDeep.js
260 lines (242 loc) · 9.43 KB
/
mergeDeep.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const mergeBy = require('./mergeArrayBy')
const NAME_FIELDS = ['name', 'username']
class MergeDeep {
constructor (log, ignorableFields, configvalidators = {}, overridevalidators = {}) {
this.log = log
this.ignorableFields = ignorableFields
this.configvalidators = configvalidators
this.overridevalidators = overridevalidators
}
isObject (item) {
return (item && typeof item === 'object' && !Array.isArray(item))
}
isEmpty (item) {
if (this.isObject(item)) {
return Object.keys(item).length === 0
} else if (Array.isArray(item)) {
for (const a of item) {
if (!this.isEmpty(a)) {
return false
}
}
return true
} else {
return item === undefined
}
}
compareEmptyTarget (t, s, additions, modifications) {
additions = Object.assign(additions, s)
return ({ additions, modifications, hasChanges: true })
}
/**
* Compare deeply a base object with overlay object
*
* @param {*} t base object
* @param {*} s overlay object
* @param {*} additions aggregated so far
* @param {*} modifications aggregated so far
* @returns object with additions, modifications
*/
compareDeep (t, s, additions, modifications) {
if (additions === undefined) {
if (Array.isArray(s)) {
additions = []
modifications = []
} else {
additions = {}
modifications = {}
}
}
if (t === undefined || this.isEmpty(t)) {
return this.compareEmptyTarget(t, s, additions, modifications)
}
// Don't iterate characters (string primitives are compared in the loop)
if (typeof s === 'string') {
return
}
// Convert top-level array to object
const target = Object.assign({}, t)
const source = Object.assign({}, s)
for (const key in source) {
// Logic specific for Github
// API response includes urls for resources, or other ignorable fields; we can ignore them
if (key.indexOf('url') >= 0 || this.ignorableFields.indexOf(key) >= 0) {
continue
}
// If the attribute of the object or the element of the array is not a simple primitive
if (this.isObject(source[key]) || Array.isArray(source[key])) {
if (Array.isArray(source[key])) {
additions[key] = []
modifications[key] = []
} else {
additions[key] = {}
modifications[key] = {}
}
// Deep compare Array if the same element is there in source and target,
if (Array.isArray(source[key]) && Array.isArray(target[key])) {
if (source[key].length !== target[key].length) {
modifications[key] = [...source[key]]
}
const visited = {}
const temp = [...source[key], ...target[key]]
for (const a of temp) {
if (this.isObject(a)) {
if (visited[a.name]) {
// Common array in target and source
modifications[key].push({})
additions[key].push({})
this.compareDeep(a, visited[a.name], additions[key][additions[key].length - 1], modifications[key][modifications[key].length - 1])
// Any addtions for the matching key must be moved to modifications
if (!this.isEmpty(additions[key])) {
modifications[key] = modifications[key].concat(additions[key])
}
// Add name attribute to the modifications to make it look better ; it won't be added otherwise as it would be the same
if (!this.isEmpty(modifications[key][modifications[key].length - 1])) {
Object.assign(modifications[key][modifications[key].length - 1], { name: a.name })
}
delete visited[a.name]
continue
} else if (visited[a.username]) {
// Common array in target and source
modifications[key].push({})
additions[key].push({})
this.compareDeep(a, visited[a.username], additions[key][additions[key].length - 1], modifications[key][modifications[key].length - 1])
if (!this.isEmpty(additions[key])) {
modifications[key] = modifications[key].concat(additions[key])
}
// Add username attribute to the modifications to make it look better ; it won't be added otherwise as it would be the same
if (!this.isEmpty(modifications[key][modifications[key].length - 1])) {
Object.assign(modifications[key][modifications[key].length - 1], { username: a.username })
}
delete visited[a.username]
continue
}
if (a.name) {
visited[a.name] = a
} else if (a.username) {
visited[a.username] = a
}
} else {
// If already seen this, it is not a missing field
if (visited[a]) {
delete visited[a]
continue
}
visited[a] = a
}
}
const combined = []
for (const fields of Object.keys(visited)) {
combined.push(visited[fields])
}
// Elements that are in target are not additions
additions[key] = combined.filter(value => !target[key].includes(value))
} else {
// recursively compare the objects
this.compareDeep(target[key], source[key], additions[key], modifications[key])
}
} else {
if (target[key] === undefined) {
if (source[key]) {
additions[key] = source[key]
}
} else if (target[key] !== source[key]) {
modifications[key] = source[key]
// retroactively add `name` or `username` to the modifications
if (source.name) {
this.log.debug(`Adding name for ${key} ${source[key]}`)
modifications.name = source.name
} else if (source.username) {
modifications.username = source.username
}
}
}
modifications = this.removeEmptyAndNulls(modifications, key)
additions = this.removeEmptyAndNulls(additions, key)
}
let hasChanges = !this.isEmpty(additions) || !this.isEmpty(modifications)
// Indicate changes when source is empty and target is not
hasChanges |= this.isEmpty(source) && !this.isEmpty(target)
return ({ additions, modifications, hasChanges })
}
validateOverride (key, baseconfig, overrideconfig) {
if (this.overridevalidators[key]) {
// this.log.debug(`Calling overridevalidator for key ${key} `)
if (!this.overridevalidators[key].canOverride(baseconfig, overrideconfig)) {
this.log.error(`Error in calling overridevalidator for key ${key} ${this.overridevalidators[key].error}`)
throw new Error(this.overridevalidators[key].error)
}
}
}
validateConfig (key, baseconfig) {
if (this.configvalidators[key]) {
// this.log.debug(`Calling configvalidator for key ${key} `)
if (!this.configvalidators[key].isValid(baseconfig)) {
this.log.error(`Error in calling configvalidator for key ${key} ${this.configvalidators[key].error}`)
throw new Error(this.configvalidators[key].error)
}
}
}
mergeEmptyTarget (target, source) {
if (Array.isArray(source)) {
target = Array.isArray(target) ? target.concat(source) : source
} else {
target = Object.assign({}, source)
}
return target
}
mergeDeep (immutabletarget, ...sources) {
let target = Object.assign({}, immutabletarget)
while (sources.length) {
const source = sources.shift()
if (target === undefined || this.isEmpty(target)) {
target = this.mergeEmptyTarget(target, source)
continue
}
for (const key in source) {
// If the attribute of the object or the element of the array is not a simple primitive
if (this.isObject(source[key]) || Array.isArray(source[key])) {
// Deep merge Array so that if the same element is there in source and target,
// override the target with source otherwise include both source and target elements
if (Array.isArray(source[key]) && Array.isArray(target[key])) {
const combined = mergeBy(key, this.configvalidators[key], this.overridevalidators[key], NAME_FIELDS, target[key], source[key])
Object.assign(target, {
[key]: combined
})
} else {
this.validateOverride(key, target[key], source[key])
target[key] = this.mergeDeep(target[key], source[key])
this.validateConfig(key, target[key])
}
} else {
// Not calling validators when target[key] is primitive or empty
target[key] = source[key]
}
}
}
return target
}
removeEmptyAndNulls (modifications, key) {
if (Array.isArray(modifications[key])) {
modifications[key] = modifications[key].filter(k => {
return !this.isEmpty(k)
})
}
if (this.isEmpty(modifications[key])) {
delete modifications[key]
} else {
if (Array.isArray(modifications)) {
modifications.push(modifications[key])
delete modifications[key]
}
}
if (Array.isArray(modifications)) {
modifications = modifications.filter(k => {
return !this.isEmpty(k)
})
}
return modifications
}
}
MergeDeep.NAME_FIELDS = NAME_FIELDS
module.exports = MergeDeep