Repository navigation
Expand file tree
/
Copy pathserialized.ts
More file actions
232 lines (217 loc) · 5.35 KB
/
Copy pathserialized.ts
File metadata and controls
232 lines (217 loc) · 5.35 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import CBOR from 'cbor'
import * as PlainOperational from './plain'
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) => {
const _diff = PlainOperational.diff(left, right)
if (!_diff) return _diff
try {
const cborEncoded: Buffer = CBOR.encode(_diff)
const compressed = cborEncoded.toString('base64')
if (!compressed) throw new Error()
return compressed
} catch (e) {
return undefined
}
}
/**
* @description
* Returns the changed values of the object.
*
* @param object any
*/
export const diffs = (object: any[]) => {
const diffs: string[] = []
for (let i = 0; i < (object.length - 1); i++) {
const left = object[i]
const right = object[i + 1]
const diffObject = diff(left, right)
if (diffObject) diffs.push(diffObject)
}
return diffs
}
/**
* @description
* Returns the changed content in
* JSON PATCH (RFC 6902) format.
*
* @param diff string
* @param original any
* @returns It returns JSON PATCH (RFC 6902)
*/
export const changelogs = (diff: string, original: any) => {
try {
const _diff: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(diff, 'base64')
)
return PlainOperational.changelogs(_diff, original)
} catch (e) {
return undefined
}
}
export interface IChangelogsFormattedOption {
diff: string
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) => {
try {
const diff: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(option.diff, 'base64')
)
return PlainOperational.changelogsFormatted({
diff,
format: option.format,
original: option.original,
})
} catch (e) {
return undefined
}
}
/**
* @description
* Applies changes to objects.
*
* @param left any
* @param diff string
*/
export const patch = (left: any, diff: string) => {
try {
const _diff: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(diff, 'base64')
)
return CustomizedDiffPatch.patch(left, _diff)
} catch (e) {
return undefined
}
}
/**
* @description
* Applies multiple changes to objects.
*
* @param left any
* @param diffs string[]
*/
export const patches = (object: any, diffs: string[]) => {
let patched: any = object
try {
for (let diff of diffs) {
const _diff: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(diff, 'base64')
)
patched = CustomizedDiffPatch.patch(patched, _diff)
}
} catch (e) {
return undefined
}
return patched
}
/**
* @description
* Exclude changes from objects.
*
* @param right any
* @param diff string
*/
export const unpatch = (right: any, diff: string) => {
const _diff: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(diff, 'base64')
)
return CustomizedDiffPatch.unpatch(right, _diff)
}
/**
* @description
* Exclude multiple changes from objects.
*
* @param right any
* @param diffs string
*/
export const unpatches = (object: any, diffs: string[]) => {
let patched: any = undefined
try {
for (let diff of diffs) {
const _diff: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(diff, 'base64')
)
patched = CustomizedDiffPatch.unpatch(patched, _diff)
}
} catch (e) { }
return patched
}
/**
* @description
* Reverse the diff order.
*
* @param diff string
*/
export const reverse = (diff: string) => {
try {
const _diff: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(diff, 'base64')
)
return CustomizedDiffPatch.reverse(_diff)
} catch (e) {
return undefined
}
}
export const decodeDiff = (diff: string) => {
try {
const decoded: JSONDiffPatch.Delta = CBOR.decode(
Buffer.from(diff, 'base64')
)
if (!decoded) throw new Error()
return decoded
} catch (e) {
return undefined
}
}
export const decodeDiffs = (diffs: string[]) => {
let decodedDiffs: JSONDiffPatch.Delta[] = []
try {
for (let diff of diffs) {
const decodedDiff = decodeDiff(diff)
if (!decodedDiff) throw new Error()
decodedDiffs.push(decodedDiff)
}
return decodedDiffs
} catch (e) {
return undefined
}
}
export const encodeDiff = (diff: JSONDiffPatch.Delta) => {
try {
const encoded: string = CBOR.encode(diff).toString('base64')
if (!encoded) throw new Error()
return encoded
} catch (e) {
return undefined
}
}
export const encodeDiffs = (diffs: JSONDiffPatch.Delta[]) => {
let encodedDiffs: string[] = []
try {
for (let diff of diffs) {
const encodedDiff = encodeDiff(diff)
if (!encodedDiff) throw new Error()
encodedDiffs.push(encodedDiff)
}
return encodedDiffs
} catch (e) {
return undefined
}
}