Skip to content

Commit

Permalink
fix: add or update fields in the existing environment or rulesets (#665)
Browse files Browse the repository at this point in the history
* feat: initial commit

* fix: merge deep code

* fix: lint

* fix: modification conditions

* fix: simplify conditions

---------

Co-authored-by: ls07667 <[email protected]>
  • Loading branch information
luvsaxena1 and ls07667 authored Aug 20, 2024
1 parent 19e2691 commit 776b31d
Show file tree
Hide file tree
Showing 2 changed files with 707 additions and 391 deletions.
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

0 comments on commit 776b31d

Please sign in to comment.