proxy-merge

1.0.0 • Public • Published

proxy-merge

npm version license CircleCI

Merge one or more objects using Proxy. Flatten a proxy-merged object to remove the proxy.

npm install -S proxy-merge

Examples

const proxyMerge = require('proxy-merge');

Merge two (or more...) objects.

const input0 = { foo: 'bar' };
const input1 = { age: 5 };
const obj = proxyMerge(input0, input1);
obj.foo; // => 'bar'
obj.age; // => 5

Only keys from the first input are listed with Object.keys(obj)...

Object.keys(obj); // => ['foo']

...but, the first input is not the output object.

input0 === obj; // => false

Changes made to the input after merging will be reflected by the proxy-merged object.

input0.change0 = 0;
input1.change1 = 1;
obj.change0; // => 0
obj.change1; // => 1

If you need a vanilla Object, use .flatten(obj).

const flat = proxyMerge.flatten(obj);
Object.keys(flat).sort(); // ['age', 'change0', 'change1', 'foo']
flat.age; // => 5

Changes made to the inputs are not reflected in the flattened object.

input0.change2 = 2;
obj.change2; // => 2
flat.change2; // => undefined

Dependencies (0)

    Dev Dependencies (9)

    Package Sidebar

    Install

    npm i proxy-merge

    Weekly Downloads

    341

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • ktonon