Skip to content

Instantly share code, notes, and snippets.

@OR13
Created July 11, 2017 21:31
Show Gist options
  • Save OR13/91252763f746940556a24b88ec8e905c to your computer and use it in GitHub Desktop.
Save OR13/91252763f746940556a24b88ec8e905c to your computer and use it in GitHub Desktop.
jiff.js
var a = [
{ name: 'a' },
{ name: 'b' },
{ name: 'c' },
]
var b = a.slice();
b.splice(1, 1);
b.push({ name: 'd' });
// Generate diff (ie JSON Patch) from a to b
var patch = jiff.diff(a, b);
// [{"op":"add","path":"/3","value":{"name":"d"}},{"op":"remove","path":"/1"}]
console.log(JSON.stringify(patch));
var patched = jiff.patch(patch, a);
// [{"name":"a"},{"name":"c"},{"name":"d"}]
console.log(JSON.stringify(patched));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment