Skip to content

Commit

Permalink
dont modify target object im mergeDeep
Browse files Browse the repository at this point in the history
  • Loading branch information
kochis committed Aug 7, 2024
1 parent 58cb3e9 commit 88fadbb
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/util/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ export const filterUndefined = (object: any) => {
return obj;
};

// deep merge nested objects
// (arrays are concatenated
// deep merge nested objects - returns a new object
// (arrays are concatenated)
export const mergeDeep = (target: any, source: any = {}) => {
let output = Object.assign({}, target);
for (const key in source) {
if (source[key] instanceof Object && !Array.isArray(source[key]) && key in target) {
Object.assign(source[key], mergeDeep(target[key], source[key]));
output[key] = mergeDeep(target[key], source[key]);
} else if (Array.isArray(source[key]) && Array.isArray(target[key])) {
target[key] = target[key].concat(source[key]);
output[key] = target[key].concat(source[key]);
} else {
target[key] = source[key];
output[key] = source[key];
}
}
return target;
return output;
}

0 comments on commit 88fadbb

Please sign in to comment.