merge lutils-merge
Merge javascript objects recursively.
npm install lutils-merge
API
merge
, merge.white
, merge.black
supports these patterns:
merge(obj1, obj2, obj3, ...)
merge([obj1, obj2, obj3, ...], options)
merge([obj1, obj2, obj3, ...], ({ key }) => key !== 'keyIReallyDontWant' )
merge([obj1, obj2, obj3, ...], options, ({ key }) => key !== 'keyIReallyDontWant' )
merge([target], [object], [...[object]])
Merges all objects into first object.
const obj1 = { a: 1, b: { c: 1 }, e: 3 }
const obj2 = { a: 2, b: { d: 2 } }
const obj3 = { e: () => {} }
merge(obj1, obj2, obj3)
merge([
{ a: { c: 1 } },
{
a: { b: 2 },
d: 1
}
], { depth: 1 })
merge.white()
Merges all objects into first object only if there is a key match.
merge.white(
{ a: 1 },
{
a: 2,
b: 2
}
)
merge.black()
Merges all objects into first object only if there isn't a key match.
merge.black(
{ a: 1 },
{ a: 2, b: 2 }
)
Advanced usage
Options
{
depth: 8,
types: { object: true, array: true }
types: [ "object", "array" ]
test: function(params) {}
}
Test Functions
When merging, a test function can be specified in two ways:
merge([obj1, obj2], () => {})
merge([obj1, obj2], { test: () => {} })
The test function is supplied paramaters for each iteration through the merge.
merge([obj1, obj2], (params) => {
params.iterated
params.key
params.obj1
params.obj2
params.depth
params.options
params.recursing
params.assigning
})
For example; merge.white
is defined with this test function:
function(params) {
if ( params.recursing ) return true
return params.key in params.obj2
}