@toolsnap/json-diff
v1.0.0
Published
Deep JSON comparison tool that reports added, removed, and changed properties
Maintainers
Readme
@toolsnap/json-diff
Deep JSON comparison tool that reports added, removed, and changed properties. Zero dependencies.
Installation
npm install @toolsnap/json-diffUsage
const { diff, diffFormatted } = require('@toolsnap/json-diff');
const obj1 = { name: 'foo', version: 1, tags: ['a', 'b'] };
const obj2 = { name: 'foo', version: 2, tags: ['a', 'c'], active: true };
// Get raw differences
const differences = diff(obj1, obj2);
console.log(differences);
// [
// { type: 'changed', path: 'version', oldValue: 1, newValue: 2 },
// { type: 'changed', path: 'tags[1]', oldValue: 'b', newValue: 'c' },
// { type: 'added', path: 'active', value: true }
// ]
// Get formatted summary
const result = diffFormatted(obj1, obj2);
console.log(result.summary);
// { added: 1, removed: 0, changed: 2, total: 3 }API
diff(obj1, obj2)- Deep compare, returns array of differencesdiffFormatted(obj1, obj2)- Returns{ equal, differences, summary }applyPatch(obj, differences)- Apply differences to create a new object
Difference Types
added- Property exists in obj2 but not obj1removed- Property exists in obj1 but not obj2changed- Property exists in both but values differ
License
MIT
