@@ -45,6 +45,7 @@ function matchItems(array1, array2, index1, index2, context) {
4545 }
4646 let objectHash = context . objectHash ;
4747 if ( ! objectHash ) {
48+
4849 // no way to match objects was provided, try match by position
4950 return context . matchByPosition && index1 === index2 ;
5051 }
@@ -123,6 +124,7 @@ export const diffFilter = function arraysDiffFilter(context) {
123124 context . push ( child , index ) ;
124125 commonHead ++ ;
125126 }
127+
126128 // separate common tail
127129 while (
128130 commonTail + commonHead < len1 &&
@@ -144,31 +146,35 @@ export const diffFilter = function arraysDiffFilter(context) {
144146 let result ;
145147 if ( commonHead + commonTail === len1 ) {
146148 if ( len1 === len2 ) {
149+
147150 // arrays are identical
148151 context . setResult ( undefined ) . exit ( ) ;
149152 return ;
150153 }
154+
151155 // trivial case, a block (1 or more consecutive items) was added
152156 result = result || {
153157 _t : 'a' ,
154158 } ;
155159 for ( index = commonHead ; index < len2 - commonTail ; index ++ ) {
156- result [ index ] = [ array2 [ index ] ] ;
160+ result [ index ] = [ array2 [ index ] ] ;
157161 }
158162 context . setResult ( result ) . exit ( ) ;
159163 return ;
160164 }
161165 if ( commonHead + commonTail === len2 ) {
166+
162167 // trivial case, a block (1 or more consecutive items) was removed
163168 result = result || {
164169 _t : 'a' ,
165170 } ;
166171 for ( index = commonHead ; index < len1 - commonTail ; index ++ ) {
167- result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
172+ result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
168173 }
169174 context . setResult ( result ) . exit ( ) ;
170175 return ;
171176 }
177+
172178 // reset hash cache
173179 delete matchContext . hashCache1 ;
174180 delete matchContext . hashCache2 ;
@@ -183,8 +189,9 @@ export const diffFilter = function arraysDiffFilter(context) {
183189 } ;
184190 for ( index = commonHead ; index < len1 - commonTail ; index ++ ) {
185191 if ( arrayIndexOf ( seq . indices1 , index - commonHead ) < 0 ) {
192+
186193 // removed
187- result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
194+ result [ `_${ index } ` ] = [ array1 [ index ] , 0 , 0 ] ;
188195 removedItems . push ( index ) ;
189196 }
190197 }
@@ -210,6 +217,7 @@ export const diffFilter = function arraysDiffFilter(context) {
210217 for ( index = commonHead ; index < len2 - commonTail ; index ++ ) {
211218 let indexOnArray2 = arrayIndexOf ( seq . indices2 , index - commonHead ) ;
212219 if ( indexOnArray2 < 0 ) {
220+
213221 // added, try to match with a removed item and register as position move
214222 let isMove = false ;
215223 if ( detectMove && removedItemsLength > 0 ) {
@@ -228,9 +236,11 @@ export const diffFilter = function arraysDiffFilter(context) {
228236 matchContext
229237 )
230238 ) {
239+
231240 // store position move as: [originalValue, newPosition, ARRAY_MOVE]
232241 result [ `_${ index1 } ` ] . splice ( 1 , 2 , index , ARRAY_MOVE ) ;
233242 if ( ! includeValueOnMove ) {
243+
234244 // don't include moved value on diff, to save bytes
235245 result [ `_${ index1 } ` ] [ 0 ] = '' ;
236246 }
@@ -248,10 +258,12 @@ export const diffFilter = function arraysDiffFilter(context) {
248258 }
249259 }
250260 if ( ! isMove ) {
261+
251262 // added
252- result [ index ] = [ array2 [ index ] ] ;
263+ result [ index ] = [ array2 [ index ] ] ;
253264 }
254265 } else {
266+
255267 // match, do inner diff
256268 index1 = seq . indices1 [ indexOnArray2 ] + commonHead ;
257269 index2 = seq . indices2 [ indexOnArray2 ] + commonHead ;
@@ -293,23 +305,26 @@ export const patchFilter = function nestedPatchFilter(context) {
293305 for ( index in delta ) {
294306 if ( index !== '_t' ) {
295307 if ( index [ 0 ] === '_' ) {
308+
296309 // removed item from original array
297310 if ( delta [ index ] [ 2 ] === 0 || delta [ index ] [ 2 ] === ARRAY_MOVE ) {
298311 toRemove . push ( parseInt ( index . slice ( 1 ) , 10 ) ) ;
299312 } else {
300313 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,' +
302315 ` invalid diff type: ${ delta [ index ] [ 2 ] } `
303316 ) ;
304317 }
305318 } else {
306319 if ( delta [ index ] . length === 1 ) {
320+
307321 // added item at new array
308322 toInsert . push ( {
309323 index : parseInt ( index , 10 ) ,
310324 value : delta [ index ] [ 0 ] ,
311325 } ) ;
312326 } else {
327+
313328 // modified item at new array
314329 toModify . push ( {
315330 index : parseInt ( index , 10 ) ,
@@ -327,6 +342,7 @@ export const patchFilter = function nestedPatchFilter(context) {
327342 let indexDiff = delta [ `_${ index1 } ` ] ;
328343 let removedValue = array . splice ( index1 , 1 ) [ 0 ] ;
329344 if ( indexDiff [ 2 ] === ARRAY_MOVE ) {
345+
330346 // reinsert later
331347 toInsert . push ( {
332348 index : indexDiff [ 1 ] ,
@@ -366,7 +382,7 @@ export const patchFilter = function nestedPatchFilter(context) {
366382patchFilter . filterName = 'arrays' ;
367383
368384export const collectChildrenPatchFilter = function collectChildrenPatchFilter (
369- context
385+ context
370386) {
371387 if ( ! context || ! context . children ) {
372388 return ;
0 commit comments