Git Product home page Git Product logo

object-extend's Introduction

object-extend

A well-tested function to deep extend (or merge) JavaScript objects without further dependencies.

Unit-tested with over 94 single assertion tests.

Usage

var a = {
    app: 'My App',
    environment: 'development',
    connections: {
        default: 'mysql',
        mysql: {
            driver: 'mysql',
            host: 'localhost',
            database: 'myapp.dev'
        }
    }
};

var b = {
    environment: 'production',
    connections: {
        mysql: {
            database: 'myapp.prod'
        }
    }
};

extend(a, b);

// Result:
{
    app: 'My App',
    environment: 'production',
    connections: {
        default: 'mysql',
        mysql: {
            driver: 'mysql',
            host: 'localhost',
            database: 'myapp.prod'
        }
    }
}

Limitations

Array: Only actual objects are extended. Arrays will not be merged but overwritten.

var a { foo: [1, 2, 3] };
var b { foo: [4, 5, 6] };

extend(a, b); // { foo: [4, 5, 6] }

Notes

This function extends only enumerable properties without going up the prototype chain.

License

MIT.

object-extend's People

Stargazers

Divine avatar James Yang avatar Matthew avatar kevin anderson avatar  avatar Keith Williams avatar Atanas Minev avatar Andreas Klein avatar Erich Nascimento avatar Matt Mueller avatar

Watchers

James Cloos avatar Bernhard Wanger avatar

Forkers

andersonk17474

object-extend's Issues

ServiceNow Rhino fail

This is nice little function. I ran into an issue when trying to use this in a server-side environment. This is directed at a tiny user group, but it may help someone. The javascript Rhino engine ServiceNow uses is rather old. Like pre ECMA 3. This function needs an extra check, as the first line checking the object type for param 'a' will not copy an object from 'b' when the key doesn't exist in 'a'.

if ((Object.prototype.toString.call(a[key]) == '[object Object]') && (!a.hasOwnProperty(key))){
       a[key] = extend({}, b[key]);
}
else if (Object.prototype.toString.call(a[key]) != '[object Object]') {
      a[key] = b[key];
 } else {
       a[key] = extend(a[key], b[key]);
  }

redacted

redacted for security reason.

A faster way to extend

This isn't really an issue but I figured I'd let you know what I found on the js perf tests when using a for loop instead of a forEach

Here is the js perf tests http://jsperf.com/deep-extend-comparison

function extend(a, b){
 // Don't touch 'null' or 'undefined' objects.
 if(!a || !b){
  return a;
 }

 var keys = Object.keys(b);

 for (var i = 0, l = keys.length; i < l; i++){
  var key = keys[i];

  // Detect object without array, date or null.
  if(Object.prototype.toString.call(b[key]) === "[object Object]"){
   if(Object.prototype.toString.call(a[key]) !== "[object Object]"){
    a[key] = b[key];
   }else{
    a[key] = extend(a[key], b[key]);
   }
  }else{
   a[key] = b[key];
  }
 }
 return a;
};

Contact

Hi team,

I've tried to contact you in mail, but there was no response, can you please contact me ?

[email protected]

Thanks,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.