Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active December 12, 2019 17:14
Show Gist options
  • Save davidhq/b8ecfa2a97e56789fcd6f70cb6776529 to your computer and use it in GitHub Desktop.
Save davidhq/b8ecfa2a97e56789fcd6f70cb6776529 to your computer and use it in GitHub Desktop.
json-diff.js
import { applyPatch } from 'fast-json-patch/index.mjs';
import justDiff from 'just-diff';
const { diff, jsonPatchPathConverter } = justDiff;
const doc = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const patch = diff(doc, [], jsonPatchPathConverter);
console.log(patch);
console.log(applyPatch(doc, patch).newDocument);
// [
// { op: 'remove', path: '/9' },
// { op: 'remove', path: '/8' },
// { op: 'remove', path: '/7' },
// { op: 'remove', path: '/6' },
// { op: 'remove', path: '/5' },
// { op: 'remove', path: '/4' },
// { op: 'remove', path: '/3' },
// { op: 'remove', path: '/2' },
// { op: 'remove', path: '/14' },
// { op: 'remove', path: '/13' },
// { op: 'remove', path: '/12' },
// { op: 'remove', path: '/11' },
// { op: 'remove', path: '/10' },
// { op: 'remove', path: '/1' },
// { op: 'remove', path: '/0' }
// ]
// RESULT:
// [ 11, 12, 13, 14, 15 ]
// should be an empty list!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment