advance-json-merge
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i advance-json-merge

Weekly Downloads

1

Version

1.0.4

License

none

Unpacked Size

16.7 kB

Total Files

8

Last publish

Collaborators

  • jscon