Skip to content

Instantly share code, notes, and snippets.

@palanisamym14
Created March 30, 2022 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palanisamym14/5168a8260c75177ac35fea3376ec1583 to your computer and use it in GitHub Desktop.
Save palanisamym14/5168a8260c75177ac35fea3376ec1583 to your computer and use it in GitHub Desktop.
/**
* Simple object check.
* @param item
* @returns {boolean}
*/
export function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
export function mergeDeep(target, ...sources) {
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]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeDeep(target, ...sources);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment