Package Exports
- json8-patch
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 (json8-patch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
JSON8-patch
JSON8-patch passes the entire json-patch-tests suite; see tests
npm install json8-patchvar JSON8 = require('json8-patch');For performance concerns JSON8-patch mutates documents; if you want it to work on its own shallow copy of your document use:
doc = JSON8.clone(doc)See clone.
var doc = {
"name": "John Doe",
"address": {
"street": "...",
"number": 42,
"city": "Neverland"
},
"tags": ["friends", "coworker"],
"age": 25,
"starred": true,
"height": null
});patch
doc = JSON8.patch(doc, [
{ "op": "add", "path": "/tags/0", "value": "family" },
{ "op": "remove", "path": "/height" },
{ "op": "replace", "path": "/age", "value": "26" },
{ "op": "move", "from": "/address/city", "path": "/address/country" },
{ "op": "copy", "from": "/starred", "to": "bookmarked" },
{ "op": "test", "path": "/starred", "value": true }
]);JSON8.patch returns a document because the JSON Patch specification states that an operation can replace the original document.
JSON8.patch is atomic, if any operation fails, the document will be restored to its original state and an error will be thrown.
apply
JSON8.apply is an alias to JSON.patch and behaves exactly the same.
revert
If JSON8.patch is passed with a third argument {revert: true} it will return an array [doc, items].
The items object can be used to revert a patch on a document.
// apply the patch
var patchResult = JSON8.patch(doc, patch, {revert: true});
doc = patchResult[0];
// revert the patch
doc = JSON8.revert(doc, patchResult[1]);
// doc is strictly identical (in the JSON sense) to the originalOperations
add, copy, replace, move, remove, test operations return an array of the form [document, previous, idx]
The first argument is returned for the same reason JSON8.apply returns a document see patch
The second argument is the previous value at the specified destination if any, undefined otherwise.
The third argument is the index of the new element. For add operation only using the JSON Pointer '-' token to push an item at the end of an array.
add
doc = JSON8.add(doc, '/foo', 'foo')[0]remove
doc = JSON8.remove(doc, '/foo')[0];replace
doc = JSON8.replace(doc, '/foo', 'foo')[0];move
doc = JSON8.move(doc, '/foo', '/bar')[0];copy
doc = JSON8.copy(doc, '/foo', '/bar')[0];test
doc = JSON8.test(doc, '/foo', 'bar')[0];Extra operations
Those are not part of the standard and are only provided for convenience. They cannot be used inside a JSON Patch document.
get
JSON8.get(doc, '/foo');
// returns value at /foohas
JSON8.has(doc, '/foo');
// returns true if foo property exists, false otherwisetests
git submodule update --init --recursive
npm install -g eslint mocha
npm test