jsonpatch-observe
v1.0.0
Published
Observe objects recursively and generate JSON patches
Downloads
16
Maintainers
Readme
Observe an object tree for changes and generate JSON Patches (RFC 6902). Uses Harmony Proxy, available in NodeJS version 6.4 and above.
Usage
const {observe} = require("jsonpatch-observe");
let observable = observe({});
observable.$subscribe(patch => console.log(patch));
observable.a = {b:1}; //prints {op:"add", path:"a", value:{b:1}}
observable.a.b = 2; //prints {op:"add", path:"a/b", value:2}
delete observable.a; //prints {op:"remove", path:"a"}
Note that the properties of an Observable are also Observables. This is how it's able to detect when you do observable.a.b = 2
.
Unobserved Properties
You can exclude certain properties from observe
as follows:
require("jsonpatch-observe").config.excludeProperty = function(obj, prop) {
//return true to exclude the property
}
Splice Patch
The JSONPatch standard does not specify a "splice" operation. Without splice, Array changes are represented as a series of individual "add", "replace", and "remove" operations, which can be quite inefficient to apply.
This module supports generating the splice patch. Enable it as follows:
require("jsonpatch-observe").config.enableSplice = true;
The splice patch has the following format:
{
op: "splice",
path: "/myarr/3", //path to array index
remove: 2, //number of elements removed
add: ['a','b','c'] //elements added
}
I created a fork of Starcounter-Jack JSONPatch library capable of consuming this non-standard splice patch.