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; }