diff-json

2.0.0 • Public • Published

diff-json

Build Status Downloads

A diff tool for javascript objects inspired by https://github.com/eugeneware/changeset.

Features

diff

If a key is specified for an embedded array, the diff will be generated based on the objects have same keys.

Examples:

 
  var changesets = require('diff-json');
  var newObj, oldObj;
 
  oldObj = {
    name: 'joe',
    age: 55,
    coins: [2, 5],
    children: [
      {name: 'kid1', age: 1},
      {name: 'kid2', age: 2}
    ]};
 
  newObj = {
    name: 'smith',
    coins: [2, 5, 1],
    children: [
      {name: 'kid3', age: 3},
      {name: 'kid1', age: 0},
      {name: 'kid2', age: 2}
    ]};
 
 
  # Assume children is an array of child object and the child object has 'name' as its primary key
  diffs = changesets.diff(oldObj, newObj, {children: 'name'});
 
  expect(diffs).to.eql([
    {
      type: 'update', key: 'name', value: 'smith', oldValue: 'joe'
    },
    {
      type: 'update', key: 'coins', embededKey: '$index', changes: [
          {type: 'add', key: '2', value: 1 }
        ]
    },
    {
      type: 'update',
      key: 'children',
      embededKey: 'name',
      changes: [
        {
          type: 'update', key: 'kid1', changes: [
            {type: 'update', key: 'age', value: 0, oldValue: 1 }
          ]
        },
        {
          type: 'add', key: 'kid3', value: {name: 'kid3', age: 3 }
        }
      ]
    },
    {
      type: 'remove', key: 'age', value: 55
    }
  ]);

applyChange

Examples:

 
  var changesets = require('diff-json');
  var oldObj = {
    name: 'joe',
    age: 55,
    coins: [2, 5],
    children: [
      {name: 'kid1', age: 1},
      {name: 'kid2', age: 2}
    ]};
 
 
  # Assume children is an array of child object and the child object has 'name' as its primary key
  diffs = [
    {
      type: 'update', key: 'name', value: 'smith', oldValue: 'joe'
    },
    {
      type: 'update', key: 'coins', embededKey: '$index', changes: [
          {type: 'add', key: '2', value: 1 }
        ]
    },
    {
      type: 'update',
      key: 'children',
      embededKey: 'name', // The key property name of the elements in an array
      changes: [
        {
          type: 'update', key: 'kid1', changes: [
            {type: 'update', key: 'age', value: 0, oldValue: 1 }
          ]
        },
        {
          type: 'add', key: 'kid3', value: {name: 'kid3', age: 3 }
        }
      ]
    },
    {
      type: 'remove', key: 'age', value: 55
    }
  ]
 
  changesets.applyChanges(oldObj, diffs)
  expect(oldObj).to.eql({
    name: 'smith',
    coins: [2, 5, 1],
    children: [
      {name: 'kid3', age: 3},
      {name: 'kid1', age: 0},
      {name: 'kid2', age: 2}
    ]});
 

revertChange

Examples:

 
  var changesets = require('diff-json');
 
  var newObj = {
    name: 'smith',
    coins: [2, 5, 1],
    children: [
      {name: 'kid3', age: 3},
      {name: 'kid1', age: 0},
      {name: 'kid2', age: 2}
   ]};
 
  # Assume children is an array of child object and the child object has 'name' as its primary key
  diffs =  [
    {
      type: 'update', key: 'name', value: 'smith', oldValue: 'joe'
    },
    {
      type: 'update', key: 'coins', embededKey: '$index', changes: [
          {type: 'add', key: '2', value: 1 }
        ]
    },
    {
      type: 'update',
      key: 'children',
      embededKey: 'name', // The key property name of the elements in an array
      changes: [
        {
          type: 'update', key: 'kid1', changes: [
            {type: 'update', key: 'age', value: 0, oldValue: 1 }
          ]
        },
        {
          type: 'add', key: 'kid3', value: {name: 'kid3', age: 3 }
        }
      ]
    },
    {
      type: 'remove', key: 'age', value: 55
    }
  ]
 
  changesets.revertChanges(newObj, diffs)
  expect(newObj).to.eql {
    name: 'joe',
    age: 55,
    coins: [2, 5],
    children: [
      {name: 'kid1', age: 1},
      {name: 'kid2', age: 2}
    ]};
 

Get started

npm install diff-json

Run the test

npm run test

Licence

The MIT License (MIT)

Copyright (c) 2013 viruschidai@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i diff-json

Weekly Downloads

47,280

Version

2.0.0

License

MIT

Unpacked Size

23.3 kB

Total Files

6

Last publish

Collaborators

  • runk
  • viruschidai