All Projects → cosmicanant → Recursive Diff

cosmicanant / Recursive Diff

Licence: mit
A JavaScript library to find diff between two JavaScript Objects. Support for Array, Number, Date and other primitive data types.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Recursive Diff

Solidarity
Solidarity is an environment checker for project dependencies across multiple machines.
Stars: ✭ 540 (+660.56%)
Mutual labels:  node-module, javascript-library
bulksearch
Lightweight and read-write optimized full text search library.
Stars: ✭ 108 (+52.11%)
Mutual labels:  javascript-library, node-module
Rando.js
The world's easiest, most powerful random function.
Stars: ✭ 659 (+828.17%)
Mutual labels:  node-module, javascript-library
database-js
Common Database Interface for Node
Stars: ✭ 58 (-18.31%)
Mutual labels:  javascript-library, node-module
Flexsearch
Next-Generation full text search library for Browser and Node.js
Stars: ✭ 8,108 (+11319.72%)
Mutual labels:  node-module, javascript-library
Fine Uploader
Multiple file upload plugin with image previews, drag and drop, progress bars. S3 and Azure support, image scaling, form support, chunking, resume, pause, and tons of other features.
Stars: ✭ 8,158 (+11390.14%)
Mutual labels:  javascript-library
Push.js
The world's most versatile desktop notifications framework 🌎
Stars: ✭ 8,536 (+11922.54%)
Mutual labels:  javascript-library
Diver.js
Dives deep into the dom and dumps it in the object literal notation.
Stars: ✭ 57 (-19.72%)
Mutual labels:  javascript-library
Poet Js
Po.et JS is an small library that provides methods to easily create and sign Po.et Claims.
Stars: ✭ 56 (-21.13%)
Mutual labels:  javascript-library
Odiff
The fastest pixel-by-pixel image visual difference tool in the world.
Stars: ✭ 1,173 (+1552.11%)
Mutual labels:  diff
Web Audio Javascript Webassembly Sdk Interactive Audio
🌐 Superpowered Web Audio JavaScript and WebAssembly SDK for modern web browsers. Allows developers to implement low-latency interactive audio features into web sites and web apps with a friendly Javascript API. https://superpowered.com
Stars: ✭ 68 (-4.23%)
Mutual labels:  javascript-library
Python Patch
Library to parse and apply unified diffs
Stars: ✭ 65 (-8.45%)
Mutual labels:  diff
Fobo
FoBo - A Modular Front-End Toolkit module for Lift
Stars: ✭ 59 (-16.9%)
Mutual labels:  javascript-library
Node Ebml
EBML parser
Stars: ✭ 66 (-7.04%)
Mutual labels:  node-module
Evangelist
🌟 Library of helpers that are useful for functional programming
Stars: ✭ 58 (-18.31%)
Mutual labels:  javascript-library
Egeo
EGEO is the open-source UI library used to build Stratio's UI. It includes UI Components, Utilities, Services and much more to build user interfaces quickly and with ease. The library is distributed in AoT mode.
Stars: ✭ 69 (-2.82%)
Mutual labels:  javascript-library
Messageviewjs
Talking Scene JavaScript Library
Stars: ✭ 56 (-21.13%)
Mutual labels:  javascript-library
Branchsite
CLI tool for publishing your static website to a separate branch
Stars: ✭ 65 (-8.45%)
Mutual labels:  node-module
Ale
✌️a Flexible and fast JavaScript view framework
Stars: ✭ 67 (-5.63%)
Mutual labels:  diff
Share Selected Text
share selected text on twitter, buffer, and some others. Inspired by medium.com
Stars: ✭ 64 (-9.86%)
Mutual labels:  javascript-library

NPM Version NPM Downloads Build Coverage Status Dependency Status

Recursive-Diff

A JavaScript library (with TypeScript support) to find diff between two JS Objects/Array, support for complex nested JS Objects


This library can be used to get diff between two JS Objects/Arrays(or other primitive values). Diff are returned in the form of Array where each ARRAY item represents a change in the original Object/Array. A diff item can have following three properties:

  • path: An array representation of nested path
  • op: Can be any one of the following - add, update or delete
  • val: New value after change
const rdiff = require('recursive-diff');
const initialVal = {
  a: {
    b: 1,
    c: 2,
    d: [1]
  }
}
const changedVal = {
  a: {
    b: 2,
    d: [1, 2],
  },
};

const diff = rdiff.getDiff(initialVal, changedVal);
/***
Diff of initialVal and changedVal is: [
  {
    "path": [
      "a",
      "b"
    ],
    "op": "update",
    "val": 2
  },
  {
    "path": [
      "a",
      "c"
    ],
    "op": "delete"
  },
  {
    "path": [
      "a",
      "d",
      1
    ],
    "op": "add",
    "val": 2
  }
]
**/

