data-diff

0.0.2 • Public • Published

Data Diff

A library that calculates the changes from one json object to another. It was designed for figuring out the differences in data returned from the server so only the models that change would trigger updates to the views. It will work in both the browser and in node.

Example

First response from server:

[
  {
    "id": 1,
    "name": "James Brown",
    "status": 1,
    "counts": {
      "fingers": 5,
      "toes": 5
    }
  },
  {
    "id": 2,
    "name": "Jane Doe",
    "status": 1,
    "counts": {
      "fingers": 5,
      "toes": 5
    }
  }
]

Second response from server:

[
  {
    "id": 1,
    "name": "James Brown",
    "status": 1,
    "counts": {
      "fingers": 5,
      "toes": 5
    }
  },
  {
    "id": 2,
    "name": "Jane Doe",
    "status": 2,
    "counts": {
      "fingers": 5,
      "toes": 10 
    }
  },
  {
    "id": 3,
    "name": "John Smith",
    "status": 2,
    "counts": {
      "fingers": 5,
      "toes": 5
    }
  }
]

After running through dataDiff

[
  {
    "id": 2,
    "status": 2,
    "counts": {
      "toes": 10 
    }
  },
  {
    "id": 3,
    "name": "John Smith",
    "status": 2,
    "counts": {
      "fingers": 5,
      "toes": 5
    }
  }
]

Only the new items and the differences get returned.

Usage

var diff = dataDiff(data1, data2, 'id'); //id is the key to match the two data points together

Installation

Node: npm install data-diff

Browser: Development | Production

Package Sidebar

Install

npm i data-diff

Weekly Downloads

0

Version

0.0.2

License

none

Last publish

Collaborators

  • jga