Skip to content
Merged
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
37 changes: 29 additions & 8 deletions src/specmap/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,38 @@ function applyPatch(obj, patch, opts) {
}
else if (patch.op === 'mergeDeep') {
const currentValue = getInByJsonPath(obj, patch.path)
const origValPatchValue = Object.assign({}, currentValue)
deepExtend(currentValue, patch.value)

// deepExtend doesn't merge arrays, so we will do it manually
// Iterate the properties of the patch
for (const prop in patch.value) {
if (Object.prototype.hasOwnProperty.call(patch.value, prop)) {
const propVal = patch.value[prop]
if (Array.isArray(propVal)) {
const existing = origValPatchValue[prop] || []
currentValue[prop] = existing.concat(propVal)
const propVal = patch.value[prop]
const isArray = Array.isArray(propVal)
if (isArray) {
// deepExtend doesn't merge arrays, so we will do it manually
const existing = currentValue[prop] || []
currentValue[prop] = existing.concat(propVal)
}
else if (isObject(propVal) && !isArray) {
// If it's an object, iterate it's keys and merge
// if there are conflicting keys, merge deep, otherwise shallow merge
const existing = currentValue[prop] || {}
for (const key in propVal) {
if (Object.prototype.hasOwnProperty.call(existing, key)) {
// if there is a single conflicting key, just deepExtend the entire value
// and break from the loop (since all future keys are also merged)
// We do this because we can't deepExtend two primitives
// (existing[key] & propVal[key] may be primitives)
deepExtend(existing, propVal)
break
}
else {
Object.assign(existing, {[key]: propVal[key]})
}
}
currentValue[prop] = existing
}
else {
// It's a primitive, just replace existing
currentValue[prop] = propVal
}
}
}
Expand Down