0.1.3 • Published 6 years ago

ddiff v0.1.3

Weekly downloads
10
License
Apache-2.0
Repository
github
Last release
6 years ago

ddiff

npm (scoped) Travis Coveralls

Calculate differences between two data structures

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save ddiff

The UMD build is also available on jsdelivr:

<script src="https://cdn.jsdelivr.net/npm/ddiff/dist/ddiff.umd.js"></script>

You can find the library on window.ddiff.

Usage

import {diff} from 'ddiff';

const lhs = {
  name: 'my object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'elements']
  }
};

const rhs = {
  name: 'updated object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'more', 'elements', { than: 'before' }]
};

diff(lhs, rhs);
// [ { kind: 'E',
//     path: [ 'name' ],
//     lhs: 'my object',
//     rhs: 'updated object' },
//   { kind: 'A',
//     path: [ 'details', 'with' ],
//     index: 2,
//     item: { kind: 'E', path: [], lhs: 'elements', rhs: 'more' } },
//   { kind: 'A',
//     path: [ 'details', 'with' ],
//     index: 3,
//     item: { kind: 'N', rhs: 'elements' } },
//   { kind: 'A',
//     path: [ 'details', 'with' ],
//     index: 4,
//     item: { kind: 'N', rhs: { than: 'before' } } } ]

Changes

Change records have the following structure:

  • kind - indicates the kind of change; will be one of the following:
    • N - indicates a newly added property/element
    • D - indicates a property/element was deleted
    • E - indicates a property/element was edited
    • A - indicates a change occurred within an array
  • path - the property path (from the left-hand-side root)
  • lhs - the value on the left-hand-side of the comparison (undefined if kind === 'N')
  • rhs - the value on the right-hand-side of the comparison (undefined if kind === 'D')
  • index - when kind === 'A', indicates the array index where the change occurred
  • item - when kind === 'A', contains a nested change record indicating the change that occurred at the array index

Credits

The test suite is adapted from deep-diff. @flitbit Thanks for your amazing work.

Contributing

See the contributing file.

License

Apache License, Version 2.0 © Thiago Santos