@sanity/diff-patch 5.0.0 → 6.0.0
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/README.md +248 -95
- package/dist/index.cjs +180 -160
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -151
- package/dist/index.d.ts +47 -151
- package/dist/index.js +182 -162
- package/dist/index.js.map +1 -1
- package/package.json +5 -1
- package/src/diffPatch.ts +458 -357
- package/src/index.ts +3 -15
- package/src/patches.ts +53 -32
- package/src/paths.ts +12 -4
- package/src/setOperations.ts +29 -0
package/src/diffPatch.ts
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {makePatches, stringifyPatches} from '@sanity/diff-match-patch'
|
|
2
2
|
import {DiffError} from './diffError.js'
|
|
3
|
-
import {type Path, pathToString} from './paths.js'
|
|
3
|
+
import {isKeyedObject, type KeyedSanityObject, type Path, pathToString} from './paths.js'
|
|
4
4
|
import {validateProperty} from './validate.js'
|
|
5
5
|
import {
|
|
6
6
|
type Patch,
|
|
7
|
-
type SetPatch,
|
|
8
7
|
type UnsetPatch,
|
|
9
|
-
type InsertAfterPatch,
|
|
10
8
|
type DiffMatchPatch,
|
|
11
|
-
type SanityInsertPatch,
|
|
12
|
-
type SanityPatch,
|
|
13
|
-
type SanitySetPatch,
|
|
14
|
-
type SanityUnsetPatch,
|
|
15
|
-
type SanityDiffMatchPatch,
|
|
16
9
|
type SanityPatchMutation,
|
|
10
|
+
type SanityPatchOperations,
|
|
11
|
+
type SanitySetPatchOperation,
|
|
12
|
+
type SanityUnsetPatchOperation,
|
|
13
|
+
type SanityInsertPatchOperation,
|
|
14
|
+
type SanityDiffMatchPatchOperation,
|
|
17
15
|
} from './patches.js'
|
|
16
|
+
import {difference, intersection} from './setOperations.js'
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Document keys that are ignored during diff operations.
|
|
20
|
+
* These are system-managed fields that should not be included in patches on
|
|
21
|
+
* top-level documents and should not be diffed with diff-match-patch.
|
|
22
|
+
*/
|
|
23
|
+
const SYSTEM_KEYS = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Maximum size of strings to consider for diff-match-patch (1MB)
|
|
27
|
+
* Based on testing showing consistently good performance up to this size
|
|
28
|
+
*/
|
|
29
|
+
const DMP_MAX_STRING_SIZE = 1_000_000
|
|
22
30
|
|
|
23
31
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @internal
|
|
32
|
+
* Maximum difference in string length before falling back to set operations (40%)
|
|
33
|
+
* Above this threshold, likely indicates text replacement which can be slow
|
|
27
34
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
const DMP_MAX_STRING_LENGTH_CHANGE_RATIO = 0.4
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Minimum string size to apply change ratio check (10KB)
|
|
39
|
+
* Small strings are always fast regardless of change ratio
|
|
40
|
+
*/
|
|
41
|
+
const DMP_MIN_SIZE_FOR_RATIO_CHECK = 10_000
|
|
32
42
|
|
|
33
43
|
/**
|
|
34
44
|
* An object (record) that _may_ have a `_key` property
|
|
@@ -51,37 +61,6 @@ export interface DocumentStub {
|
|
|
51
61
|
[key: string]: unknown
|
|
52
62
|
}
|
|
53
63
|
|
|
54
|
-
/**
|
|
55
|
-
* Options for the diff-match-patch algorithm.
|
|
56
|
-
*
|
|
57
|
-
* @public
|
|
58
|
-
*/
|
|
59
|
-
export interface DiffMatchPatchOptions {
|
|
60
|
-
/**
|
|
61
|
-
* Whether or not to use diff-match-patch at all
|
|
62
|
-
*
|
|
63
|
-
* @defaultValue `true`
|
|
64
|
-
*/
|
|
65
|
-
enabled: boolean
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Threshold at which to start using diff-match-patch instead of a regular `set` patch.
|
|
69
|
-
*
|
|
70
|
-
* @defaultValue `30`
|
|
71
|
-
*/
|
|
72
|
-
lengthThresholdAbsolute: number
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Only use generated diff-match-patch if the patch length is less than or equal to
|
|
76
|
-
* (targetString * relative). Example: A 100 character target with a relative factor
|
|
77
|
-
* of 1.2 will allow a 120 character diff-match-patch. If larger than this number,
|
|
78
|
-
* it will fall back to a regular `set` patch.
|
|
79
|
-
*
|
|
80
|
-
* @defaultValue `1.2`
|
|
81
|
-
*/
|
|
82
|
-
lengthThresholdRelative: number
|
|
83
|
-
}
|
|
84
|
-
|
|
85
64
|
/**
|
|
86
65
|
* Options for the patch generator
|
|
87
66
|
*
|
|
@@ -110,80 +89,30 @@ export interface PatchOptions {
|
|
|
110
89
|
* @defaultValue `undefined` (do not apply revision check)
|
|
111
90
|
*/
|
|
112
91
|
ifRevisionID?: string | true
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Whether or not to hide warnings during the diff process.
|
|
116
|
-
*
|
|
117
|
-
* @defaultValue `false`
|
|
118
|
-
*/
|
|
119
|
-
hideWarnings?: boolean
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Options for the diff-match-patch algorithm.
|
|
123
|
-
*/
|
|
124
|
-
diffMatchPatch?: Partial<DiffMatchPatchOptions>
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Options for diff generation, where all DMP properties are required
|
|
129
|
-
*
|
|
130
|
-
* @public
|
|
131
|
-
*/
|
|
132
|
-
export type DiffOptions = PatchOptions & {diffMatchPatch: Required<DiffMatchPatchOptions>}
|
|
133
|
-
|
|
134
|
-
const defaultOptions = {
|
|
135
|
-
hideWarnings: false,
|
|
136
|
-
diffMatchPatch: {
|
|
137
|
-
enabled: true,
|
|
138
|
-
|
|
139
|
-
// Only use diff-match-patch if target string is longer than this threshold
|
|
140
|
-
lengthThresholdAbsolute: 30,
|
|
141
|
-
|
|
142
|
-
// Only use generated diff-match-patch if the patch length is less than or equal to
|
|
143
|
-
// (targetString * relative). Example: A 100 character target with a relative factor
|
|
144
|
-
// of 1.2 will allow a 120 character diff-match-patch. If larger than this number,
|
|
145
|
-
// it will fall back to a regular `set` patch.
|
|
146
|
-
lengthThresholdRelative: 1.2,
|
|
147
|
-
},
|
|
148
|
-
} satisfies DiffOptions
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Merges the default options with the passed in options.
|
|
152
|
-
*
|
|
153
|
-
* @param options - Options to merge with the defaults
|
|
154
|
-
* @returns Merged options
|
|
155
|
-
*/
|
|
156
|
-
function mergeOptions(options: PatchOptions): DiffOptions {
|
|
157
|
-
return {
|
|
158
|
-
...defaultOptions,
|
|
159
|
-
...options,
|
|
160
|
-
diffMatchPatch: {...defaultOptions.diffMatchPatch, ...(options.diffMatchPatch || {})},
|
|
161
|
-
}
|
|
162
92
|
}
|
|
163
93
|
|
|
164
94
|
/**
|
|
165
95
|
* Generates an array of mutations for Sanity, based on the differences between
|
|
166
96
|
* the two passed documents/trees.
|
|
167
97
|
*
|
|
168
|
-
* @param
|
|
169
|
-
* @param
|
|
98
|
+
* @param source - The first document/tree to compare
|
|
99
|
+
* @param target - The second document/tree to compare
|
|
170
100
|
* @param opts - Options for the diff generation
|
|
171
101
|
* @returns Array of mutations
|
|
172
102
|
* @public
|
|
173
103
|
*/
|
|
174
104
|
export function diffPatch(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
105
|
+
source: DocumentStub,
|
|
106
|
+
target: DocumentStub,
|
|
107
|
+
options: PatchOptions = {},
|
|
178
108
|
): SanityPatchMutation[] {
|
|
179
|
-
const
|
|
180
|
-
const id = options.id || (itemA._id === itemB._id && itemA._id)
|
|
109
|
+
const id = options.id || (source._id === target._id && source._id)
|
|
181
110
|
const revisionLocked = options.ifRevisionID
|
|
182
|
-
const ifRevisionID = typeof revisionLocked === 'boolean' ?
|
|
111
|
+
const ifRevisionID = typeof revisionLocked === 'boolean' ? source._rev : revisionLocked
|
|
183
112
|
const basePath = options.basePath || []
|
|
184
113
|
if (!id) {
|
|
185
114
|
throw new Error(
|
|
186
|
-
'_id on
|
|
115
|
+
'_id on source and target not present or differs, specify document id the mutations should be applied to',
|
|
187
116
|
)
|
|
188
117
|
}
|
|
189
118
|
|
|
@@ -193,94 +122,95 @@ export function diffPatch(
|
|
|
193
122
|
)
|
|
194
123
|
}
|
|
195
124
|
|
|
196
|
-
if (basePath.length === 0 &&
|
|
197
|
-
throw new Error(`_type is immutable and cannot be changed (${
|
|
125
|
+
if (basePath.length === 0 && source._type !== target._type) {
|
|
126
|
+
throw new Error(`_type is immutable and cannot be changed (${source._type} => ${target._type})`)
|
|
198
127
|
}
|
|
199
128
|
|
|
200
|
-
const operations = diffItem(
|
|
201
|
-
return serializePatches(operations,
|
|
129
|
+
const operations = diffItem(source, target, basePath, [])
|
|
130
|
+
return serializePatches(operations).map((patchOperations, i) => ({
|
|
131
|
+
patch: {
|
|
132
|
+
id,
|
|
133
|
+
// only add `ifRevisionID` to the first patch
|
|
134
|
+
...(i === 0 && ifRevisionID && {ifRevisionID}),
|
|
135
|
+
...patchOperations,
|
|
136
|
+
},
|
|
137
|
+
}))
|
|
202
138
|
}
|
|
203
139
|
|
|
204
140
|
/**
|
|
205
|
-
*
|
|
206
|
-
*
|
|
141
|
+
* Generates an array of patch operation objects for Sanity, based on the
|
|
142
|
+
* differences between the two passed values
|
|
207
143
|
*
|
|
208
|
-
* @param
|
|
209
|
-
* @param
|
|
210
|
-
* @param
|
|
211
|
-
* @
|
|
212
|
-
* @param patches - Array of patches to append the results to. Note that this is MUTATED.
|
|
213
|
-
* @returns Array of patches
|
|
144
|
+
* @param source - The source value to start off with
|
|
145
|
+
* @param target - The target value that the patch operations will aim to create
|
|
146
|
+
* @param basePath - An optional path that will be prefixed to all subsequent patch operations
|
|
147
|
+
* @returns Array of mutations
|
|
214
148
|
* @public
|
|
215
149
|
*/
|
|
216
|
-
export function
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
150
|
+
export function diffValue(
|
|
151
|
+
source: unknown,
|
|
152
|
+
target: unknown,
|
|
153
|
+
basePath: Path = [],
|
|
154
|
+
): SanityPatchOperations[] {
|
|
155
|
+
return serializePatches(diffItem(source, target, basePath))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function diffItem(
|
|
159
|
+
source: unknown,
|
|
160
|
+
target: unknown,
|
|
220
161
|
path: Path = [],
|
|
221
162
|
patches: Patch[] = [],
|
|
222
163
|
): Patch[] {
|
|
223
|
-
if (
|
|
164
|
+
if (source === target) {
|
|
224
165
|
return patches
|
|
225
166
|
}
|
|
226
167
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const aIsUndefined = aType === 'undefined'
|
|
231
|
-
const bIsUndefined = bType === 'undefined'
|
|
232
|
-
|
|
233
|
-
if (aIsUndefined && !bIsUndefined) {
|
|
234
|
-
patches.push({op: 'set', path, value: itemB})
|
|
168
|
+
if (typeof source === 'string' && typeof target === 'string') {
|
|
169
|
+
diffString(source, target, path, patches)
|
|
235
170
|
return patches
|
|
236
171
|
}
|
|
237
172
|
|
|
238
|
-
if (
|
|
239
|
-
|
|
173
|
+
if (Array.isArray(source) && Array.isArray(target)) {
|
|
174
|
+
diffArray(source, target, path, patches)
|
|
240
175
|
return patches
|
|
241
176
|
}
|
|
242
177
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (!isContainer) {
|
|
247
|
-
return diffPrimitive(itemA as PrimitiveValue, itemB as PrimitiveValue, options, path, patches)
|
|
178
|
+
if (isRecord(source) && isRecord(target)) {
|
|
179
|
+
diffObject(source, target, path, patches)
|
|
180
|
+
return patches
|
|
248
181
|
}
|
|
249
182
|
|
|
250
|
-
if (
|
|
251
|
-
|
|
252
|
-
patches.push({op: 'set', path, value: itemB})
|
|
183
|
+
if (target === undefined) {
|
|
184
|
+
patches.push({op: 'unset', path})
|
|
253
185
|
return patches
|
|
254
186
|
}
|
|
255
187
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
: diffObject(itemA as object, itemB as object, options, path, patches)
|
|
188
|
+
patches.push({op: 'set', path, value: target})
|
|
189
|
+
return patches
|
|
259
190
|
}
|
|
260
191
|
|
|
261
192
|
function diffObject(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
options: DiffOptions,
|
|
193
|
+
source: Record<string, unknown>,
|
|
194
|
+
target: Record<string, unknown>,
|
|
265
195
|
path: Path,
|
|
266
196
|
patches: Patch[],
|
|
267
197
|
) {
|
|
268
198
|
const atRoot = path.length === 0
|
|
269
|
-
const aKeys = Object.keys(
|
|
199
|
+
const aKeys = Object.keys(source)
|
|
270
200
|
.filter(atRoot ? isNotIgnoredKey : yes)
|
|
271
|
-
.map((key) => validateProperty(key,
|
|
201
|
+
.map((key) => validateProperty(key, source[key], path))
|
|
272
202
|
|
|
273
203
|
const aKeysLength = aKeys.length
|
|
274
|
-
const bKeys = Object.keys(
|
|
204
|
+
const bKeys = Object.keys(target)
|
|
275
205
|
.filter(atRoot ? isNotIgnoredKey : yes)
|
|
276
|
-
.map((key) => validateProperty(key,
|
|
206
|
+
.map((key) => validateProperty(key, target[key], path))
|
|
277
207
|
|
|
278
208
|
const bKeysLength = bKeys.length
|
|
279
209
|
|
|
280
210
|
// Check for deleted items
|
|
281
211
|
for (let i = 0; i < aKeysLength; i++) {
|
|
282
212
|
const key = aKeys[i]
|
|
283
|
-
if (!(key in
|
|
213
|
+
if (!(key in target)) {
|
|
284
214
|
patches.push({op: 'unset', path: path.concat(key)})
|
|
285
215
|
}
|
|
286
216
|
}
|
|
@@ -288,286 +218,457 @@ function diffObject(
|
|
|
288
218
|
// Check for changed items
|
|
289
219
|
for (let i = 0; i < bKeysLength; i++) {
|
|
290
220
|
const key = bKeys[i]
|
|
291
|
-
diffItem(
|
|
221
|
+
diffItem(source[key], target[key], path.concat([key]), patches)
|
|
292
222
|
}
|
|
293
223
|
|
|
294
224
|
return patches
|
|
295
225
|
}
|
|
296
226
|
|
|
297
|
-
function diffArray(
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
227
|
+
function diffArray(source: unknown[], target: unknown[], path: Path, patches: Patch[]) {
|
|
228
|
+
if (isUniquelyKeyed(source) && isUniquelyKeyed(target)) {
|
|
229
|
+
return diffArrayByKey(source, target, path, patches)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return diffArrayByIndex(source, target, path, patches)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function diffArrayByIndex(source: unknown[], target: unknown[], path: Path, patches: Patch[]) {
|
|
304
236
|
// Check for new items
|
|
305
|
-
if (
|
|
237
|
+
if (target.length > source.length) {
|
|
306
238
|
patches.push({
|
|
307
239
|
op: 'insert',
|
|
308
|
-
|
|
309
|
-
|
|
240
|
+
position: 'after',
|
|
241
|
+
path: path.concat([-1]),
|
|
242
|
+
items: target.slice(source.length).map(nullifyUndefined),
|
|
310
243
|
})
|
|
311
244
|
}
|
|
312
245
|
|
|
313
246
|
// Check for deleted items
|
|
314
|
-
if (
|
|
315
|
-
const isSingle =
|
|
316
|
-
const unsetItems =
|
|
317
|
-
|
|
318
|
-
// If we
|
|
319
|
-
//
|
|
320
|
-
|
|
321
|
-
// by key, as this is safer in a realtime, collaborative setting
|
|
322
|
-
if (isRevisionLocked(options) || !isUniquelyKeyed(unsetItems)) {
|
|
323
|
-
patches.push({
|
|
324
|
-
op: 'unset',
|
|
325
|
-
path: path.concat([isSingle ? itemB.length : [itemB.length, '']]),
|
|
326
|
-
})
|
|
327
|
-
} else {
|
|
247
|
+
if (target.length < source.length) {
|
|
248
|
+
const isSingle = source.length - target.length === 1
|
|
249
|
+
const unsetItems = source.slice(target.length)
|
|
250
|
+
|
|
251
|
+
// If we have unique array keys, we'll want to unset by key, as this is
|
|
252
|
+
// safer in a realtime, collaborative setting
|
|
253
|
+
if (isUniquelyKeyed(unsetItems)) {
|
|
328
254
|
patches.push(
|
|
329
255
|
...unsetItems.map(
|
|
330
256
|
(item): UnsetPatch => ({op: 'unset', path: path.concat({_key: item._key})}),
|
|
331
257
|
),
|
|
332
258
|
)
|
|
259
|
+
} else {
|
|
260
|
+
patches.push({
|
|
261
|
+
op: 'unset',
|
|
262
|
+
path: path.concat([isSingle ? target.length : [target.length, '']]),
|
|
263
|
+
})
|
|
333
264
|
}
|
|
334
265
|
}
|
|
335
266
|
|
|
336
267
|
// Check for illegal array contents
|
|
337
|
-
for (let i = 0; i <
|
|
338
|
-
if (Array.isArray(
|
|
339
|
-
throw new DiffError('Multi-dimensional arrays not supported', path.concat(i),
|
|
268
|
+
for (let i = 0; i < target.length; i++) {
|
|
269
|
+
if (Array.isArray(target[i])) {
|
|
270
|
+
throw new DiffError('Multi-dimensional arrays not supported', path.concat(i), target[i])
|
|
340
271
|
}
|
|
341
272
|
}
|
|
342
273
|
|
|
343
|
-
const overlapping = Math.min(
|
|
344
|
-
const segmentA =
|
|
345
|
-
const segmentB =
|
|
274
|
+
const overlapping = Math.min(source.length, target.length)
|
|
275
|
+
const segmentA = source.slice(0, overlapping)
|
|
276
|
+
const segmentB = target.slice(0, overlapping)
|
|
346
277
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
: diffArrayByIndex(segmentA, segmentB, options, path, patches)
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
function diffArrayByIndex(
|
|
353
|
-
itemA: unknown[],
|
|
354
|
-
itemB: unknown[],
|
|
355
|
-
options: DiffOptions,
|
|
356
|
-
path: Path,
|
|
357
|
-
patches: Patch[],
|
|
358
|
-
) {
|
|
359
|
-
for (let i = 0; i < itemA.length; i++) {
|
|
360
|
-
diffItem(
|
|
361
|
-
itemA[i],
|
|
362
|
-
nullifyUndefined(itemB[i], path, i, options),
|
|
363
|
-
options,
|
|
364
|
-
path.concat(i),
|
|
365
|
-
patches,
|
|
366
|
-
)
|
|
278
|
+
for (let i = 0; i < segmentA.length; i++) {
|
|
279
|
+
diffItem(segmentA[i], nullifyUndefined(segmentB[i]), path.concat(i), patches)
|
|
367
280
|
}
|
|
368
281
|
|
|
369
282
|
return patches
|
|
370
283
|
}
|
|
371
284
|
|
|
285
|
+
/**
|
|
286
|
+
* Diffs two arrays of keyed objects by their `_key` properties.
|
|
287
|
+
*
|
|
288
|
+
* This approach is preferred over index-based diffing for collaborative editing scenarios
|
|
289
|
+
* because it generates patches that are more resilient to concurrent modifications.
|
|
290
|
+
* When multiple users edit the same array simultaneously, key-based patches have better
|
|
291
|
+
* conflict resolution characteristics than index-based patches.
|
|
292
|
+
*
|
|
293
|
+
* The function handles three main operations:
|
|
294
|
+
* 1. **Reordering**: When existing items change positions within the array
|
|
295
|
+
* 2. **Content changes**: When the content of existing items is modified
|
|
296
|
+
* 3. **Insertions/Deletions**: When items are added or removed from the array
|
|
297
|
+
*
|
|
298
|
+
* @param source - The original array with keyed objects
|
|
299
|
+
* @param target - The target array with keyed objects
|
|
300
|
+
* @param path - The path to this array within the document
|
|
301
|
+
* @param patches - Array to accumulate generated patches
|
|
302
|
+
* @returns The patches array with new patches appended
|
|
303
|
+
*/
|
|
372
304
|
function diffArrayByKey(
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
options: DiffOptions,
|
|
305
|
+
source: KeyedSanityObject[],
|
|
306
|
+
target: KeyedSanityObject[],
|
|
376
307
|
path: Path,
|
|
377
308
|
patches: Patch[],
|
|
378
309
|
) {
|
|
379
|
-
|
|
380
|
-
const
|
|
310
|
+
// Create lookup maps for efficient key-based access to array items
|
|
311
|
+
const sourceItemsByKey = new Map(source.map((item) => [item._key, item]))
|
|
312
|
+
const targetItemsByKey = new Map(target.map((item) => [item._key, item]))
|
|
313
|
+
|
|
314
|
+
// Categorize keys by their presence in source vs target arrays
|
|
315
|
+
const sourceKeys = new Set(sourceItemsByKey.keys())
|
|
316
|
+
const targetKeys = new Set(targetItemsByKey.keys())
|
|
317
|
+
const keysRemovedFromSource = difference(sourceKeys, targetKeys)
|
|
318
|
+
const keysAddedToTarget = difference(targetKeys, sourceKeys)
|
|
319
|
+
const keysInBothArrays = intersection(sourceKeys, targetKeys)
|
|
320
|
+
|
|
321
|
+
// Handle reordering of existing items within the array.
|
|
322
|
+
// We detect reordering by comparing the relative positions of keys that exist in both arrays,
|
|
323
|
+
// excluding keys that were added or removed (since they don't participate in reordering).
|
|
324
|
+
const sourceKeysStillPresent = Array.from(difference(sourceKeys, keysRemovedFromSource))
|
|
325
|
+
const targetKeysAlreadyPresent = Array.from(difference(targetKeys, keysAddedToTarget))
|
|
326
|
+
|
|
327
|
+
// Track which keys need to be reordered by comparing their relative positions
|
|
328
|
+
const keyReorderOperations: {sourceKey: string; targetKey: string}[] = []
|
|
329
|
+
|
|
330
|
+
for (let i = 0; i < keysInBothArrays.size; i++) {
|
|
331
|
+
const keyAtPositionInSource = sourceKeysStillPresent[i]
|
|
332
|
+
const keyAtPositionInTarget = targetKeysAlreadyPresent[i]
|
|
333
|
+
|
|
334
|
+
// If different keys occupy the same relative position, a reorder is needed
|
|
335
|
+
if (keyAtPositionInSource !== keyAtPositionInTarget) {
|
|
336
|
+
keyReorderOperations.push({
|
|
337
|
+
sourceKey: keyAtPositionInSource,
|
|
338
|
+
targetKey: keyAtPositionInTarget,
|
|
339
|
+
})
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Generate reorder patch if any items changed positions
|
|
344
|
+
if (keyReorderOperations.length) {
|
|
345
|
+
patches.push({
|
|
346
|
+
op: 'reorder',
|
|
347
|
+
path,
|
|
348
|
+
snapshot: source,
|
|
349
|
+
reorders: keyReorderOperations,
|
|
350
|
+
})
|
|
351
|
+
}
|
|
381
352
|
|
|
382
|
-
//
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
return diffArrayByIndex(itemA, itemB, options, path, patches)
|
|
353
|
+
// Process content changes for items that exist in both arrays
|
|
354
|
+
for (const key of keysInBothArrays) {
|
|
355
|
+
diffItem(sourceItemsByKey.get(key), targetItemsByKey.get(key), [...path, {_key: key}], patches)
|
|
386
356
|
}
|
|
387
357
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
358
|
+
// Remove items that no longer exist in the target array
|
|
359
|
+
for (const keyToRemove of keysRemovedFromSource) {
|
|
360
|
+
patches.push({op: 'unset', path: [...path, {_key: keyToRemove}]})
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Insert new items that were added to the target array
|
|
364
|
+
// We batch consecutive insertions for efficiency and insert them at the correct positions
|
|
365
|
+
if (keysAddedToTarget.size) {
|
|
366
|
+
let insertionAnchorKey: string // The key after which we'll insert pending items
|
|
367
|
+
let itemsPendingInsertion: unknown[] = []
|
|
368
|
+
|
|
369
|
+
const flushPendingInsertions = () => {
|
|
370
|
+
if (itemsPendingInsertion.length) {
|
|
371
|
+
patches.push({
|
|
372
|
+
op: 'insert',
|
|
373
|
+
// Insert after the anchor key if we have one, otherwise insert at the beginning
|
|
374
|
+
...(insertionAnchorKey
|
|
375
|
+
? {position: 'after', path: [...path, {_key: insertionAnchorKey}]}
|
|
376
|
+
: {position: 'before', path: [...path, 0]}),
|
|
377
|
+
items: itemsPendingInsertion,
|
|
378
|
+
})
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Walk through the target array to determine where new items should be inserted
|
|
383
|
+
for (const key of targetKeys) {
|
|
384
|
+
if (keysAddedToTarget.has(key)) {
|
|
385
|
+
// This is a new item - add it to the pending insertion batch
|
|
386
|
+
itemsPendingInsertion.push(targetItemsByKey.get(key)!)
|
|
387
|
+
} else if (keysInBothArrays.has(key)) {
|
|
388
|
+
// This is an existing item - flush any pending insertions before it
|
|
389
|
+
flushPendingInsertions()
|
|
390
|
+
insertionAnchorKey = key
|
|
391
|
+
itemsPendingInsertion = []
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Flush any remaining insertions at the end
|
|
396
|
+
flushPendingInsertions()
|
|
393
397
|
}
|
|
394
398
|
|
|
395
399
|
return patches
|
|
396
400
|
}
|
|
397
401
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
402
|
+
/**
|
|
403
|
+
* Determines whether to use diff-match-patch or fallback to a `set` operation
|
|
404
|
+
* when creating a patch to transform a `source` string to `target` string.
|
|
405
|
+
*
|
|
406
|
+
* `diffMatchPatch` patches are typically preferred to `set` operations because
|
|
407
|
+
* they handle conflicts better (when multiple editors work simultaneously) by
|
|
408
|
+
* preserving the user's intended and allowing for 3-way merges.
|
|
409
|
+
*
|
|
410
|
+
* **Heuristic rationale:**
|
|
411
|
+
*
|
|
412
|
+
* Perf analysis revealed that string length has minimal impact on small,
|
|
413
|
+
* keystroke-level changes, but large text replacements (high change ratio) can
|
|
414
|
+
* trigger worst-case algorithm behavior. The 40% change ratio threshold is a
|
|
415
|
+
* simple heuristic that catches problematic replacement scenarios while
|
|
416
|
+
* allowing the algorithm to excel at insertions and deletions.
|
|
417
|
+
*
|
|
418
|
+
* **Performance characteristics (tested on M2 MacBook Pro):**
|
|
419
|
+
*
|
|
420
|
+
* *Keystroke-level editing (most common use case):*
|
|
421
|
+
* - Small strings (1KB-10KB): 0ms for 1-5 keystrokes, consistently sub-millisecond
|
|
422
|
+
* - Medium strings (50KB-200KB): 0ms for 1-5 keystrokes, consistently sub-millisecond
|
|
423
|
+
* - 10 simultaneous keystrokes: ~12ms on 100KB strings
|
|
424
|
+
*
|
|
425
|
+
* *Copy-paste operations (less frequent):*
|
|
426
|
+
* - Small copy-paste operations (<50KB): 0-10ms regardless of string length
|
|
427
|
+
* - Large insertions/deletions (50KB+): 0-50ms (excellent performance)
|
|
428
|
+
* - Large text replacements (50KB+): 70ms-2s+ (can be slow due to algorithm complexity)
|
|
429
|
+
*
|
|
430
|
+
* **Algorithm details:**
|
|
431
|
+
* Uses Myers' diff algorithm with O(ND) time complexity where N=text length and D=edit distance.
|
|
432
|
+
* Includes optimizations: common prefix/suffix removal, line-mode processing, and timeout protection.
|
|
433
|
+
*
|
|
434
|
+
*
|
|
435
|
+
* **Test methodology:**
|
|
436
|
+
* - Generated realistic word-based text patterns
|
|
437
|
+
* - Simulated actual editing behaviors (keystrokes vs copy-paste)
|
|
438
|
+
* - Measured performance across string sizes from 1KB to 10MB
|
|
439
|
+
* - Validated against edge cases including repetitive text and scattered changes
|
|
440
|
+
*
|
|
441
|
+
* @param source - The previous version of the text
|
|
442
|
+
* @param target - The new version of the text
|
|
443
|
+
* @returns true if diff-match-patch should be used, false if fallback to set operation
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* ```typescript
|
|
447
|
+
* // Keystroke editing - always fast
|
|
448
|
+
* shouldUseDiffMatchPatch(largeDoc, largeDocWithTypo) // true, ~0ms
|
|
449
|
+
*
|
|
450
|
+
* // Small paste - always fast
|
|
451
|
+
* shouldUseDiffMatchPatch(doc, docWithSmallInsertion) // true, ~0ms
|
|
452
|
+
*
|
|
453
|
+
* // Large replacement - potentially slow
|
|
454
|
+
* shouldUseDiffMatchPatch(article, completelyDifferentArticle) // false, use set
|
|
455
|
+
* ```
|
|
456
|
+
*
|
|
457
|
+
* Compatible with @sanity/diff-match-patch@3.2.0
|
|
458
|
+
*/
|
|
459
|
+
export function shouldUseDiffMatchPatch(source: string, target: string): boolean {
|
|
460
|
+
const maxLength = Math.max(source.length, target.length)
|
|
461
|
+
|
|
462
|
+
// Always reject strings larger than our tested size limit
|
|
463
|
+
if (maxLength > DMP_MAX_STRING_SIZE) {
|
|
464
|
+
return false
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// For small strings, always use diff-match-patch regardless of change ratio
|
|
468
|
+
// Performance testing showed these are always fast (<10ms)
|
|
469
|
+
if (maxLength < DMP_MIN_SIZE_FOR_RATIO_CHECK) {
|
|
470
|
+
return true
|
|
417
471
|
}
|
|
418
472
|
|
|
419
|
-
|
|
473
|
+
// Calculate the change ratio to detect large text replacements
|
|
474
|
+
// High ratios indicate replacement scenarios which can trigger slow algorithm paths
|
|
475
|
+
const lengthDifference = Math.abs(target.length - source.length)
|
|
476
|
+
const changeRatio = lengthDifference / maxLength
|
|
477
|
+
|
|
478
|
+
// If change ratio is high, likely a replacement operation that could be slow
|
|
479
|
+
// Fall back to set operation for better user experience
|
|
480
|
+
if (changeRatio > DMP_MAX_STRING_LENGTH_CHANGE_RATIO) {
|
|
481
|
+
return false
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// All other cases: use diff-match-patch
|
|
485
|
+
// This covers keystroke editing and insertion/deletion scenarios which perform excellently
|
|
486
|
+
return true
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function getDiffMatchPatch(source: string, target: string, path: Path): DiffMatchPatch | undefined {
|
|
490
|
+
const last = path.at(-1)
|
|
491
|
+
// don't use diff-match-patch for system keys
|
|
492
|
+
if (typeof last === 'string' && last.startsWith('_')) return undefined
|
|
493
|
+
if (!shouldUseDiffMatchPatch(source, target)) return undefined
|
|
494
|
+
|
|
420
495
|
try {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
496
|
+
// Using `makePatches(string, string)` directly instead of the multi-step approach e.g.
|
|
497
|
+
// `stringifyPatches(makePatches(cleanupEfficiency(makeDiff(source, target))))`.
|
|
498
|
+
// this is because `makePatches` internally handles diff generation and
|
|
499
|
+
// automatically applies both `cleanupSemantic()` and `cleanupEfficiency()`
|
|
500
|
+
// when beneficial, resulting in cleaner code with near identical performance and
|
|
501
|
+
// better error handling.
|
|
502
|
+
// [source](https://github.com/sanity-io/diff-match-patch/blob/v3.2.0/src/patch/make.ts#L67-L76)
|
|
503
|
+
//
|
|
504
|
+
// Performance validation (M2 MacBook Pro):
|
|
505
|
+
// Both approaches measured at identical performance:
|
|
506
|
+
// - 10KB strings: 0-1ms total processing time
|
|
507
|
+
// - 100KB strings: 0-1ms total processing time
|
|
508
|
+
// - Individual step breakdown: makeDiff(0ms) + cleanup(0ms) + makePatches(0ms) + stringify(~1ms)
|
|
509
|
+
const strPatch = stringifyPatches(makePatches(source, target))
|
|
510
|
+
return {op: 'diffMatchPatch', path, value: strPatch}
|
|
424
511
|
} catch (err) {
|
|
425
512
|
// Fall back to using regular set patch
|
|
426
513
|
return undefined
|
|
427
514
|
}
|
|
428
|
-
|
|
429
|
-
// Don't use patch if it's longer than allowed relative threshold.
|
|
430
|
-
// Allow a 120 character patch for a 100 character string,
|
|
431
|
-
// but don't allow a 800 character patch for a 500 character value.
|
|
432
|
-
return strPatch.length > itemB.length * lengthThresholdRelative
|
|
433
|
-
? undefined
|
|
434
|
-
: {op: 'diffMatchPatch', path, value: strPatch}
|
|
435
515
|
}
|
|
436
516
|
|
|
437
|
-
function
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
options: DiffOptions,
|
|
441
|
-
path: Path,
|
|
442
|
-
patches: Patch[],
|
|
443
|
-
): Patch[] {
|
|
444
|
-
const dmp = getDiffMatchPatch(itemA, itemB, options, path)
|
|
445
|
-
|
|
446
|
-
patches.push(
|
|
447
|
-
dmp || {
|
|
448
|
-
op: 'set',
|
|
449
|
-
path,
|
|
450
|
-
value: itemB,
|
|
451
|
-
},
|
|
452
|
-
)
|
|
453
|
-
|
|
517
|
+
function diffString(source: string, target: string, path: Path, patches: Patch[]) {
|
|
518
|
+
const dmp = getDiffMatchPatch(source, target, path)
|
|
519
|
+
patches.push(dmp ?? {op: 'set', path, value: target})
|
|
454
520
|
return patches
|
|
455
521
|
}
|
|
456
522
|
|
|
457
523
|
function isNotIgnoredKey(key: string) {
|
|
458
|
-
return
|
|
524
|
+
return SYSTEM_KEYS.indexOf(key) === -1
|
|
459
525
|
}
|
|
460
526
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
(
|
|
490
|
-
const path = pathToString(item.path)
|
|
491
|
-
patch.unset.push(path)
|
|
492
|
-
return patch
|
|
493
|
-
},
|
|
494
|
-
{id, unset: []},
|
|
495
|
-
)
|
|
496
|
-
|
|
497
|
-
const withInsert = insert.reduce((acc: SanityInsertPatch[], item: InsertAfterPatch) => {
|
|
498
|
-
const after = pathToString(item.after)
|
|
499
|
-
return acc.concat({id, insert: {after, items: item.items}})
|
|
500
|
-
}, [])
|
|
501
|
-
|
|
502
|
-
const withDmp =
|
|
503
|
-
dmp.length > 0 &&
|
|
504
|
-
dmp.reduce(
|
|
505
|
-
(patch: SanityDiffMatchPatch, item: DiffMatchPatch) => {
|
|
506
|
-
const path = pathToString(item.path)
|
|
507
|
-
patch.diffMatchPatch[path] = item.value
|
|
508
|
-
return patch
|
|
509
|
-
},
|
|
510
|
-
{id, diffMatchPatch: {}},
|
|
511
|
-
)
|
|
512
|
-
|
|
513
|
-
const patchSet: SanityPatch[] = [withUnset, withSet, withDmp, ...withInsert].filter(
|
|
514
|
-
(item): item is SanityPatch => item !== false,
|
|
515
|
-
)
|
|
527
|
+
// mutually exclusive operations
|
|
528
|
+
type SanityPatchOperation =
|
|
529
|
+
| SanitySetPatchOperation
|
|
530
|
+
| SanityUnsetPatchOperation
|
|
531
|
+
| SanityInsertPatchOperation
|
|
532
|
+
| SanityDiffMatchPatchOperation
|
|
533
|
+
|
|
534
|
+
function serializePatches(patches: Patch[], curr?: SanityPatchOperation): SanityPatchOperations[] {
|
|
535
|
+
const [patch, ...rest] = patches
|
|
536
|
+
if (!patch) return curr ? [curr] : []
|
|
537
|
+
|
|
538
|
+
switch (patch.op) {
|
|
539
|
+
case 'set':
|
|
540
|
+
case 'diffMatchPatch': {
|
|
541
|
+
// TODO: reconfigure eslint to use @typescript-eslint/no-unused-vars
|
|
542
|
+
// eslint-disable-next-line no-unused-vars
|
|
543
|
+
type CurrentOp = Extract<SanityPatchOperation, {[K in typeof patch.op]: {}}>
|
|
544
|
+
const emptyOp = {[patch.op]: {}} as CurrentOp
|
|
545
|
+
|
|
546
|
+
if (!curr) return serializePatches(patches, emptyOp)
|
|
547
|
+
if (!(patch.op in curr)) return [curr, ...serializePatches(patches, emptyOp)]
|
|
548
|
+
|
|
549
|
+
Object.assign((curr as CurrentOp)[patch.op], {[pathToString(patch.path)]: patch.value})
|
|
550
|
+
return serializePatches(rest, curr)
|
|
551
|
+
}
|
|
552
|
+
case 'unset': {
|
|
553
|
+
const emptyOp = {unset: []}
|
|
554
|
+
if (!curr) return serializePatches(patches, emptyOp)
|
|
555
|
+
if (!('unset' in curr)) return [curr, ...serializePatches(patches, emptyOp)]
|
|
516
556
|
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
557
|
+
curr.unset.push(pathToString(patch.path))
|
|
558
|
+
return serializePatches(rest, curr)
|
|
559
|
+
}
|
|
560
|
+
case 'insert': {
|
|
561
|
+
if (curr) return [curr, ...serializePatches(patches)]
|
|
562
|
+
|
|
563
|
+
return [
|
|
564
|
+
{
|
|
565
|
+
insert: {
|
|
566
|
+
[patch.position]: pathToString(patch.path),
|
|
567
|
+
items: patch.items,
|
|
568
|
+
},
|
|
569
|
+
} as SanityInsertPatchOperation,
|
|
570
|
+
...serializePatches(rest),
|
|
571
|
+
]
|
|
572
|
+
}
|
|
573
|
+
case 'reorder': {
|
|
574
|
+
if (curr) return [curr, ...serializePatches(patches)]
|
|
575
|
+
|
|
576
|
+
// REORDER STRATEGY: Two-phase approach to avoid key collisions
|
|
577
|
+
//
|
|
578
|
+
// Problem: Direct key swaps can cause collisions. For example, swapping A↔B:
|
|
579
|
+
// - Set A's content to B: ✓
|
|
580
|
+
// - Set B's content to A: ✗ (A's content was already overwritten)
|
|
581
|
+
//
|
|
582
|
+
// Solution: Use temporary keys as an intermediate step
|
|
583
|
+
// Phase 1: Move all items to temporary keys with their final content
|
|
584
|
+
// Phase 2: Update just the _key property to restore the final keys
|
|
585
|
+
|
|
586
|
+
// Phase 1: Move items to collision-safe temporary keys
|
|
587
|
+
const tempKeyOperations: SanityPatchOperations = {}
|
|
588
|
+
tempKeyOperations.set = {}
|
|
589
|
+
|
|
590
|
+
for (const {sourceKey, targetKey} of patch.reorders) {
|
|
591
|
+
const temporaryKey = `__temp_reorder_${sourceKey}__`
|
|
592
|
+
const finalContentForThisPosition =
|
|
593
|
+
patch.snapshot[getIndexForKey(patch.snapshot, targetKey)]
|
|
594
|
+
|
|
595
|
+
Object.assign(tempKeyOperations.set, {
|
|
596
|
+
[pathToString([...patch.path, {_key: sourceKey}])]: {
|
|
597
|
+
...finalContentForThisPosition,
|
|
598
|
+
_key: temporaryKey,
|
|
599
|
+
},
|
|
600
|
+
})
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// Phase 2: Update _key properties to restore the intended final keys
|
|
604
|
+
const finalKeyOperations: SanityPatchOperations = {}
|
|
605
|
+
finalKeyOperations.set = {}
|
|
606
|
+
|
|
607
|
+
for (const {sourceKey, targetKey} of patch.reorders) {
|
|
608
|
+
const temporaryKey = `__temp_reorder_${sourceKey}__`
|
|
609
|
+
|
|
610
|
+
Object.assign(finalKeyOperations.set, {
|
|
611
|
+
[pathToString([...patch.path, {_key: temporaryKey}, '_key'])]: targetKey,
|
|
612
|
+
})
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
return [tempKeyOperations, finalKeyOperations, ...serializePatches(rest)]
|
|
616
|
+
}
|
|
617
|
+
default: {
|
|
618
|
+
return []
|
|
619
|
+
}
|
|
620
|
+
}
|
|
520
621
|
}
|
|
521
622
|
|
|
522
623
|
function isUniquelyKeyed(arr: unknown[]): arr is KeyedSanityObject[] {
|
|
523
|
-
const
|
|
624
|
+
const seenKeys = new Set<string>()
|
|
524
625
|
|
|
525
|
-
for (
|
|
526
|
-
|
|
527
|
-
if (!
|
|
528
|
-
|
|
529
|
-
|
|
626
|
+
for (const item of arr) {
|
|
627
|
+
// Each item must be a keyed object with a _key property
|
|
628
|
+
if (!isKeyedObject(item)) return false
|
|
629
|
+
|
|
630
|
+
// Each _key must be unique within the array
|
|
631
|
+
if (seenKeys.has(item._key)) return false
|
|
530
632
|
|
|
531
|
-
|
|
633
|
+
seenKeys.add(item._key)
|
|
532
634
|
}
|
|
533
635
|
|
|
534
636
|
return true
|
|
535
637
|
}
|
|
536
638
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
639
|
+
// Cache to avoid recomputing key-to-index mappings for the same array
|
|
640
|
+
const keyToIndexCache = new WeakMap<KeyedSanityObject[], Record<string, number>>()
|
|
641
|
+
|
|
642
|
+
function getIndexForKey(keyedArray: KeyedSanityObject[], targetKey: string) {
|
|
643
|
+
const cachedMapping = keyToIndexCache.get(keyedArray)
|
|
644
|
+
if (cachedMapping) return cachedMapping[targetKey]
|
|
540
645
|
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
(
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
return acc
|
|
646
|
+
// Build a mapping from _key to array index
|
|
647
|
+
const keyToIndexMapping = keyedArray.reduce<Record<string, number>>(
|
|
648
|
+
(mapping, {_key}, arrayIndex) => {
|
|
649
|
+
mapping[_key] = arrayIndex
|
|
650
|
+
return mapping
|
|
547
651
|
},
|
|
548
|
-
{
|
|
652
|
+
{},
|
|
549
653
|
)
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
function arrayIsEqual(itemA: unknown[], itemB: unknown[]) {
|
|
553
|
-
return itemA.length === itemB.length && itemA.every((item, i) => itemB[i] === item)
|
|
554
|
-
}
|
|
555
654
|
|
|
556
|
-
|
|
557
|
-
if (typeof item !== 'undefined') {
|
|
558
|
-
return item
|
|
559
|
-
}
|
|
655
|
+
keyToIndexCache.set(keyedArray, keyToIndexMapping)
|
|
560
656
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
console.warn(`undefined value in array converted to null (at '${serializedPath}')`)
|
|
564
|
-
}
|
|
657
|
+
return keyToIndexMapping[targetKey]
|
|
658
|
+
}
|
|
565
659
|
|
|
566
|
-
|
|
660
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
661
|
+
return typeof value === 'object' && !!value && !Array.isArray(value)
|
|
567
662
|
}
|
|
568
663
|
|
|
569
|
-
|
|
570
|
-
|
|
664
|
+
/**
|
|
665
|
+
* Simplify returns `null` if the value given was `undefined`. This behavior
|
|
666
|
+
* is the same as how `JSON.stringify` works so this is relatively expected
|
|
667
|
+
* behavior.
|
|
668
|
+
*/
|
|
669
|
+
function nullifyUndefined(item: unknown) {
|
|
670
|
+
if (item === undefined) return null
|
|
671
|
+
return item
|
|
571
672
|
}
|
|
572
673
|
|
|
573
674
|
function yes(_: unknown) {
|