Skip to content

Instantly share code, notes, and snippets.

@andrienko

andrienko/extend.js

Created Mar 30, 2016
Embed
What would you like to do?
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