Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.79k stars 215 forks source link

compare() with Object/Array substitution #250

Closed jla closed 4 years ago

jla commented 4 years ago
> jsonpatch.compare({a: {b: 1}}, {a: [1, 2]});
[
  {op: "replace", path: "/a", value: [1, 2]},
  {op: "add", path: "/a/0", value: 1},
  {op: "add", path: "/a/1", value: 2}
]

> jsonpatch.compare({a: {b: 1}}, {a: [1, 2]}).reduce(jsonpatch.applyReducer, {a: {b: 1}});
{a: [1, 2, 1, 2]}
jla commented 4 years ago

Probably related:

> var obj = {data: {a: 'b'}};
> var observer = jsonpatch.observe(obj);
> obj.data = [1, 2];
> var patches = jsonpatch.generate(observer);
> obj
{data: [1, 2, 1, 2]}