Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions lib/mergeDeep.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,19 @@ class MergeDeep {
}
}
const combined = []
for (const fields of Object.keys(visited)) {
combined.push(visited[fields])
}
// Elements that are not in target are additions
additions[key] = combined.filter(item => {
if (this.isObjectNotArray(item)) {
return !target.some(targetItem => GET_NAME_USERNAME_PROPERTY(item) === GET_NAME_USERNAME_PROPERTY(targetItem))
} else {
return !target.includes(item)
if (Object.keys(visited).length !== 0) {
for (const fields of Object.keys(visited)) {
combined.push(visited[fields])
}
})

// Elements that are not in target are additions
additions[key] = combined.filter(item => {
if (this.isObjectNotArray(item)) {
return !target.some(targetItem => GET_NAME_USERNAME_PROPERTY(item) === GET_NAME_USERNAME_PROPERTY(targetItem))
} else {
return !target.includes(item)
}
})
}
// Elements that not in source are deletions
if (combined.length > 0) {
// Elements that not in source are deletions
Expand All @@ -247,8 +248,21 @@ class MergeDeep {
this.compareDeep(a, visited[id], additions[additions.length - 1], modifications[modifications.length - 1], deletions[deletions.length - 1])
}
// Any addtions for the matching key must be moved to modifications
const lastAddition = additions[additions.length - 1]
const lastModification = modifications[modifications.length - 1]

if (!this.isEmpty(additions)) {
modifications = modifications.concat(additions)
for (const key in lastAddition) {
if (!lastModification[key]) {
lastModification[key] = Array.isArray(lastAddition[key]) ? [] : {}
}
if (!Array.isArray(lastAddition[key])) {
Object.assign(lastModification[key], lastAddition[key])
} else {
lastModification[key].push(...lastAddition[key])
}
}
additions.length = 0
}
// 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[modifications.length - 1])) {
Expand Down
Loading