Skip to content

Instantly share code, notes, and snippets.

@andrienko
Created March 30, 2016 14:37
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 andrienko/a563aa16e164b7e5cd9bf7ab46b2fd43 to your computer and use it in GitHub Desktop.
Save andrienko/a563aa16e164b7e5cd9bf7ab46b2fd43 to your computer and use it in GitHub Desktop.
Yet another jquery-like deep extend implementation
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