import { IDeepMerge, IObject, isObject } from './isObject'; /** * @description Method to perform a deep merge of objects * @param {Object} target - The targeted object that needs to be merged with the supplied @sources * @param {Array} sources - The source(s) that will be used to update the @target object * @return {Object} The final merged object */ // merge objects keep the original object values export const deepMerge: IDeepMerge = (target: IObject, ...sources: IObject[]): IObject => { if (!sources.length) { return target; } const source = sources.shift(); if (isObject(target) && isObject(source)) { for (const key in source) { if (isObject(source[key])) { if (!target[key]) { Object.assign(target, { [key]: {} }); } deepMerge(target[key], source[key]); } else { Object.assign(target, { [key]: source[key] }); } } } return deepMerge(target, ...sources); };