npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

merge-util

v0.3.1

Published

Deep merge object utility

Downloads

929

Readme

merge-util

Just another deep merge function for objects. Merge is recursive for nested objects by default.

Installation

$ component install cristiandouce/merge-util

or..

$ npm install merge-util

Usage

var merge = require('merge-util');

var obj1 = { hello: 'World' };
var obj2 = { hello: 'World!', good: { bye: 'Lennin' } };

merge(obj1,obj2);

console.log(obj1); // out: "{ hello: 'World!', good: { bye: 'Lennin'}}"

API

merge(obj1, obj2, opts)

Merge obj2 into obj1 with some options.

var obj1 = { obj: { any: 'thing' }};
var obj2 = { obj: { something: 'wrong?' } };
merge(obj1, obj2); // out: "{ obj: { something: 'Wrong?', any: 'thing' }}"

Available options include:

  • inheritance : Defaults to false. Include inherited members in the merge.

  • shallow : Defaults to false. A true value prevents merge from being recursive to nested objects.

  • discardEmpty : Defaults to true. A false value makes empty values (as defined by ianstormtaylor/is-empty) in the "right-hand" be preserved as they were instead of being merged as undefined attributes in the "left-hand" object.

Note: you can also pass a Boolean as the opts parameter as value for inheritance alone. This provides backwards compatibility with version 0.1.0

inheritance

function Being () {
  this.living = true;
}

Youth.prototype = new Being;
function Youth () {
  this.plays = true;
}

function Robot() {
  this.material = "metal";
}

var bot = new Robot();
var kid = new Youth();

merge(bot, kid, { inheritance: false }); // merge(dog, kid, false); also works as in 0.1.0
//out => bot: "{ plays: true, material: 'metal' }"

merge(bot, kid, { inheritance: true }); // merge(dog, kid, true); also works as in 0.1.0
//out => bot: "{ living: true, plays: true, material: 'metal' }"

shallow

var xBuster = { name: "X-Buster", power: 600 };
var zeroBlade = { name: "Zero Blade", power: 9001 }

var megaman = {
  name: "Megaman X",
  weapon: xBuster
}

var zero = {
  name: "Zero"
  weapon: zeroBlade
}


merge(megaman, zero); // same as merge(megaman, zero, { shallow: false });
//out => megaman: "{ name: "Zero", weapon: { name: "Zero Blade", power: 9001 } }"

log(xBuster);
//out: { name: "Zero Blade", power: 9001 }
// `xBuster` was merged with `zeroBlade`

//If instead we had done this...
merge(megaman, zero, { shallow: true });
//out => megaman: "{ name: "Zero", weapon: { name: "Zero Blade", power: 9001 } }"

log(xBuster);
//out: { name: "X-Buster", power: 600 }
// `megaman`'s' `weapon` was swapped for `zero`'s, but `xBuster` itself wasn't merged.

discardEmpty

var lameArmor = { name: "Lame armor", defense: 10 };
var upgradableArmor = { name: "Cool armor", defense: 20, upgrades: { } }

var noob = {
  name: "Noob player",
  armor: lameArmor
}

var veteran = {
  name: "Looking for upgrades"
  armor: upgradableArmor
}


merge(noob, veteran); // same as merge(noob, veteran, { discardEmpty: true });
//out =>
//noob:
//  { name: "Looking for uprades",
//    weapon: {
//      name: "Cool armor",
//      defense: 20,
//      upgrades: undefined
//    }
//  }
// `upgrades` attribute was discarded because it was an empty object.

merge(noob, veteran, { discardEmpty: false });
//out =>
//noob:
//  { name: "Looking for uprades",
//    weapon: {
//      name: "Cool armor",
//      defense: 20,
//      upgrades: { }
//    }
//  }
// `upgrades` attribute was merged as an empty object.

TODO

  • Add some tests.
  • Add more merge examples.

License

MIT