Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@broofa/merge

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@broofa/merge

Merge immutable JSON data structures to allow for identity (===) comparisons on deeply-equal subtrees

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by300%
Maintainers
1
Weekly downloads
 
Created
Source

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, before and after
  • 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] where assert.deepEqual(before[X], state[X]) passes
  • state[X] === after[X] where no part of after[X] is equal to before[X]
  • state[X] === (new Object/Array) when some, but not all state w/in [X] has changed

Installation

npm i @broofa/merge

Example

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]; // ⇨ true


Markdown generated from src/README_js.md by RunMD Logo

Keywords

FAQs

Package last updated on 07 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc