putil-merge
A 'swiss army knife' solution for merging multiple objects into one. It supports deep merge, cloning objects, copying descriptors and filtering.
Installation
$ npm install putil-merge --save
Usage
merge([config], target, [source])
- config [
Object
]
- deep [
Boolean
]: If true, it performs deep merge operation. - clone [
Boolean
]: If true, it clones objects except setting references - descriptor [
Boolean
]: If true, it copies descriptors - filter [
Function(obj, key, value)
]
- target [
Object
] - source [
Object|Array<Object>
]
Sequential calling
It supports sequential calling style.
merge.(option).(option)...(target, source)
Etc:
merge(target, source)
merge.deep(target, source)
merge.deep.clone(target, source)
merge.clone.deep(target, source)
merge.descriptor(target, source)
merge.deep.descriptor(target, [source1, source2])
merge.clone.descriptor(target, source)
merge.deep.clone.descriptor(target, [source1, source2])
merge.deep.clone.descriptor(target, source)
merge.clone.deep.descriptor(target, source)
merge.descriptor.clone.deep(target, source)
merge.descriptor.filter(filterfn)(target, source)
Examples
Merge source object over target object:
const a = {prm1: 1, prm2: 2};
const b = {prm1: 11, prm3: [1, 2, 3, 4]};
var merged = merge(a, b);
Clone any object:
const a = {prm1: 1, prm2: { prm3: 3}};
var cloned = merge.clone(a);
var deepCloned = merge.deep.clone(a);
Merge source object over target object with custom filtering:
var a = {id: 1};
var b = {name: 'John', surname: 'Wick'};
var merged = merge.deep.filter(function(o,k,v){
return k === 'name';
})(target, source);
Node Compatibility
License
MIT