An object difference library for NodeJS
This module can compares two objects and return the key that has changed along with the original and updated values.
Installation
$ npm install objecdiff
Usage
const objecdiff = require('objecdiff');
// Alternatively:
import { diff } from 'objecdiff';
API
diff(original, updated) Documentation
objecdiff.diff(objectA, objectB)
// Alternatively if using the `import` method shown above:
diff(objectA, objectB)
-
objectA
-Object
- The original or first object that you would like to compare. -
objectB
-Object
- The updated or second object that you would like to compare the first against.
Example
import { diff } from 'objecdiff';
let a = {
firstName: 'Mike',
lastName: 'R',
city: 'Boston'
},
b = {
firstName: 'Dan',
name: 'Dan G',
color: {
favorite: 'blue'
},
city: 'Boston'
};
console.log(diff(a, b));
// => [ { path: 'firstName', original: 'Mike', updated: 'Dan' },
// => { path: 'lastName', original: 'R', updated: undefined },
// => { path: 'name', original: undefined, updated: 'Dan G' },
// => { path: 'color.favorite', original: null, updated: 'blue' } ]
Tests
$ npm test
To see test coverage, please run:
$ npm run coverage
Features
- Natively supports nested documents
- Compares the objects and returns the key that has changed along with the original and updated values