-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffing.ts
More file actions
36 lines (31 loc) · 796 Bytes
/
diffing.ts
File metadata and controls
36 lines (31 loc) · 796 Bytes
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
import * as jsondiffpatch from 'jsondiffpatch'
import { set, unset } from 'es-toolkit/compat'
import microdiff from 'microdiff'
/** Merges by using `microdiff` */
export function diffMerge(
prev: object,
next: object,
method: true | 'microdiff' | 'jsondiffpatch' = true,
) {
switch (method) {
case true:
case 'microdiff': {
const diff = microdiff(prev, next)
for (const event of diff) {
switch (event.type) {
case 'CREATE':
case 'CHANGE':
set(prev, event.path, event.value)
break
case 'REMOVE':
unset(prev, event.path)
break
}
}
}
case 'jsondiffpatch': {
const diff = jsondiffpatch.diff(prev, next)
jsondiffpatch.patch(prev, diff)
}
}
}