-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
40 lines (34 loc) · 1.03 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// To run:
//
// $ node example.js
import { immutableJSONPatch, revertJSONPatch } from './lib/esm/index.js'
const document = {
baz: 'qux',
foo: 'bar'
}
console.log('document', document)
const operations = [
{ op: 'replace', path: '/baz', value: 'boo' },
{ op: 'add', path: '/hello', value: ['world'] },
{ op: 'remove', path: '/foo' }
]
console.log('operations', operations)
const updatedDocument = immutableJSONPatch(document, operations)
console.log('updatedDocument', updatedDocument)
// updatedDocument = {
// "baz": "boo",
// "hello": ["world"]
// }
const reverseOperations = revertJSONPatch(document, operations)
console.log('reverseOperations', reverseOperations)
// reverseOperations = [
// { op: 'add', path: '/foo', value: 'bar' },
// { op: 'remove', path: '/hello' },
// { op: 'replace', path: '/baz', value: 'qux' }
// ]
const revertedDocument = immutableJSONPatch(updatedDocument, reverseOperations)
console.log('revertedDocument', revertedDocument)
// revertedDocument = {
// "baz": "qux",
// "foo": "bar"
// }