nikolay-govorov / nanomerge

Tiny universal isomorphic library for deep merging objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nano Merge

Build status License Maintenance intention for this crate GitHub package version Code style: prettier

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.

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 nanomerge = require('nanomerge');

var myCustomMerge = nanomerge.create({ /* options */ });

myCustomMerge({ a: 1 }, { b: 2 }) //=> { a: 1, b: 2 }

Configuration

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 */
     ],
   },
};

About

Tiny universal isomorphic library for deep merging objects

License:MIT License


Languages

Language:JavaScript 100.0%