@@ -45,6 +45,7 @@ function matchItems(array1, array2, index1, index2, context) {
45
45
}
46
46
let objectHash = context . objectHash ;
47
47
if ( ! objectHash ) {
48
+
48
49
// no way to match objects was provided, try match by position
49
50
return context . matchByPosition && index1 === index2 ;
50
51
}
@@ -123,6 +124,7 @@ export const diffFilter = function arraysDiffFilter(context) {
123
124
context . push ( child , index ) ;
124
125
commonHead ++ ;
125
126
}
127
+
126
128
// separate common tail
127
129
while (
128
130
commonTail + commonHead < len1 &&
@@ -144,31 +146,35 @@ export const diffFilter = function arraysDiffFilter(context) {
144
146
let result ;
145
147
if ( commonHead + commonTail === len1 ) {
146
148
if ( len1 === len2 ) {
149
+
147
150
// arrays are identical
148
151
context . setResult ( undefined ) . exit ( ) ;
149
152
return ;
150
153
}
154
+
151
155
// trivial case, a block (1 or more consecutive items) was added
152
156
result = result || {
153
157
_t : 'a' ,
154
158
} ;
155
159
for ( index = commonHead ; index < len2 - commonTail ; index ++ ) {
156
- result [ index ] = [ array2 [ index ] ] ;
160
+ result [ index ] = [ array2 [ index ] ] ;
157
161
}
158
162
context . setResult ( result ) . exit ( ) ;
159
163
return ;
160
164
}
161
165
if ( commonHead + commonTail === len2 ) {
166
+
162
167
// trivial case, a block (1 or more consecutive items) was removed
163
168
result = result || {
164
169
_t : 'a' ,
165
170
} ;
166
171
for ( index = commonHead ; index < len1 - commonTail ; index ++ ) {
167
- result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
172
+ result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
168
173
}
169
174
context . setResult ( result ) . exit ( ) ;
170
175
return ;
171
176
}
177
+
172
178
// reset hash cache
173
179
delete matchContext . hashCache1 ;
174
180
delete matchContext . hashCache2 ;
@@ -183,8 +189,9 @@ export const diffFilter = function arraysDiffFilter(context) {
183
189
} ;
184
190
for ( index = commonHead ; index < len1 - commonTail ; index ++ ) {
185
191
if ( arrayIndexOf ( seq . indices1 , index - commonHead ) < 0 ) {
192
+
186
193
// removed
187
- result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
194
+ result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
188
195
removedItems . push ( index ) ;
189
196
}
190
197
}
@@ -210,6 +217,7 @@ export const diffFilter = function arraysDiffFilter(context) {
210
217
for ( index = commonHead ; index < len2 - commonTail ; index ++ ) {
211
218
let indexOnArray2 = arrayIndexOf ( seq . indices2 , index - commonHead ) ;
212
219
if ( indexOnArray2 < 0 ) {
220
+
213
221
// added, try to match with a removed item and register as position move
214
222
let isMove = false ;
215
223
if ( detectMove && removedItemsLength > 0 ) {
@@ -228,9 +236,11 @@ export const diffFilter = function arraysDiffFilter(context) {
228
236
matchContext
229
237
)
230
238
) {
239
+
231
240
// store position move as: [originalValue, newPosition, ARRAY_MOVE]
232
241
result [ `_${ index1 } ` ] . splice ( 1 , 2 , index , ARRAY_MOVE ) ;
233
242
if ( ! includeValueOnMove ) {
243
+
234
244
// don't include moved value on diff, to save bytes
235
245
result [ `_${ index1 } ` ] [ 0 ] = '' ;
236
246
}
@@ -248,10 +258,12 @@ export const diffFilter = function arraysDiffFilter(context) {
248
258
}
249
259
}
250
260
if ( ! isMove ) {
261
+
251
262
// added
252
- result [ index ] = [ array2 [ index ] ] ;
263
+ result [ index ] = [ array2 [ index ] ] ;
253
264
}
254
265
} else {
266
+
255
267
// match, do inner diff
256
268
index1 = seq . indices1 [ indexOnArray2 ] + commonHead ;
257
269
index2 = seq . indices2 [ indexOnArray2 ] + commonHead ;
@@ -293,23 +305,26 @@ export const patchFilter = function nestedPatchFilter(context) {
293
305
for ( index in delta ) {
294
306
if ( index !== '_t' ) {
295
307
if ( index [ 0 ] === '_' ) {
308
+
296
309
// removed item from original array
297
310
if ( delta [ index ] [ 2 ] === 0 || delta [ index ] [ 2 ] === ARRAY_MOVE ) {
298
311
toRemove . push ( parseInt ( index . slice ( 1 ) , 10 ) ) ;
299
312
} else {
300
313
throw new Error (
301
- ` only removal or move can be applied at original array indices,` +
314
+ ' only removal or move can be applied at original array indices,' +
302
315
` invalid diff type: ${ delta [ index ] [ 2 ] } `
303
316
) ;
304
317
}
305
318
} else {
306
319
if ( delta [ index ] . length === 1 ) {
320
+
307
321
// added item at new array
308
322
toInsert . push ( {
309
323
index : parseInt ( index , 10 ) ,
310
324
value : delta [ index ] [ 0 ] ,
311
325
} ) ;
312
326
} else {
327
+
313
328
// modified item at new array
314
329
toModify . push ( {
315
330
index : parseInt ( index , 10 ) ,
@@ -327,6 +342,7 @@ export const patchFilter = function nestedPatchFilter(context) {
327
342
let indexDiff = delta [ `_${ index1 } ` ] ;
328
343
let removedValue = array . splice ( index1 , 1 ) [ 0 ] ;
329
344
if ( indexDiff [ 2 ] === ARRAY_MOVE ) {
345
+
330
346
// reinsert later
331
347
toInsert . push ( {
332
348
index : indexDiff [ 1 ] ,
@@ -366,7 +382,7 @@ export const patchFilter = function nestedPatchFilter(context) {
366
382
patchFilter . filterName = 'arrays' ;
367
383
368
384
export const collectChildrenPatchFilter = function collectChildrenPatchFilter (
369
- context
385
+ context
370
386
) {
371
387
if ( ! context || ! context . children ) {
372
388
return ;
0 commit comments