Last active
December 12, 2019 17:14
-
-
Save davidhq/b8ecfa2a97e56789fcd6f70cb6776529 to your computer and use it in GitHub Desktop.
json-diff.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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