0.2.0 • Published 6 years ago
nanomerge v0.2.0
Nano Merge
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 800 bytes (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.
Install
npm install --save nanomerge
Usage
Default settings
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 }
Custom merge mechanism
If passed only one parameter, it is interpreted as configuration data, and a customized merge function is returned.
var Merge = require('nanomerge/merge');
var myCustomMerger = new Merge({ /* options */ });
myCustomMerger.merge({ a: 1 }, { b: 2 }) //=> { a: 1, b: 2 }
Configuration
var config = {
/**
* 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) {},
}
},
/* You custom types */
],
},
};