-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplain.ts
More file actions
156 lines (145 loc) · 3.55 KB
/
plain.ts
File metadata and controls
156 lines (145 loc) · 3.55 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as JSONDiffPatch from 'jsondiffpatch'
import { CustomizedDiffPatch } from './diff'
/**
* @description
* Returns the changed value of the object.
*
* @param left any
* @param right any
*/
export const diff = (left: any, right: any) => {
return CustomizedDiffPatch.diff(left, right)
}
/**
* @description
* Returns the changed values of the object.
*
* @param object any
*/
export const diffs = (object: any[]) => {
const diffs: JSONDiffPatch.Delta[] = []
for (let i = 0; i < (object.length - 1); i++) {
const left = object[i]
const right = object[i + 1]
const diffObject = CustomizedDiffPatch.diff(left, right)
if (diffObject) diffs.push(diffObject)
}
return diffs
}
export interface IChangeLogs {
op: 'add' | 'remove' | 'replace' | 'move'
path: string
value: any
}
/**
* @description
* Returns the changed content in
* JSON PATCH (RFC 6902) format.
*
* @param diff
* @param original any
* @returns It returns JSON PATCH (RFC 6902)
*/
export const changelogs = (diff: JSONDiffPatch.Delta, original: any) => {
try {
// @ts-ignore
const result: IChangeLogs[] = JSONDiffPatch.formatters.jsonpatch.format(diff, original)
if (!result) throw new Error()
return result
} catch (e) {
return undefined
}
}
export interface IChangelogsFormattedOption {
diff: JSONDiffPatch.Delta
original: any
format: 'console' | 'annotated' | 'html'
}
/**
* @description
* Returns the changed content in
* human readable log form.
*
* @param option
* @returns string
*/
export const changelogsFormatted = (option: IChangelogsFormattedOption) => {
let result: string | undefined = undefined
try {
switch (option.format) {
case 'annotated':
result = JSONDiffPatch.formatters.annotated.format(option.diff, option.original)
break
case 'console':
result = JSONDiffPatch.formatters.console.format(option.diff, option.original)
break
case 'html':
result = JSONDiffPatch.formatters.html.format(option.diff, option.original)
break
}
} catch (e) { }
return result
}
/**
* @description
* Applies changes to objects.
*
* @param left any
* @param diff
*/
export const patch = (left: any, diff: JSONDiffPatch.Delta) => {
return CustomizedDiffPatch.patch(left, diff)
}
/**
* @description
* Applies multiple changes to objects.
*
* @param left any
* @param diffs
*/
export const patches = (object: any, diffs: JSONDiffPatch.Delta[]) => {
let patched: any = object
try {
for (let diff of diffs)
patched = CustomizedDiffPatch.patch(patched, diff)
} catch (e) {
return undefined
}
return patched
}
/**
* @description
* Exclude changes from objects.
*
* @param right any
* @param diff
*/
export const unpatch = (right: any, diff: JSONDiffPatch.Delta) => {
return CustomizedDiffPatch.unpatch(right, diff)
}
/**
* @description
* Exclude multiple changes from objects.
*
* @param right any
* @param diff
*/
export const unpatches = (object: any, diffs: JSONDiffPatch.Delta[]) => {
let patched: any = undefined
try {
for (let diff of diffs)
patched = CustomizedDiffPatch.unpatch(patched, diff)
} catch (e) {
return undefined
}
return patched
}
/**
* @description
* Reverse the diff order.
*
* @param diff
*/
export const reverse = (diff: JSONDiffPatch.Delta) => {
return CustomizedDiffPatch.reverse(diff)
}