Created
September 15, 2010 12:13
-
-
Save teemow/580651 to your computer and use it in GitHub Desktop.
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
// From jQuery.extend in the jQuery JavaScript Library v1.3.2 | |
// Copyright (c) 2009 John Resig | |
// Dual licensed under the MIT and GPL licenses. | |
// http://docs.jquery.com/License | |
// Modified for node.js (formely for copying properties correctly) | |
exports.merge = function() { | |
// copy reference to target object | |
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, source; | |
// Handle a deep copy situation | |
if ( typeof target === "boolean" ) { | |
deep = target; | |
target = arguments[1] || {}; | |
// skip the boolean and the target | |
i = 2; | |
} | |
// Handle case when target is a string or something (possible in deep copy) | |
if ( typeof target !== "object" && !(typeof target === 'function') ) | |
target = {}; | |
// mixin process itself if only one argument is passed | |
if ( length == i ) { | |
target = GLOBAL; | |
--i; | |
} | |
for ( ; i < length; i++ ) { | |
// Only deal with non-null/undefined values | |
if ( (source = arguments[i]) != null ) { | |
// Extend the base object | |
Object.getOwnPropertyNames(source).forEach(function(k){ | |
var d = Object.getOwnPropertyDescriptor(source, k) || {value: source[k]}; | |
if (d.get) { | |
target.__defineGetter__(k, d.get); | |
if (d.set) { | |
target.__defineSetter__(k, d.set); | |
} | |
} | |
else { | |
// Prevent never-ending loop | |
if (target === d.value) { | |
continue; | |
} | |
if (deep && d.value && typeof d.value === "object") { | |
target[k] = mixin(deep, | |
// Never move original objects, clone them | |
source[k] || (d.value.length != null ? [] : {}) | |
, d.value); | |
} | |
else { | |
target[k] = d.value; | |
} | |
} | |
}); | |
} | |
} | |
// Return the modified object | |
return target; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment