1.0.4 • Published 7 years ago
@broofa/merge v1.0.4
merge
Merge immutable model state (or any data structure, really), preserving references to unchanged nodes, such that the === operation can be used to determine where state has changed. (Useful when dealing with immutable data models such as React+Redux, where === is used for exactly this purpose.)
Specifically, given
- Two JSON data structures,
beforeandafter state = merge(before, after)
Then:
assert.deepEqual(state, after)always passes
And for any path, [X], to an Object or Array within state:
state[X] === before[X]whereassert.deepEqual(before[X], state[X])passesstate[X] === after[X]where no part ofafter[X]is equal tobefore[X]state[X] === (new Object/Array)when some, but not all state w/in[X]has changed
Installation
npm i @broofa/mergeExample
const assert = require('assert');
const merge = require('@broofa/merge');
const before = {
a: 'hello',
b: 123,
c: {ca: ['zig'], cb: [{a:1}, {b:2}]},
};
const after = {
a: 'world',
b: 123,
c: {ca: ['zig'], cb: [{a:99}, {b:2}]},
};
const state = merge(before, after);
assert.deepEqual(after, state); // Always true
// Where state HAS changed
state === before; // ⇨ false
state.c === before.c; // ⇨ false
state.c.cb === before.c.cb; // ⇨ false
state.c.cb[0] === before.c.cb[0]; // ⇨ false
// Where state HAS NOT changed
state.c.ca === before.c.ca; // ⇨ true
state.c.cb[1] === before.c.cb[1]; // ⇨ trueMarkdown generated from src/README_js.md by 