-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
377 lines (305 loc) · 8.21 KB
/
index.ts
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
export type AnyJson = boolean | number | string | null | JsonArray | JsonMap;
interface JsonMap {
[key: string]: AnyJson;
}
interface JsonArray extends Array<AnyJson> {}
export type ObjPath = (string | number)[];
export type DiffInsert = [ObjPath, any];
export type DiffPart = DiffInsert | ObjPath;
export type Diff = DiffPart[];
export function isInsert(d: DiffPart): d is DiffInsert {
return isArr(d[0]);
}
export function isObj(o: any): o is {[k: string]: any} {
return o instanceof Object && !(o instanceof Array);
}
export function isArr(o: any): o is Array<any> {
return o instanceof Array;
}
export function shallowCopy(o: AnyJson): AnyJson {
if (isObj(o)) return {...o};
if (isArr(o)) return o.slice();
return o;
}
export function getContainer(
orig: AnyJson,
result: AnyJson,
path: ObjPath
): AnyJson | void {
let len = path.length;
if (!len) return undefined;
let origContainer: any = orig;
let container: any = result;
if (container === origContainer) container = shallowCopy(origContainer);
for (let i = 0; i < len - 1; ++i) {
let seg = path[i];
if (typeof seg === "number" && isArr(origContainer) && isArr(container)) {
origContainer = origContainer[seg];
if (container[seg] === origContainer) {
container = container[seg] = shallowCopy(origContainer);
} else {
container = container[seg];
}
}
if (typeof seg === "string" && isObj(origContainer) && isObj(container)) {
origContainer = origContainer[seg];
if (container[seg] === origContainer) {
container = container[seg] = shallowCopy(origContainer);
} else {
container = container[seg];
}
}
}
return container;
}
export function getVal(container: AnyJson, path: ObjPath): AnyJson {
let len = path.length;
for (let i = 0; i < len; ++i) {
let seg = path[i];
if (typeof seg === "number" && isArr(container)) {
container = container[seg];
}
if (typeof seg === "string" && isObj(container)) {
container = (container as any)[seg];
}
}
return container;
}
export function applyDiff(o: AnyJson, d: Diff | void) {
if (!d) return o;
let result = shallowCopy(o);
d.forEach(p => {
if (isInsert(p)) result = applyInsert(o, result, p);
else result = applyDelete(o, result, p);
});
return result;
}
export function applyInsert(
orig: AnyJson,
result: AnyJson,
insert: DiffInsert
): AnyJson {
let [path, val] = insert;
let container: any = getContainer(orig, result, path);
if (!container) return val;
let key = path[path.length - 1];
if (typeof key === "number" && isArr(container)) {
container.splice(key, 0, val);
}
if (typeof key === "string" && isObj(container)) {
container[key] = val;
}
return result;
}
export function applyDelete(
orig: AnyJson,
result: AnyJson,
path: ObjPath
): AnyJson {
let container: any = getContainer(orig, result, path);
if (!container) return null;
let key = path[path.length - 1];
if (typeof key === "number" && isArr(container)) {
container.splice(key, 1);
return result;
}
if (typeof key === "string" && isObj(container)) {
delete container[key];
return result;
}
return null;
}
export function diff(
a: AnyJson,
b: AnyJson,
tolerance = Infinity
): Diff | void {
let result: Diff = [];
if (gatherDiff(a, b, tolerance, [], result) || result.length > tolerance)
return [[[], b]];
if (result.length === 0) return null;
return result;
}
function gatherDiff(
a: AnyJson,
b: AnyJson,
tolerance = 3,
path: ObjPath,
result: Diff
): boolean {
if (a === undefined) a = null;
if (b === undefined) b = null;
if (typeof a === "number" && isNaN(a)) a = null;
if (typeof b === "number" && isNaN(b)) b = null;
if (a === b) return false;
if (typeof a !== typeof b) {
result.push([path, b]);
return false;
}
if (a instanceof Array) {
if (!(b instanceof Array)) {
result.push([path, b]);
return false;
}
let offset = 0;
const thunks: (() => void)[] = [];
if (
!arrDiff(
a,
b,
tolerance - result.length,
() => thunks.push(() => ++offset),
(aIdx: number, bIdx: number) =>
thunks.push(() => result.push(path.concat([offset]))),
(aIdx: number, bIdx: number) =>
thunks.push(() => {
result.push([path.concat([offset++]), (b as any)[bIdx]]);
})
)
)
return true;
for (let i = thunks.length - 1; i >= 0; --i) {
thunks[i]();
}
return false;
}
if (b instanceof Array) {
result.push([path, b]);
return false;
}
if (a instanceof Object) {
if (!(b instanceof Object)) {
result.push([path, b]);
return false;
}
for (var k in a as object) {
if (!(k in (b as object))) {
result.push(path.concat([k]));
if (result.length > tolerance) {
return true;
}
continue;
}
if (
gatherDiff(
(a as any)[k],
(b as any)[k],
tolerance,
path.concat([k]),
result
)
) {
return true;
}
if (result.length > tolerance) {
return true;
}
}
for (var k in b as any) {
if (!(k in (a as any))) {
result.push([path.concat([k]), (b as any)[k]]);
if (result.length > tolerance) {
return true;
}
}
}
return false;
}
result.push([path, b]);
return false;
}
export function deepEqual(a: AnyJson, b: AnyJson) {
return a === b || diff(a, b, 0) == null;
}
/**
* Finds the longest common subsequence between a and b,
* optionally shortcutting any search whose removed elements
* would exceed the provided tolerance value.
* If there is no match within the provided tolerance, this function
* returns null.
*/
export function lcs(
a: AnyJson[],
b: AnyJson[],
tolerance = a.length + b.length
): AnyJson[] | void {
let result: AnyJson[] = [];
return arrDiff(a, b, tolerance, aIdx => result.push(a[aIdx]))
? result.reverse()
: null;
}
function arrDiff(
a: AnyJson[],
b: AnyJson[],
tolerance = a.length + b.length,
onEq: (aIdx: number, bIdx: number) => void,
onPickA: (aIdx: number, bIdx: number) => void = () => null,
onPickB: (aIdx: number, bIdx: number) => void = () => null
): boolean {
tolerance = Math.min(tolerance, a.length + b.length);
let aLen = a.length;
let bLen = b.length;
let aOfDiagonal = new Uint32Array(tolerance * 2 + 2);
let aOfDiagonalForEditSize = new Array(tolerance + 1);
let shortestEdit: [number, number] | void = (function() {
for (var d = 0; d <= tolerance; ++d) {
for (var k = -d; k <= d; k += 2) {
let aIdx: number;
let takeB = aOfDiagonal[k + 1 + tolerance];
let takeA = aOfDiagonal[k - 1 + tolerance];
if (k === -d || (k !== d && takeA < takeB)) {
aIdx = takeB;
} else {
aIdx = takeA + 1;
}
let bIdx = aIdx - k;
while (
aIdx < aLen &&
bIdx < bLen &&
deepEqual(a[aIdx], b[bIdx])
) {
aIdx++;
bIdx++;
}
aOfDiagonal[k + tolerance] = aIdx;
if (aIdx >= aLen && bIdx >= bLen) {
aOfDiagonalForEditSize[d] = aOfDiagonal.slice();
return [d, k] as [number, number];
}
}
aOfDiagonalForEditSize[d] = aOfDiagonal.slice();
}
return null;
})();
if (shortestEdit) {
let [d, k] = shortestEdit;
let aIdx = aOfDiagonalForEditSize[d][k + tolerance];
let bIdx = aIdx - k;
while (d > 0) {
let k = aIdx - bIdx;
let v = aOfDiagonalForEditSize[d - 1];
let prevK: number;
if (k === -d || (k !== d && v[k - 1 + tolerance] < v[k + 1 + tolerance])) {
prevK = k + 1;
} else {
prevK = k - 1;
}
let prevAIdx = v[prevK + tolerance];
let prevBIdx = prevAIdx - prevK;
while (aIdx > prevAIdx && bIdx > prevBIdx) {
onEq(--aIdx, --bIdx);
}
if (aIdx > prevAIdx) {
onPickA(--aIdx, bIdx);
} else if (bIdx > prevBIdx) {
onPickB(aIdx, --bIdx);
}
--d;
}
while (aIdx > 0 && bIdx > 0) {
onEq(--aIdx, --bIdx);
}
return true;
}
return false;
}