json-patch-ot
Library to reconcile JSON patch changes using Operational Transformation
You must pass the function a list of JSON patches that you want to transform against. These should be your accepted changes that are already accepted by the server, but were not known about at the creation of the second argument, the proposed changes.
Finally you can pass an options object which currently only supports one option. The acceptedWinsOnEqualPath
option will decide if new replace
operations should override an accepted replace
that was made to the exact same path
.
Example
const acceptedOps: Operation[] = [
{op: OpType.remove, path: '/array/1'},
{op: OpType.add, path: '/array/3', value: 30},
];
const proposedOps: Operation[] = [
{op: OpType.replace, path: '/array/2', value: 4},
{op: OpType.replace, path: '/array/1', value: 2},
{op: OpType.replace, path: '/array/5', value: 10},
];
const result = JSONPatchOT(acceptedOps, proposedOps);
Options
acceptedWinsOnEqualPath
For some operation types, the default behaviour is to overwrite if the proposed change has the same path as an accepted change. For example, below, without the option passed, the second replace in the proposedOps would not be remove. This is useful if you want proposed changes only to be able to change a path if they knew the value it had before. Note: remove
ops in accepted changes always cause proposed operations with the same path to be deleted.
const options = {acceptedWinsOnEqualPath: true};
const acceptedOps: Operation[] = [
{op: OpType.replace, path: '/toreplace', value: 'new val'}
];
const proposedOps: Operation[] = [
{op: OpType.replace, path: '/some/other', value: 3},
{op: OpType.replace, path: '/toreplace', value: 'something else'},
];
const result = JSONPatchOT(acceptedOps, proposedOps, options);