advance-json-merge
v1.0.4
Published
[![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)
Downloads
23
Readme
advance-json-merge
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.