File tree 2 files changed +20
-2
lines changed
2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 249
249
if ( target && source && change && change . kind ) {
250
250
var it = target ,
251
251
i = - 1 ,
252
- last = change . path . length - 1 ;
252
+ last = change . path ? change . path . length - 1 : 0 ;
253
253
while ( ++ i < last ) {
254
254
if ( typeof it [ change . path [ i ] ] === 'undefined' ) {
255
255
it [ change . path [ i ] ] = ( typeof change . path [ i ] === 'number' ) ? [ ] : { } ;
258
258
}
259
259
switch ( change . kind ) {
260
260
case 'A' :
261
- applyArrayChange ( it [ change . path [ i ] ] , change . index , change . item ) ;
261
+ applyArrayChange ( change . path ? it [ change . path [ i ] ] : it , change . index , change . item ) ;
262
262
break ;
263
263
case 'D' :
264
264
delete it [ change . path [ i ] ] ;
Original file line number Diff line number Diff line change @@ -482,4 +482,22 @@ describe('deep-diff', function() {
482
482
483
483
} ) ;
484
484
485
+ describe ( 'regression test for bug #35' , function ( ) {
486
+ var lhs = [ "a" , "a" , "a" ] ;
487
+ var rhs = [ "a" ] ;
488
+
489
+ it ( 'can apply diffs between two top level arrays' , function ( ) {
490
+ var differences = deep . diff ( lhs , rhs ) ;
491
+
492
+ /* We must apply the differences in reverse order, since the array indices
493
+ in the diff become stale/invalid if you delete elements from the array
494
+ whose indices are in ascending order */
495
+ for ( var i = differences . length - 1 ; i >= 0 ; i -- ) {
496
+ deep . applyChange ( lhs , true , differences [ i ] ) ;
497
+ }
498
+
499
+ expect ( lhs ) . to . eql ( [ "a" ] ) ;
500
+ } ) ;
501
+ } ) ;
502
+
485
503
} ) ;
You can’t perform that action at this time.
0 commit comments