Skip to content

Instantly share code, notes, and snippets.

@sanex3339
Created April 8, 2016 08:36
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 sanex3339/596d053411bddb9890786e6eb023be19 to your computer and use it in GitHub Desktop.
Save sanex3339/596d053411bddb9890786e6eb023be19 to your computer and use it in GitHub Desktop.
/**
* @param destination
* @param sources
* @returns {any}
* @constructor
*/
export function DeepExtend (destination: any, ...sources: any[]): any {
let isObject: Function = (object: any) => {
return typeof object === 'object' && object !== null;
};
for (let source of sources) {
for (let prop in source) {
if (!source.hasOwnProperty(prop)) {
continue;
}
if (isObject(destination[prop]) || isObject(source[prop])){
if (!isObject(destination[prop]) || !isObject(source[prop])){
throw new Error('Trying to combine an object with a non-object (' + prop + ')');
} else {
destination[prop] = DeepExtend(JSON.parse(JSON.stringify(destination[prop])), source[prop]);
}
} else {
destination[prop] = source[prop];
}
}
}
return destination;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment