advance-json-merge
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) [![npm version](https://badge.fury.io/js/advance-json-merge.svg)](https://badge.fury.io/js/advance-json-merge)
Last updated a year ago by jscon .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install advance-json-merge 
SYNC missed versions from official npm registry.

advance-json-merge

MIT Licence npm version

Advance json merge

  • written in Typescript
  • combination Object.assign method
  • can merge with any level

Installation

Node.js / Browserify

npm install advance-json-merge --save
var advanceMerge = require('advance-json-merge');

Global object

Include the pre-built script.

<script src="./dist/index.umd.min.js"></script>

usage


var { mergeWithLevel } = require('advance-json-merge');
const origin = {
      e: {
        a: {
          c: 'o_c',
          d: 'o_d'
        },
        b: {
          e: 'o_e'
        },
        c: {
          h: 'o_h'
        },
        d: {
          j: 'o_j'
        }
      },
      f: 1
    };
const target = {
      e: {
        a: {
          a: 't_a'
        },
        b: {
          e: 't_e',
          f: 't_f'
        },
        d: 2
      },
      f: {
        a: {},
        b: {
          c: 't_c',
          h: 't_h'
        }
      }
    };

// level 0 merge
    const result = mergeWithLevel(origin, target, 0);
    expect(result).toEqual(target);

// level 1 merge
    const result = mergeWithLevel(origin, target, 1);
    expect(result).toEqual(Object.assign({}, origin, target));

// level 2 merge
    const result = mergeWithLevel(origin, target, 2);
    expect(result).toEqual({
      e: {
        a: {
          a: 't_a'
        },
        b: {
          e: 't_e',
          f: 't_f'
        },
        c: {
          h: 'o_h'
        },
        d: 2
      },
      f: {
        a: {},
        b: {
          c: 't_c',
          h: 't_h'
        }
      }
    });

// level 3+ merge
    const levelNum = chance.integer({ min: 3, max: 1000 });
    const result = mergeWithLevel(origin, target, levelNum);
    expect(result).toEqual({
      e: {
        a: {
          a: 't_a',
          c: 'o_c',
          d: 'o_d'
        },
        b: {
          e: 't_e',
          f: 't_f'
        },
        c: {
          h: 'o_h'
        },
        d: 2
      },
      f: {
        a: {},
        b: {
          c: 't_c',
          h: 't_h'
        }
      }
    });  

使用说明

  • 本工具并不会分析数组内部的对象属性,因此如果键值对的值是 数组,此时你将数组当成诸如 int 等原始值去理解;

Build & test

npm run build
npm test

document

npm run doc

then open the generated out/index.html file in your browser.

License

MIT.

Current Tags

  • 1.0.4                                ...           latest (a year ago)

4 Versions

  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (25)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |