Skip to content

Commit

Permalink
add deepCloneExclude() and mergeArrayExclude()
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikaoto committed Feb 14, 2023
1 parent 134c075 commit 071d2c0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/utils/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,37 @@ export const clone = obj => {
return o
}

// Clone anything deeply but exclude keys given in 'exclude'
export const deepCloneExclude(obj, exclude = []) {
if (isArray(obj)) {
return obj.map(x => deepCloneExclude(x, exclude))
}

const o = {}
for (const k in obj) {
if (exclude.indexOf(k) > -1) continue

let v = obj[k]

if (k === 'extend' && isArray(v)) {
v = mergeArrayExclude(v, exclude)
}

if (isArray(v)) {
o[k] = v.map(x => deepClone(x, exclude))
} else if (isObject(v)) {
o[k] = deepClone(v, exclude)
} else o[k] = v
}

return o
}

// Merge array, but exclude keys listed in 'excl'
export const mergeArrayExclude = (arr, excl = []) => {
return arr.reduce((acc, curr) => deepMerge(acc, deepCloneExclude(curr, excl)), {})
}

/**
* Deep cloning of object
*/
Expand Down

0 comments on commit 071d2c0

Please sign in to comment.