Created
March 30, 2016 14:37
-
-
Save andrienko/a563aa16e164b7e5cd9bf7ab46b2fd43 to your computer and use it in GitHub Desktop.
Yet another jquery-like deep extend implementation
This file contains hidden or 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
module.exports = (function () { | |
var extend_single = function (destination, source, deep) { | |
for (var key in source) { | |
if (source.hasOwnProperty(key)) { | |
if (deep && typeof destination[key] == 'object' && typeof source[key] == 'object') { | |
destination[key] = extend_single(destination[key], source[key], deep); | |
} else { | |
destination[key] = source[key]; | |
} | |
} | |
} | |
return destination; | |
} | |
var extend = function (is_deep) { | |
deep_extend = (is_deep && typeof deep_extend !== 'object') ? (is_deep === null ? false : true) : false; | |
var start = (typeof is_deep === 'object' && is_deep != null) ? 0 : 1; | |
if (arguments.length - start <= 0) return {}; | |
var to = arguments[start]; | |
for (var index = start + 1; index < arguments.length; index++) { | |
to = extend_single(to, arguments[index], deep_extend); | |
} | |
return to; | |
} | |
return extend; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment