@sanity/diff 5.0.0-next.0-9b570ece82-202507150640 → 5.0.0-next.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/lib/index.js +15 -10
- package/lib/index.js.map +1 -1
- package/package.json +10 -9
- package/lib/index.d.mts +0 -417
- package/lib/index.mjs +0 -658
- package/lib/index.mjs.map +0 -1
package/lib/index.mjs
DELETED
|
@@ -1,658 +0,0 @@
|
|
|
1
|
-
import { cleanupSemantic, makeDiff, DIFF_INSERT, DIFF_DELETE, DIFF_EQUAL } from "@sanity/diff-match-patch";
|
|
2
|
-
function replaceProperty(parent, prop, value) {
|
|
3
|
-
return delete parent[prop], parent[prop] = value, value;
|
|
4
|
-
}
|
|
5
|
-
function getLongestCommonSubsequence(previous, next) {
|
|
6
|
-
const matrix = getLengthMatrix(previous, next);
|
|
7
|
-
return backtrack(matrix, previous, next);
|
|
8
|
-
}
|
|
9
|
-
function getLengthMatrix(previous, next) {
|
|
10
|
-
const len1 = previous.length, len2 = next.length;
|
|
11
|
-
let x = 0, y = 0;
|
|
12
|
-
const matrix = new Array(len1 + 1);
|
|
13
|
-
for (x = 0; x < len1 + 1; x++)
|
|
14
|
-
for (matrix[x] = [len2 + 1], y = 0; y < len2 + 1; y++)
|
|
15
|
-
matrix[x][y] = 0;
|
|
16
|
-
for (x = 1; x < len1 + 1; x++)
|
|
17
|
-
for (y = 1; y < len2 + 1; y++)
|
|
18
|
-
previous[x - 1] === next[y - 1] ? matrix[x][y] = matrix[x - 1][y - 1] + 1 : matrix[x][y] = Math.max(matrix[x - 1][y], matrix[x][y - 1]);
|
|
19
|
-
return matrix;
|
|
20
|
-
}
|
|
21
|
-
function backtrack(matrix, previous, next) {
|
|
22
|
-
let prevIndex = previous.length, nextIndex = next.length;
|
|
23
|
-
const subsequence = {
|
|
24
|
-
sequence: [],
|
|
25
|
-
prevIndices: [],
|
|
26
|
-
nextIndices: []
|
|
27
|
-
};
|
|
28
|
-
for (; prevIndex !== 0 && nextIndex !== 0; )
|
|
29
|
-
if (previous[prevIndex - 1] === next[nextIndex - 1])
|
|
30
|
-
subsequence.sequence.unshift(previous[prevIndex - 1]), subsequence.prevIndices.unshift(prevIndex - 1), subsequence.nextIndices.unshift(nextIndex - 1), --prevIndex, --nextIndex;
|
|
31
|
-
else {
|
|
32
|
-
const valueAtMatrixAbove = matrix[prevIndex][nextIndex - 1], valueAtMatrixLeft = matrix[prevIndex - 1][nextIndex];
|
|
33
|
-
valueAtMatrixAbove > valueAtMatrixLeft ? --nextIndex : --prevIndex;
|
|
34
|
-
}
|
|
35
|
-
return subsequence;
|
|
36
|
-
}
|
|
37
|
-
function diffArray(fromInput, toInput, options) {
|
|
38
|
-
if (fromInput === toInput) {
|
|
39
|
-
const fromValue = fromInput.value, toValue = toInput.value;
|
|
40
|
-
return {
|
|
41
|
-
type: "array",
|
|
42
|
-
action: "unchanged",
|
|
43
|
-
isChanged: !1,
|
|
44
|
-
fromValue,
|
|
45
|
-
toValue,
|
|
46
|
-
get items() {
|
|
47
|
-
const items2 = diffExactByPosition(fromInput, toInput, options);
|
|
48
|
-
if (!items2) throw new Error("invariant broken: equivalent input, but diff detected");
|
|
49
|
-
return replaceProperty(this, "items", items2);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
const keyedA = indexByKey(fromInput), keyedB = indexByKey(toInput);
|
|
54
|
-
if (keyedA && keyedB)
|
|
55
|
-
return diffArrayByKey(fromInput, keyedA, toInput, keyedB);
|
|
56
|
-
const items = diffExactByPosition(fromInput, toInput, options);
|
|
57
|
-
return items ? buildArrayDiff(fromInput, toInput, items, !1) : diffArrayByReinsert(fromInput, toInput);
|
|
58
|
-
}
|
|
59
|
-
function buildArrayDiff(fromInput, toInput, items, isChanged) {
|
|
60
|
-
const fromValue = fromInput.value, toValue = toInput.value;
|
|
61
|
-
return isChanged ? {
|
|
62
|
-
type: "array",
|
|
63
|
-
action: "changed",
|
|
64
|
-
isChanged: !0,
|
|
65
|
-
fromValue,
|
|
66
|
-
toValue,
|
|
67
|
-
items,
|
|
68
|
-
annotation: toInput.annotation
|
|
69
|
-
} : {
|
|
70
|
-
type: "array",
|
|
71
|
-
action: "unchanged",
|
|
72
|
-
isChanged: !1,
|
|
73
|
-
fromValue,
|
|
74
|
-
toValue,
|
|
75
|
-
items
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
function diffExactByPosition(fromInput, toInput, options) {
|
|
79
|
-
if (fromInput.length !== toInput.length)
|
|
80
|
-
return;
|
|
81
|
-
const items = [];
|
|
82
|
-
for (let idx = 0; idx < fromInput.length; idx++) {
|
|
83
|
-
const diff = diffInput(fromInput.at(idx), toInput.at(idx), options);
|
|
84
|
-
if (diff.isChanged)
|
|
85
|
-
return;
|
|
86
|
-
items.push({
|
|
87
|
-
fromIndex: idx,
|
|
88
|
-
toIndex: idx,
|
|
89
|
-
hasMoved: !1,
|
|
90
|
-
diff,
|
|
91
|
-
annotation: toInput.annotationAt(idx)
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
return items;
|
|
95
|
-
}
|
|
96
|
-
function diffArrayByReinsert(fromInput, toInput, options) {
|
|
97
|
-
const items = [];
|
|
98
|
-
for (let idx = 0; idx < toInput.length; idx++) {
|
|
99
|
-
const input = toInput.at(idx);
|
|
100
|
-
items.push({
|
|
101
|
-
fromIndex: void 0,
|
|
102
|
-
toIndex: idx,
|
|
103
|
-
hasMoved: !1,
|
|
104
|
-
diff: addedInput(input, void 0),
|
|
105
|
-
annotation: input.annotation
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
for (let idx = 0; idx < fromInput.length; idx++) {
|
|
109
|
-
const input = fromInput.at(idx);
|
|
110
|
-
items.push({
|
|
111
|
-
fromIndex: idx,
|
|
112
|
-
toIndex: void 0,
|
|
113
|
-
hasMoved: !1,
|
|
114
|
-
diff: removedInput(input, void 0),
|
|
115
|
-
annotation: input.annotation
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
return buildArrayDiff(fromInput, toInput, items, !0);
|
|
119
|
-
}
|
|
120
|
-
function diffArrayByKey(fromArray, fromKeyIndex, toArray, toKeyIndex, options) {
|
|
121
|
-
const items = [];
|
|
122
|
-
let isChanged = !1;
|
|
123
|
-
function diffCommon(key, fromIndex, toIndex, hasMoved) {
|
|
124
|
-
deletePositionInIndex(fromKeyIndex.index, key, fromIndex), deletePositionInIndex(toKeyIndex.index, key, toIndex);
|
|
125
|
-
const fromInput = fromArray.at(fromIndex), toInput = toArray.at(toIndex), diff = diffInput(fromInput, toInput);
|
|
126
|
-
items.push({
|
|
127
|
-
fromIndex,
|
|
128
|
-
toIndex,
|
|
129
|
-
hasMoved,
|
|
130
|
-
diff,
|
|
131
|
-
annotation: toArray.annotationAt(toIndex)
|
|
132
|
-
}), (diff.isChanged || fromIndex !== toIndex) && (isChanged = !0);
|
|
133
|
-
}
|
|
134
|
-
const lcs = getLongestCommonSubsequence(fromKeyIndex.keys, toKeyIndex.keys);
|
|
135
|
-
for (let fromIndex = 0; fromIndex < fromKeyIndex.keys.length; fromIndex++) {
|
|
136
|
-
const key = fromKeyIndex.keys[fromIndex], subsequenceIdx = lcs.prevIndices.indexOf(fromIndex);
|
|
137
|
-
if (subsequenceIdx !== -1) {
|
|
138
|
-
diffCommon(key, fromIndex, lcs.nextIndices[subsequenceIdx], !1);
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
const toIndexes = toKeyIndex.index.get(key), toIndex = toIndexes && toIndexes.find((idx) => !lcs.nextIndices.includes(idx));
|
|
142
|
-
if (toIndex !== void 0) {
|
|
143
|
-
diffCommon(key, fromIndex, toIndex, !0);
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
const input = fromArray.at(fromIndex);
|
|
147
|
-
items.push({
|
|
148
|
-
fromIndex,
|
|
149
|
-
toIndex: void 0,
|
|
150
|
-
hasMoved: !1,
|
|
151
|
-
diff: removedInput(input, void 0),
|
|
152
|
-
annotation: fromArray.annotationAt(fromIndex)
|
|
153
|
-
}), isChanged = !0;
|
|
154
|
-
}
|
|
155
|
-
for (const positions of toKeyIndex.index.values()) {
|
|
156
|
-
for (const toIndex of positions) {
|
|
157
|
-
const input = toArray.at(toIndex);
|
|
158
|
-
items.push({
|
|
159
|
-
fromIndex: void 0,
|
|
160
|
-
toIndex,
|
|
161
|
-
hasMoved: !1,
|
|
162
|
-
diff: addedInput(input, void 0),
|
|
163
|
-
annotation: toArray.annotationAt(toIndex)
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
isChanged = !0;
|
|
167
|
-
}
|
|
168
|
-
return items.sort(compareItemDiff), buildArrayDiff(fromArray, toArray, items, isChanged);
|
|
169
|
-
}
|
|
170
|
-
function compareItemDiff(a, b) {
|
|
171
|
-
if (a.toIndex !== void 0 && b.toIndex !== void 0)
|
|
172
|
-
return a.toIndex - b.toIndex;
|
|
173
|
-
if (a.fromIndex !== void 0 && b.fromIndex !== void 0)
|
|
174
|
-
return a.fromIndex - b.fromIndex;
|
|
175
|
-
if (a.fromIndex !== void 0 && b.toIndex !== void 0)
|
|
176
|
-
return -1;
|
|
177
|
-
if (a.toIndex !== void 0 && b.fromIndex !== void 0)
|
|
178
|
-
return 1;
|
|
179
|
-
throw new Error("invalid item diff comparison");
|
|
180
|
-
}
|
|
181
|
-
function deletePositionInIndex(index, key, pos) {
|
|
182
|
-
const positions = index.get(key);
|
|
183
|
-
deleteArrayValue(positions, pos), positions.length === 0 && index.delete(key);
|
|
184
|
-
}
|
|
185
|
-
function deleteArrayValue(arr, value) {
|
|
186
|
-
const idx = arr.indexOf(value);
|
|
187
|
-
if (idx === -1) throw new Error("value not found");
|
|
188
|
-
arr.splice(idx, 1);
|
|
189
|
-
}
|
|
190
|
-
function indexByKey(arr) {
|
|
191
|
-
const index = /* @__PURE__ */ new Map(), keys = [], length = arr.length;
|
|
192
|
-
for (let i = 0; i < length; i++) {
|
|
193
|
-
const item = arr.at(i);
|
|
194
|
-
let key = null;
|
|
195
|
-
switch (item.type) {
|
|
196
|
-
case "string":
|
|
197
|
-
key = `s${item.value}`;
|
|
198
|
-
break;
|
|
199
|
-
case "number":
|
|
200
|
-
key = item.value;
|
|
201
|
-
break;
|
|
202
|
-
case "boolean":
|
|
203
|
-
key = item.value;
|
|
204
|
-
break;
|
|
205
|
-
case "null":
|
|
206
|
-
key = "n";
|
|
207
|
-
break;
|
|
208
|
-
case "object":
|
|
209
|
-
{
|
|
210
|
-
const keyField = item.get("_key");
|
|
211
|
-
if (keyField && keyField.type === "string" && (key = `k${keyField.value}`, index.has(key)))
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
break;
|
|
215
|
-
}
|
|
216
|
-
if (key === null) return;
|
|
217
|
-
keys.push(key);
|
|
218
|
-
let positions = index.get(key);
|
|
219
|
-
positions || (positions = [], index.set(key, positions)), positions.push(i);
|
|
220
|
-
}
|
|
221
|
-
return { keys, index };
|
|
222
|
-
}
|
|
223
|
-
function removedArray(input, toValue, options) {
|
|
224
|
-
return {
|
|
225
|
-
type: "array",
|
|
226
|
-
action: "removed",
|
|
227
|
-
isChanged: !0,
|
|
228
|
-
fromValue: input.value,
|
|
229
|
-
toValue,
|
|
230
|
-
annotation: input.annotation,
|
|
231
|
-
get items() {
|
|
232
|
-
const items = [];
|
|
233
|
-
for (let i = 0; i < input.length; i++) {
|
|
234
|
-
const item = input.at(i);
|
|
235
|
-
items.push({
|
|
236
|
-
fromIndex: i,
|
|
237
|
-
toIndex: void 0,
|
|
238
|
-
hasMoved: !1,
|
|
239
|
-
diff: removedInput(item, void 0),
|
|
240
|
-
annotation: input.annotationAt(i)
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
return replaceProperty(this, "items", items);
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
function addedArray(input, fromValue, options) {
|
|
248
|
-
return {
|
|
249
|
-
type: "array",
|
|
250
|
-
action: "added",
|
|
251
|
-
isChanged: !0,
|
|
252
|
-
fromValue,
|
|
253
|
-
toValue: input.value,
|
|
254
|
-
annotation: input.annotation,
|
|
255
|
-
get items() {
|
|
256
|
-
const items = [];
|
|
257
|
-
for (let i = 0; i < input.length; i++) {
|
|
258
|
-
const item = input.at(i);
|
|
259
|
-
items.push({
|
|
260
|
-
fromIndex: void 0,
|
|
261
|
-
toIndex: i,
|
|
262
|
-
hasMoved: !1,
|
|
263
|
-
diff: addedInput(item, void 0),
|
|
264
|
-
annotation: input.annotationAt(i)
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
return replaceProperty(this, "items", items);
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
const ignoredFields = /* @__PURE__ */ new Set(["_id", "_type", "_createdAt", "_updatedAt", "_rev", "_weak"]);
|
|
272
|
-
function diffObject(fromInput, toInput, options) {
|
|
273
|
-
const fields = {};
|
|
274
|
-
let isChanged = !1;
|
|
275
|
-
for (const key of fromInput.keys) {
|
|
276
|
-
if (ignoredFields.has(key)) continue;
|
|
277
|
-
const fromField = fromInput.get(key), toField = toInput.get(key);
|
|
278
|
-
if (toField) {
|
|
279
|
-
const fieldDiff = diffInput(fromField, toField, options);
|
|
280
|
-
fields[key] = fieldDiff, fieldDiff.isChanged && (isChanged = !0);
|
|
281
|
-
} else
|
|
282
|
-
fields[key] = removedInput(fromField, void 0), isChanged = !0;
|
|
283
|
-
}
|
|
284
|
-
for (const key of toInput.keys) {
|
|
285
|
-
if (ignoredFields.has(key) || fromInput.get(key)) continue;
|
|
286
|
-
const toField = toInput.get(key);
|
|
287
|
-
fields[key] = addedInput(toField, void 0), isChanged = !0;
|
|
288
|
-
}
|
|
289
|
-
const fromValue = fromInput.value, toValue = toInput.value;
|
|
290
|
-
return isChanged ? {
|
|
291
|
-
type: "object",
|
|
292
|
-
action: "changed",
|
|
293
|
-
isChanged: !0,
|
|
294
|
-
fromValue,
|
|
295
|
-
toValue,
|
|
296
|
-
fields,
|
|
297
|
-
annotation: toInput.annotation
|
|
298
|
-
} : {
|
|
299
|
-
type: "object",
|
|
300
|
-
action: "unchanged",
|
|
301
|
-
isChanged: !1,
|
|
302
|
-
fromValue,
|
|
303
|
-
toValue,
|
|
304
|
-
fields
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
function removedObject(input, toValue, options) {
|
|
308
|
-
return {
|
|
309
|
-
type: "object",
|
|
310
|
-
action: "removed",
|
|
311
|
-
isChanged: !0,
|
|
312
|
-
fromValue: input.value,
|
|
313
|
-
toValue,
|
|
314
|
-
annotation: input.annotation,
|
|
315
|
-
get fields() {
|
|
316
|
-
const fields = {};
|
|
317
|
-
for (const key of input.keys) {
|
|
318
|
-
const value = input.get(key);
|
|
319
|
-
fields[key] = removedInput(value, void 0);
|
|
320
|
-
}
|
|
321
|
-
return replaceProperty(this, "fields", fields);
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
function addedObject(input, fromValue, options) {
|
|
326
|
-
return {
|
|
327
|
-
type: "object",
|
|
328
|
-
action: "added",
|
|
329
|
-
isChanged: !0,
|
|
330
|
-
fromValue,
|
|
331
|
-
toValue: input.value,
|
|
332
|
-
annotation: input.annotation,
|
|
333
|
-
get fields() {
|
|
334
|
-
const fields = {};
|
|
335
|
-
for (const key of input.keys) {
|
|
336
|
-
const value = input.get(key);
|
|
337
|
-
fields[key] = addedInput(value, void 0);
|
|
338
|
-
}
|
|
339
|
-
return replaceProperty(this, "fields", fields);
|
|
340
|
-
}
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
function diffNumber(fromInput, toInput, options) {
|
|
344
|
-
const fromValue = fromInput.value, toValue = toInput.value, type = fromInput.type;
|
|
345
|
-
return fromValue === toValue ? {
|
|
346
|
-
type,
|
|
347
|
-
action: "unchanged",
|
|
348
|
-
fromValue,
|
|
349
|
-
toValue,
|
|
350
|
-
isChanged: !1
|
|
351
|
-
} : {
|
|
352
|
-
type: fromInput.type,
|
|
353
|
-
action: "changed",
|
|
354
|
-
isChanged: !0,
|
|
355
|
-
fromValue,
|
|
356
|
-
toValue,
|
|
357
|
-
annotation: toInput.annotation
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
function diffBoolean(fromInput, toInput, options) {
|
|
361
|
-
const fromValue = fromInput.value, toValue = toInput.value, type = fromInput.type;
|
|
362
|
-
return fromValue === toValue ? {
|
|
363
|
-
type,
|
|
364
|
-
action: "unchanged",
|
|
365
|
-
fromValue,
|
|
366
|
-
toValue,
|
|
367
|
-
isChanged: !1
|
|
368
|
-
} : {
|
|
369
|
-
type: fromInput.type,
|
|
370
|
-
action: "changed",
|
|
371
|
-
isChanged: !0,
|
|
372
|
-
fromValue,
|
|
373
|
-
toValue,
|
|
374
|
-
annotation: toInput.annotation
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
function diffString(fromInput, toInput, options) {
|
|
378
|
-
const fromValue = fromInput.value, toValue = toInput.value;
|
|
379
|
-
return fromValue === toValue ? {
|
|
380
|
-
type: "string",
|
|
381
|
-
action: "unchanged",
|
|
382
|
-
isChanged: !1,
|
|
383
|
-
fromValue,
|
|
384
|
-
toValue,
|
|
385
|
-
segments: [{ type: "stringSegment", action: "unchanged", text: fromValue }]
|
|
386
|
-
} : {
|
|
387
|
-
type: "string",
|
|
388
|
-
action: "changed",
|
|
389
|
-
isChanged: !0,
|
|
390
|
-
fromValue,
|
|
391
|
-
toValue,
|
|
392
|
-
annotation: toInput.annotation,
|
|
393
|
-
// Compute and memoize string segments only when accessed
|
|
394
|
-
get segments() {
|
|
395
|
-
const segments = buildSegments(fromInput, toInput);
|
|
396
|
-
return replaceProperty(this, "segments", segments);
|
|
397
|
-
}
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
function buildSegments(fromInput, toInput) {
|
|
401
|
-
const segments = [], dmpDiffs = cleanupSemantic(makeDiff(fromInput.value, toInput.value));
|
|
402
|
-
let fromIdx = 0, toIdx = 0;
|
|
403
|
-
for (const [op, text] of dmpDiffs)
|
|
404
|
-
switch (op) {
|
|
405
|
-
case DIFF_EQUAL:
|
|
406
|
-
segments.push({ type: "stringSegment", action: "unchanged", text }), fromIdx += text.length, toIdx += text.length;
|
|
407
|
-
break;
|
|
408
|
-
case DIFF_DELETE:
|
|
409
|
-
for (const segment of fromInput.sliceAnnotation(fromIdx, fromIdx + text.length))
|
|
410
|
-
segments.push({
|
|
411
|
-
type: "stringSegment",
|
|
412
|
-
action: "removed",
|
|
413
|
-
text: segment.text,
|
|
414
|
-
annotation: segment.annotation
|
|
415
|
-
});
|
|
416
|
-
fromIdx += text.length;
|
|
417
|
-
break;
|
|
418
|
-
case DIFF_INSERT:
|
|
419
|
-
for (const segment of toInput.sliceAnnotation(toIdx, toIdx + text.length))
|
|
420
|
-
segments.push({
|
|
421
|
-
type: "stringSegment",
|
|
422
|
-
action: "added",
|
|
423
|
-
text: segment.text,
|
|
424
|
-
annotation: segment.annotation
|
|
425
|
-
});
|
|
426
|
-
toIdx += text.length;
|
|
427
|
-
break;
|
|
428
|
-
default:
|
|
429
|
-
throw new Error(`Unhandled diff-match-patch operation "${op}"`);
|
|
430
|
-
}
|
|
431
|
-
return segments;
|
|
432
|
-
}
|
|
433
|
-
function removedString(input, toValue, options) {
|
|
434
|
-
return {
|
|
435
|
-
type: "string",
|
|
436
|
-
action: "removed",
|
|
437
|
-
isChanged: !0,
|
|
438
|
-
fromValue: input.value,
|
|
439
|
-
toValue,
|
|
440
|
-
annotation: input.annotation,
|
|
441
|
-
get segments() {
|
|
442
|
-
const segments = input.sliceAnnotation(0, input.value.length).map((segment) => ({ type: "stringSegment", action: "removed", ...segment }));
|
|
443
|
-
return replaceProperty(this, "segments", segments);
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
}
|
|
447
|
-
function addedString(input, fromValue, options) {
|
|
448
|
-
return {
|
|
449
|
-
type: "string",
|
|
450
|
-
action: "added",
|
|
451
|
-
isChanged: !0,
|
|
452
|
-
fromValue,
|
|
453
|
-
toValue: input.value,
|
|
454
|
-
annotation: input.annotation,
|
|
455
|
-
get segments() {
|
|
456
|
-
const segments = input.sliceAnnotation(0, input.value.length).map((segment) => ({ type: "stringSegment", action: "added", ...segment }));
|
|
457
|
-
return replaceProperty(this, "segments", segments);
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
function diffTypeChange(fromInput, toInput, options) {
|
|
462
|
-
return {
|
|
463
|
-
type: "typeChange",
|
|
464
|
-
action: "changed",
|
|
465
|
-
isChanged: !0,
|
|
466
|
-
fromType: fromInput.type,
|
|
467
|
-
fromValue: fromInput.value,
|
|
468
|
-
fromDiff: removedInput(fromInput, void 0),
|
|
469
|
-
toType: toInput.type,
|
|
470
|
-
toValue: toInput.value,
|
|
471
|
-
toDiff: addedInput(toInput, void 0),
|
|
472
|
-
annotation: toInput.annotation
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
function diffInput(fromInput, toInput, options = {}) {
|
|
476
|
-
return fromInput.type !== toInput.type ? fromInput.type === "null" ? addedInput(toInput, null) : toInput.type === "null" ? removedInput(fromInput, null) : diffTypeChange(fromInput, toInput) : diffWithType(fromInput.type, fromInput, toInput, options);
|
|
477
|
-
}
|
|
478
|
-
function diffWithType(type, fromInput, toInput, options) {
|
|
479
|
-
switch (type) {
|
|
480
|
-
case "null":
|
|
481
|
-
return {
|
|
482
|
-
type: "null",
|
|
483
|
-
action: "unchanged",
|
|
484
|
-
isChanged: !1,
|
|
485
|
-
toValue: null,
|
|
486
|
-
fromValue: null
|
|
487
|
-
};
|
|
488
|
-
case "boolean":
|
|
489
|
-
return diffBoolean(fromInput, toInput);
|
|
490
|
-
case "number":
|
|
491
|
-
return diffNumber(fromInput, toInput);
|
|
492
|
-
case "string":
|
|
493
|
-
return diffString(fromInput, toInput);
|
|
494
|
-
case "array":
|
|
495
|
-
return diffArray(fromInput, toInput, options);
|
|
496
|
-
case "object":
|
|
497
|
-
return diffObject(fromInput, toInput, options);
|
|
498
|
-
default:
|
|
499
|
-
throw new Error(`Unhandled diff type "${type}"`);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
function removedInput(input, toValue, options) {
|
|
503
|
-
switch (input.type) {
|
|
504
|
-
case "null":
|
|
505
|
-
return {
|
|
506
|
-
type: "null",
|
|
507
|
-
action: "removed",
|
|
508
|
-
isChanged: !0,
|
|
509
|
-
fromValue: null,
|
|
510
|
-
toValue,
|
|
511
|
-
annotation: input.annotation
|
|
512
|
-
};
|
|
513
|
-
case "boolean":
|
|
514
|
-
return {
|
|
515
|
-
type: "boolean",
|
|
516
|
-
action: "removed",
|
|
517
|
-
isChanged: !0,
|
|
518
|
-
fromValue: input.value,
|
|
519
|
-
toValue,
|
|
520
|
-
annotation: input.annotation
|
|
521
|
-
};
|
|
522
|
-
case "number":
|
|
523
|
-
return {
|
|
524
|
-
type: "number",
|
|
525
|
-
action: "removed",
|
|
526
|
-
isChanged: !0,
|
|
527
|
-
fromValue: input.value,
|
|
528
|
-
toValue,
|
|
529
|
-
annotation: input.annotation
|
|
530
|
-
};
|
|
531
|
-
case "string":
|
|
532
|
-
return removedString(input, toValue);
|
|
533
|
-
case "array":
|
|
534
|
-
return removedArray(input, toValue);
|
|
535
|
-
case "object":
|
|
536
|
-
return removedObject(input, toValue);
|
|
537
|
-
default:
|
|
538
|
-
throw new Error("Unhandled diff type");
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
function addedInput(input, fromValue, options) {
|
|
542
|
-
switch (input.type) {
|
|
543
|
-
case "null":
|
|
544
|
-
return {
|
|
545
|
-
type: "null",
|
|
546
|
-
action: "added",
|
|
547
|
-
isChanged: !0,
|
|
548
|
-
fromValue,
|
|
549
|
-
toValue: null,
|
|
550
|
-
annotation: input.annotation
|
|
551
|
-
};
|
|
552
|
-
case "boolean":
|
|
553
|
-
return {
|
|
554
|
-
type: "boolean",
|
|
555
|
-
action: "added",
|
|
556
|
-
isChanged: !0,
|
|
557
|
-
fromValue,
|
|
558
|
-
toValue: input.value,
|
|
559
|
-
annotation: input.annotation
|
|
560
|
-
};
|
|
561
|
-
case "number":
|
|
562
|
-
return {
|
|
563
|
-
type: "number",
|
|
564
|
-
action: "added",
|
|
565
|
-
isChanged: !0,
|
|
566
|
-
fromValue,
|
|
567
|
-
toValue: input.value,
|
|
568
|
-
annotation: input.annotation
|
|
569
|
-
};
|
|
570
|
-
case "string":
|
|
571
|
-
return addedString(input, fromValue);
|
|
572
|
-
case "array":
|
|
573
|
-
return addedArray(input, fromValue);
|
|
574
|
-
case "object":
|
|
575
|
-
return addedObject(input, fromValue);
|
|
576
|
-
default:
|
|
577
|
-
throw new Error("Unhandled diff type");
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
class ArrayWrapper {
|
|
581
|
-
type = "array";
|
|
582
|
-
length;
|
|
583
|
-
value;
|
|
584
|
-
annotation;
|
|
585
|
-
elements = [];
|
|
586
|
-
constructor(value, annotation) {
|
|
587
|
-
this.annotation = annotation, this.value = value, this.length = value.length;
|
|
588
|
-
}
|
|
589
|
-
at(idx) {
|
|
590
|
-
if (idx >= this.length) throw new Error("out of bounds");
|
|
591
|
-
return this.elements[idx] || (this.elements[idx] = wrap(this.value[idx], this.annotation));
|
|
592
|
-
}
|
|
593
|
-
annotationAt() {
|
|
594
|
-
return this.annotation;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
class BasicWrapper {
|
|
598
|
-
type;
|
|
599
|
-
value;
|
|
600
|
-
annotation;
|
|
601
|
-
constructor(type, value, annotation) {
|
|
602
|
-
this.type = type, this.value = value, this.annotation = annotation;
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
class ObjectWrapper {
|
|
606
|
-
type = "object";
|
|
607
|
-
value;
|
|
608
|
-
keys;
|
|
609
|
-
annotation;
|
|
610
|
-
fields = {};
|
|
611
|
-
constructor(value, annotation) {
|
|
612
|
-
this.value = value, this.annotation = annotation, this.keys = Object.keys(value);
|
|
613
|
-
}
|
|
614
|
-
get(key) {
|
|
615
|
-
const input = this.fields[key];
|
|
616
|
-
if (input)
|
|
617
|
-
return input;
|
|
618
|
-
if (!this.value.hasOwnProperty(key))
|
|
619
|
-
return;
|
|
620
|
-
const raw = this.value[key];
|
|
621
|
-
return this.fields[key] = wrap(raw, this.annotation);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
class StringWrapper {
|
|
625
|
-
type = "string";
|
|
626
|
-
value;
|
|
627
|
-
annotation;
|
|
628
|
-
constructor(value, annotation) {
|
|
629
|
-
this.value = value, this.annotation = annotation;
|
|
630
|
-
}
|
|
631
|
-
sliceAnnotation(start, end) {
|
|
632
|
-
return [{ text: this.value.slice(start, end), annotation: this.annotation }];
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
function wrap(input, annotation) {
|
|
636
|
-
if (Array.isArray(input))
|
|
637
|
-
return new ArrayWrapper(input, annotation);
|
|
638
|
-
if (input === null)
|
|
639
|
-
return new BasicWrapper("null", input, annotation);
|
|
640
|
-
const type = typeof input;
|
|
641
|
-
switch (type) {
|
|
642
|
-
case "number":
|
|
643
|
-
return new BasicWrapper(type, input, annotation);
|
|
644
|
-
case "boolean":
|
|
645
|
-
return new BasicWrapper(type, input, annotation);
|
|
646
|
-
case "object":
|
|
647
|
-
return new ObjectWrapper(input, annotation);
|
|
648
|
-
case "string":
|
|
649
|
-
return new StringWrapper(input, annotation);
|
|
650
|
-
default:
|
|
651
|
-
throw new Error(`cannot wrap value of type: ${type}`);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
export {
|
|
655
|
-
diffInput,
|
|
656
|
-
wrap
|
|
657
|
-
};
|
|
658
|
-
//# sourceMappingURL=index.mjs.map
|