JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3841
  • Score
    100M100P100Q127774F
  • License MIT

JSON Patch implementation for JavaScript

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

build status

JSON Patch RFC 6902 implementation for JavaScript.

See jsonpatch.com for more information about JSON Patch.

JSON8 Patch passes the entire json-patch-tests suite; see Tests

Getting started

npm install json8-patch


var ooPatch = require('json8-patch');

or

<script src="node_modules/json8-patch/JSON8Patch.js"></script>
var ooPatch = window.JSON8Patch

For performance concerns JSON8 Patch mutates documents; if you want it to work on its own shallow copy of your document use:

var clone = require('json8').clone;
doc = clone(doc)

See clone.

Methods

apply

doc = ooPatch.apply(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              }
]);

ooPatch.apply returns a document because the JSON Patch specification states that an operation can replace the original document.

ooPatch.apply is atomic, if any operation fails, the document will be restored to its original state and an error will be thrown.

patch

Alias for apply method.

revert

If the patch or apply method is called with a third argument {reversible: true} it will return an array of the form [doc, revert].

The revert object can be used to revert a patch on a document.

// apply the patch with the reversible option
var patchResult = ooPatch.apply(doc, patch, {reversible: true});
doc = patchResult[0];

// revert the patch
doc = ooPatch.revert(doc, patchResult[1]);
// doc is strictly identical to the origina

Operations

add, copy, replace, move, remove, test operations return an array of the form [document, previous, idx]

The first argument is the patched document.

The second argument is the previous value at the specified destination if any, undefined otherwise.

The third argument is used internaly and can be ignored. It 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 = ooPatch.add(doc, '/foo', 'foo')[0]

remove

doc = ooPatch.remove(doc, '/foo')[0];

replace

doc = ooPatch.replace(doc, '/foo', 'foo')[0];

move

doc = ooPatch.move(doc, '/foo', '/bar')[0];

copy

doc = ooPatch.copy(doc, '/foo', '/bar')[0];

test

doc = ooPatch.test(doc, '/foo', 'bar')[0];

Extra operations

Those are not part of the standard and are only provided for convenience.

get

ooPatch.get(doc, '/foo');
// returns value at /foo

has

ooPatch.has(doc, '/foo');
// returns true if foo property exists, false otherwise

Tests

git submodule update --init --recursive
npm install -g eslint mocha babel
npm test

Contributing

See CONTRIBUTING.md