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