A javascript JSON diff and patch implementation of the RFC-6902. This library refers to diffson.
npm install diffsonjs
var diffsonjs = require('diffsonjs');
var left = {a: 1, b: true};
var right = {a: 2};
var delta = diffsonjs.diff(left, right);
console.log(delta);
// [
// { op: 'replace', path: '/a', value: 2 },
// { op: 'remove', path: '/b' }
// ]
var result = diffsonjs.patch(left, delta);
console.log(result);
// {a: 2}
Compute the diff between two json objects.
interface DiffOptions {
omitArrayDiffs?: boolean // default false, different arrays are represented by repleacement.
remember?: boolean // default false, set the previous value as `oldValue` property for replacement or movement.
equals?: (left: unknown, right: unknown) => boolean // if it exists, use it instead of the normal json comparison function.
hashCode?: (value: any) => number // if it exists, it is used to speed up comparison of arrays.
}
Apply a patch to json and return the result.
interface PatchOptions {
equals?: (left: unknown, right: unknown) => boolean // if it exists, use it for tests.
}