Tiny universal/isomorphic library for intelligently deep-merging objects
var nanomerge = require('nanomerge');
var merger = nanomerge({
strategy: { array: 'merge' }
});
//=> { a: [{ a: 1 }, { a: 3 }], b: 'It works!' }
merger({ a: [{ a: 2 }, { a: 3 }] }, { a: [{ a: 1 }] }, { b: 'It works!' });
-
Small. Only 1 kilobyte (minified and gzipped). Only "nano" dependencies. It uses Size Limit to keep the size under control.
-
Easy. Everything works out of the box. Just plug and play.
-
Customizability. But, if you need to, everything can be customized.
The lib supports Node.js and all browsers starting from IE 11.
npm install --save nanomerge
var nanomerge = require('nanomerge');
nanomerge({ a: 1, b: 2 }, { a: 2, c: 3 }, { b: 5, d: 7 }); //=> { a: 2, b: 5, c: 3, d: 7 }
If passed only one parameter, it is interpreted as configuration data, and a customized merge function is returned.
var nanomerge = require('nanomerge');
var myCustomMerge = nanomerge.create({ /* options */ });
myCustomMerge({ a: 1 }, { b: 2 }) //=> { a: 1, b: 2 }
var config = {
/**
* If true, each item will be deeply copied,
* otherwise it will be copied as needed
*/
force: true, // boolean
/**
* The strategy specifies how we should handle a particular type
*/
strategy: {
array: 'replace', // string: merge | replace | concat
object: 'deep', // string: deep
primitive: 'default', // string: default
},
/**
* Custom types allow you to create intelligent mechanisms for enterprises
*/
types: {
mode: 'add', // string: add | replace
list: [
{
name: "array", // Type name. The name must be unique.
is: function(el) { }, // Function checking whether an element is an object of type
default: "simple", // The name of the mechanism of merging the default
merge: { // The object contains all the mechanisms for draining this type
simple: function(merger, a, b, config) {},
}
},
/* You custom types */
],
},
};