Last active
August 29, 2015 14:25
-
-
Save husa/75097fbadc0a6a6b1b84 to your computer and use it in GitHub Desktop.
extend. the one and only
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
function extend () { | |
return [].reduce.call(arguments, function (prev, current) { | |
for (var prop in current) { | |
if (!current.hasOwnProperty(prop)) continue; | |
if (/^(object|array)$/.test(type(current[prop])) && current[prop] !== current) { | |
if (!/^(object|array)$/.test(type(prev[prop]))) { | |
prev[prop] = type(current[prop]) === 'array' ? [] : {}; | |
} | |
prev[prop] = extend(prev[prop], current[prop]); | |
} else { | |
prev[prop] = current[prop]; | |
} | |
} | |
return prev; | |
}, {}); | |
} | |
function type(o) {return Object.prototype.toString.call(o).match(/\[object (.*?)\]/)[1].toLowerCase();} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To be used with type.js