Skip to content

Instantly share code, notes, and snippets.

@jonjaques
Last active October 25, 2017 06:04
Show Gist options
  • Save jonjaques/3036701 to your computer and use it in GitHub Desktop.
Save jonjaques/3036701 to your computer and use it in GitHub Desktop.
Library agnostic version of jQuery's Extend
function myFunction(opts) {
var defaults = {
param1: 'foo',
param2: 'bar'
};
var options = __extend(defaults, opts);
return options;
}
myFunction({param2: 'baz'}); // { param1: 'foo', param2: 'baz' }
(function(global, undefined) {
function type(obj) {
var checker = {};
var types = "Boolean Number String Function Array Date RegExp Object".split(" ");
for(var i in types){
checker[ "[object " + types[i] + "]" ] = types[i].toLowerCase();
}
return obj == null ?
String( obj ) :
checker[ Object.prototype.toString.call(obj) ] || "object";
}
function isFunction(obj) {
return type(obj) === "function";
}
function isWindow(obj) {
return obj != null && obj == obj.window;
}
function isPlainObject(obj) {
var hasOwn = Object.prototype.hasOwnProperty;
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || type(obj) !== "object" || obj.nodeType || isWindow( obj ) ) {
return false;
}
try {
// Not own constructor property must be Object
if ( obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
return false;
}
} catch ( e ) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for ( key in obj ) {}
return key === undefined || hasOwn.call( obj, key );
}
function isArray(obj){
return type(obj) === "array";
}
// Make this public
function extend() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// 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" && !isFunction(target) ) {
target = {};
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( isPlainObject(copy) || (copyIsArray = isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false;
clone = src && isArray(src) ? src : [];
} else {
clone = src && isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[ name ] = extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
return global.__extend = extend;
})(window);
@jonjaques
Copy link
Author

@RemiArnaud thanks! Thats what I get for web editing.

@jonjaques
Copy link
Author

FYI to anybody seeing this, this is several years old by now, probably has a lot of junk for old IE thats not needed anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment