Created
April 8, 2016 08:36
-
-
Save sanex3339/596d053411bddb9890786e6eb023be19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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