Skip to content

Instantly share code, notes, and snippets.

@jonjaques
Last active October 25, 2017 06:04
Show Gist options
  • Select an option

  • Save jonjaques/3036701 to your computer and use it in GitHub Desktop.

Select an option

Save jonjaques/3036701 to your computer and use it in GitHub Desktop.

Revisions

  1. jonjaques revised this gist Dec 10, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extend.js
    Original file line number Diff line number Diff line change
    @@ -89,7 +89,7 @@
    clone = src && isPlainObject(src) ? src : {};
    }
    // Never move original objects, clone them
    target[ name ] = this.extend( deep, clone, copy );
    target[ name ] = extend( deep, clone, copy );

    // Don't bring in undefined values
    } else if ( copy !== undefined ) {
  2. jonjaques renamed this gist Jan 2, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. jonjaques revised this gist Mar 20, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions extender.js
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@
    return type(obj) === "array";
    }
    // Make this public
    this.extend = function() {
    function extend() {
    var options, name, src, copy, copyIsArray, clone,
    target = arguments[0] || {},
    i = 1,
    @@ -103,5 +103,5 @@
    return target;
    };

    return global.__extend = this.extend;
    return global.__extend = extend;
    })(window);
  4. jonjaques revised this gist Mar 20, 2013. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions extender.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    (function Extender(){
    function type(obj){
    (function(global, undefined) {
    function type(obj) {
    var checker = {};
    var types = "Boolean Number String Function Array Date RegExp Object".split(" ");
    for(var i in types){
    @@ -9,13 +9,13 @@
    String( obj ) :
    checker[ Object.prototype.toString.call(obj) ] || "object";
    }
    function isFunction(obj){
    function isFunction(obj) {
    return type(obj) === "function";
    }
    function isWindow(obj){
    function isWindow(obj) {
    return obj != null && obj == obj.window;
    }
    function isPlainObject( obj ) {
    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.
    @@ -103,5 +103,5 @@
    return target;
    };

    return window.__extend = this.extend;
    })();
    return global.__extend = this.extend;
    })(window);
  5. jonjaques revised this gist Mar 19, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    function myFunction(opts) {
    var defaults = {
    param1: 'foo',
    param2: 'bar'
    };
    var options = __extend(defaults, opts);
    return options;
    }

    myFunction({param2: 'baz'}); // { param1: 'foo', param2: 'baz' }
  6. jonjaques revised this gist Mar 19, 2013. 2 changed files with 3 additions and 7 deletions.
    4 changes: 0 additions & 4 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +0,0 @@
    // Use it like this
    var ___extend = new Extender();

    var newObj = ___extend(orig, diff);
    6 changes: 3 additions & 3 deletions extender.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    function Extender(){
    (function Extender(){
    function type(obj){
    var checker = {};
    var types = "Boolean Number String Function Array Date RegExp Object".split(" ");
    @@ -103,5 +103,5 @@ function Extender(){
    return target;
    };

    return this.extend;
    }
    return window.__extend = this.extend;
    })();
  7. jonjaques revised this gist Nov 6, 2012. 2 changed files with 5 additions and 6 deletions.
    4 changes: 4 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    // Use it like this
    var ___extend = new Extender();

    var newObj = ___extend(orig, diff);
    7 changes: 1 addition & 6 deletions extender.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,3 @@
    // Use it like this
    var ext = new Extender();

    var newObj = ext.extend(orig, diff);

    function Extender(){
    function type(obj){
    var checker = {};
    @@ -109,4 +104,4 @@ function Extender(){
    };

    return this.extend;
    }
    }
  8. jonjaques revised this gist Nov 6, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions extender.js
    Original file line number Diff line number Diff line change
    @@ -107,4 +107,6 @@ function Extender(){
    // Return the modified object
    return target;
    };

    return this.extend;
    }
  9. jonjaques revised this gist Jul 3, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extender.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // Use it like this
    var ext = new Extender();

    new newObj = ext.extend(orig, diff);
    var newObj = ext.extend(orig, diff);

    function Extender(){
    function type(obj){
  10. jonjaques created this gist Jul 3, 2012.
    110 changes: 110 additions & 0 deletions extender.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    // Use it like this
    var ext = new Extender();

    new newObj = ext.extend(orig, diff);

    function Extender(){
    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
    this.extend = function() {
    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 ] = this.extend( deep, clone, copy );

    // Don't bring in undefined values
    } else if ( copy !== undefined ) {
    target[ name ] = copy;
    }
    }
    }
    }

    // Return the modified object
    return target;
    };
    }