json8-merge-patch-to-patch
v1.0.6
Published
Turn JSON Merge Patch into JSON Patch.
Downloads
7
Readme
JSON8 Merge Patch To Patch
Turn JSON Merge Patch into JSON Patch.
JSON Patch is a more capable alternative to JSON Merge Patch. To work with JSON Patch see JSON8 Patch.
npm install json8-merge-patch-to-patch
const mergePatchToPatch = require("json8-merge-patch-to-patch");
Does not validate the merge patch nor the patch for JSON correctness, use JSON8 valid.
const JSONMergePatch = {
foo: { bar: "foobar" },
bar: null,
};
const JSONPatch = mergePatchToPatch(JSONMergePatch);
/*
[
{ op: 'add', path: '/foo/bar', value: 'foobar' },
{ op: 'remove', path: '/bar' }
]
*/
Per specification a JSON Merge Patch that would successfully apply on a document might fail to apply once converted to a JSON Patch.
There are 3 cases:
Incompatible destination
const doc = [];
const JSONMergePatch = { a: "hello" };
const JSONPatch = mergePatchToPatch(JSONMergePatch);
// JSONPatch will fail to apply because doc is not an object
Wrong location
const doc = {};
const JSONMergePatch = { a: { b: "hello" } };
const JSONPatch = mergePatchToPatch(JSONMergePatch);
// JSONPatch will fail to apply because doc.a doesn't exist
Remove non-existant value
const doc = {};
const JSONMergePatch = { a: null };
const JSONPatch = mergePatchToPatch(JSONMergePatch);
// JSONPatch will fail to apply because doc.a doesn't exist
An option could be added to produce a successful JSON Patch no matter what but the only way to do this is to pass the document as well. Let me know if there is any interest or contribute.