Package Exports
- json-diff-patch-v2
- json-diff-patch-v2/build/main/index.js
- json-diff-patch-v2/build/module/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-diff-patch-v2) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
JsonDiffPatch
JsonDiffPatch is a library that allows for the diffing and patching of JSON objects.
npm install json-diff-patch-v2- Import JsonDiffPatch in your project:
import { DiffPatcher } from 'json-diff-patch-v2';- Create a
DiffPatcherinstance:
const diffPatcher = new DiffPatcher();- Use the
diff,patch, andreversemethods to work with your JSON objects:
- Diff: To find the difference between two objects.
- Patch: To apply a patch to an object.
- Reverse: To reverse a patch.
Examples
Diffing Two Objects
const left = { name: 'John', age: 25 };
const right = { name: 'John', age: 26 };
const delta = diffPatcher.diff(left, right);
console.log(delta);
// Output: { age: [25, 26] }Patching an Object
const original = { name: 'John', age: 25 };
const delta = { age: [25, 26] };
const patched = diffPatcher.patch(original, delta);
console.log(patched);
// Output: { name: 'John', age: 26 }Using Property Filter
In scenarios where you want to ignore certain properties during diffing, you can use the propertyFilter option.
const options = {
propertyFilter: function(name) {
return name.slice(0, 1) !== '$';
},
};
const diffPatcherWithFilter = new DiffPatcher(options);
const left = { data: { $volatile: 123, stable: 456 } };
const right = { data: { $volatile: 124, stable: 456 } };
const delta = diffPatcherWithFilter.diff(left, right);
console.log(delta);
// Output: undefined (since the change is in a filtered property)