merge-strategies
v0.3.2
Published
Object merging made simple
Downloads
7,528
Maintainers
Readme
merge-strategies
Object merging made simple.
Install
Usage
All functions return a new object -they are not mutated-, and take in objects of any type. If they receive scalars instead of Array
s or object
s, data
will be returned.
shallow(defaults, data)
- If both
defaults
anddata
are objects, they will be shallow merged. - Keys with
undefined
values in adata
object will acquire their value atdefaults
. - Mutations to the returned object won't have an effect over
defaults
. - Arrays won't be merged.
import { shallow } from 'merge-strategies';
// Returns: { foo: [3, 4], bar: { foo: 'foo' } }
shallow(
{ foo: [1, 2], bar: { baz: 'baz' } },
{ foo: [3, 4], bar: { foo: 'foo' } }
);
merge(defaults, data)
- If both
defaults
anddata
are objects, they will be deep merged. - Keys with
undefined
values in adata
object will acquire their value atdefaults
. - Mutations to the returned object won't have an effect over
defaults
. - Arrays won't be merged.
import { merge } from 'merge-strategies';
// Returns: { foo: [3, 4], bar: { baz: 'baz', foo: 'foo' } }
merge(
{ foo: [1, 2], bar: { baz: 'baz' } },
{ foo: [3, 4], bar: { foo: 'foo' } },
);
deep(defaults, data)
- If both
defaults
anddata
are objects, they will be deep merged. - Keys with
undefined
values in adata
object will acquire their value atdefaults
. - Mutations to the returned object won't have an effect over
defaults
. - Arrays will be concatenated.
import { deep } from 'merge-strategies';
// Returns: { foo: [1, 2, 3, 4], bar: { baz: 'baz', foo: 'foo' } }
deep(
{ foo: [1, 2], bar: { baz: 'baz' } },
{ foo: [3, 4], bar: { foo: 'foo' } },
);