const c = rdiff.applyDiff(initialVal, diff);
assert.deepEqual(c, changedVal);

Api details

  • getDiff(oldVal, newVal, keepOldVal?): getDiff takes following parameters

    • oldVal (required): initial value, can be Array/Object or even primitive type such as number, boolean or string
    • newVal (required): changed value ( required ), can be Array/Object or even primitive type such as number, boolean or string
    • keepOldValueInDiff (optional): boolean parameter which if set to true, every diff value will have an additional property oldValue which will denote the old value before mutation
  • applyDiff (oldVal, diff, visitorCallbackFn?) applyDiff takes three arguments:

    • oldVal (required): original value,
    • diff (required): diff returned by getDiff API
    • visitorCallbackFn (optional): This callback function is called at each depth level while applying the diff. It can be used to mark the mutation path with some meta properties eg: { isMutated: true }. For more details, please check the examples directory of this repo.

Using recursive diff library in Node

  • Install library using the command : npm install recursive-diff

  • sample code is given below

    const diff = require('recursive-diff');
    const oldVal = {a:1};
    const newVal = {a:2};
    const delta = diff.getDiff(oldVal, newVal);
    const ob3 = diff.applyDiff(oldVal, delta);
    assert.deepEqual(ob3, newVal);
    
    

Using recursive diff library in the Browser

'dist/recursive-diff.min.js' can be directly injected into a HTML page using the URL https://unpkg.com/[email protected]/dist/recursive-diff.min.js. Once it is included into the HTML file, diff API is accessible using window.recursiveDiff. Example given below.

<script type="text" src="https://unpkg.com/[email protected]/dist/recursive-diff.min.js"/>
<script type="text/javascript">
const oldVal = { a: 1 };
const newVal = { a: 2 };
const delta = recursiveDiff.getDiff(oldVal, newVal);
const ob3 = recursiveDiff.applyDiff(oldVal, delta); //expect ob3 is deep equal to newVal
</script>

Using recursive diff library in TypeScript

import { getDiff, applyDiff, rdiffResult } from 'recursive-diff';

const oldVal = [1, 2];
const newVal = [2, 3, 4];
const diff:rdiffResult[] = getDiff(oldVal, newVal);
console.log('diff', diff);
const final = applyDiff(oldVal, diff);
console.log('applydiff', final); // final should deep equal to newVal

Tests

Unit test can be run using the command npm test. This repo has more than 99% code coverage.

Examples

You can find more examples in the example folder of this repo. Few of the examples are listed below.

/* eslint-disable no-param-reassign */
/* eslint-disable no-console */
import { getDiff, applyDiff } from '../dist/recursive-diff';

let [a, b, c, delta] = [];
// testing primitive data type
a = 3;
b = 10;
delta = getDiff(a, b);
console.log(delta);
// Output: [{path: [], op: 'update', val: 10}]
c = applyDiff(a, delta);
console.log(c); // Output: 10

// testing array
a = [1, 2];
b = [1, 30, 40];
delta = getDiff(a, b, true); // third parameter : keepOldValInDiff, see output below
console.log(delta);
/** *
Output:
[
  { path: [1], op: 'update', val: 30, oldVal: 2 },
  { path: [2], op: 'add', val: 40 },
]
* */
c = applyDiff(a, delta);
console.log(c); // Output: [1,30,40]

// testing objects
a = {
  a: '10',
  b: '20',
  c: '30',
};
b = {
  a: '10',
  b: '40',
};
delta = getDiff(a, b);
console.log(delta);
/** * Output:
[
  { path: ['b'], op: 'update', val: 40 },
  { path: ['c'], op: 'delete', val: undefined },
]
* */
c = applyDiff(a, delta);
console.log(c); // Output: {a:'10', 'b':40}

// testing complex deep object
a = {
  b: [1, 2, [3, 4]],
  c: {
    c1: 20,
    c2: {
      c21: 'hello',
    },
    c3: 'India',
  },
};
b = {
  b: [1, 2, [4]],
  c: {
    c1: 20,
    c2: {
      c21: 'hi',
      c22: 'welcome',
    },
    c3: 'cosmic',
  },
};

delta = getDiff(a, b);
console.log(delta);
/**
Output:
[
  { path: ['b', 2, 0], op: 'update', val: 4 },
  { path: ['b', 2, 1], op: 'delete', val: undefined },
  { path: ['c', 'c2', 'c21'], op: 'update', val: 'hi' },
  { path: ['c', 'c2', 'c22'], op: 'add', val: 'welcome' },
  { path: ['c', 'c3'], op: 'update', val: 'cosmic' },
]
* */
c = applyDiff(a, delta);
console.log(c);
/** *Output
 {
    b: [1,2,[4]],
    c: {
        c1 : 20,
        c2 : {
            c21: 'hi',
            c22: 'welcome'
        },
        c3: 'cosmic'
    }
}
* */

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].