@securecall/client-component
Version:
SecureCall Core Web Component
31 lines (30 loc) • 1 kB
JavaScript
/**
* Performs a deep merge of objects and returns new object. Does not modify
* objects (immutable) and merges arrays via concatenation.
*
* @param {...object} objects - Objects to merge
* @returns {object} New object with merged key/values
*/
export function mergeDeep(...objects) {
const isObject = (obj) => obj && typeof obj === 'object';
return objects.reduce((prev, obj) => {
Object.keys(obj).forEach(key => {
const pVal = prev[key];
const oVal = obj[key];
if (oVal === undefined) {
return; // Skip undefined values
}
if (Array.isArray(pVal) && Array.isArray(oVal)) {
prev[key] = pVal.concat(oVal);
}
else if (isObject(pVal) && isObject(oVal)) {
prev[key] = mergeDeep(pVal, oVal);
}
else {
prev[key] = oVal;
}
});
return prev;
}, {});
}
//# sourceMappingURL=utils.js.map