From 88fadbb4cca367a3e2fdb2867716ccb5ab14100a Mon Sep 17 00:00:00 2001 From: Craig Kochis Date: Wed, 7 Aug 2024 13:33:55 -0400 Subject: [PATCH] dont modify target object im mergeDeep --- src/util/object.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/util/object.ts b/src/util/object.ts index 2cfcf6d..68abf01 100644 --- a/src/util/object.ts +++ b/src/util/object.ts @@ -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; }