diff-immutability-helper

Creates a diff between 2 javascript objects / variables, in a format that is compatible with immutability-helper

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
diff-immutability-helper
201.1.07 years ago7 years agoMinified + gzip package size for diff-immutability-helper in KB

Readme

diff-immutability-helper
Creates a diff between 2 JavaScript objects, allowing you to mutate one object to another using immutability-helper

!npm packagenpm-badgenpm-link !Build Statusbuild-badgebuild-link !Coverage Statuscoveralls-badgecoveralls-link

Overview

diff-immutability-helper creates an immutability-helper compatible diff object between 2 JavaScript variables.
This diff object would then allow you to mutate the base object to the target object.

Installation

$ npm install diff-immutability-helper --save

Example

Given:
import diff from 'diff-immutability-helper';

const base = {
  a: [1, 2, { b: 1 }, 4, 5, 6],
  b: 'test',
  c: 'prev'
};

const target = {
  a: [1, 2, { b: 2 }, 4, 6],
  b: 'test 2',
  d: 'new'
};

const change = diff(base, target);

will give a result of:
const change = {
  a: {
    $splice: [[4, 1], [2, 1, { b: 2 }]]
  },
  b: {$set: 'test 2'},
  $apply: (v) => omit(v, ['c']),
  $merge: {
    d: 'new'
  }
}

thus, we can then do:
import update from 'immutability-helper';
update(base, change); // to match target

Notes

  • Array diffing uses LCS
  • Only CommonJS format (for node.js) is provided. This is to minimize library size when you bundle with your application.

License

diff-immutability-helper is MIT licensed