lib/helper/diff.js
function diffArray(arr2, arr1, diff, diffFn) {
  if(!diff) return _.simpleDiff(arr2, arr1);
  var matrix = ld(arr1, arr2, diffFn)
  var n = arr1.length;
  var i = n;
  var m = arr2.length;
  var j = m;
  var edits = [];
  var current = matrix[i][j];
  while(i>0 || j>0){
  // the last line
    if (i === 0) {
      edits.unshift(3);
      j--;
      continue;
    }
    // the last col
    if (j === 0) {
      edits.unshift(2);
      i--;
      continue;
    }
    var northWest = matrix[i - 1][j - 1];
    var west = matrix[i - 1][j];
    var north = matrix[i][j - 1];

    var min = Math.min(north, west, northWest);

    if (min === west) {
      edits.unshift(2); //delete
      i--;
      current = west;
    } else if (min === northWest ) {
      if (northWest === current) {
        edits.unshift(0); //no change
      } else {
        edits.unshift(1); //update
        current = northWest;
      }
      i--;
      j--;
    } else {
      edits.unshift(3); //add
      j--;
      current = north;
    }
  }
  var LEAVE = 0;
  var ADD = 3;
  var DELELE = 2;
  var UPDATE = 1;
  var n = 0;m=0;
  var steps = [];
  var step = { index: null, add:0, removed:[] };

  for(var i=0;i<edits.length;i++){
    if(edits[i] > 0 ){ // NOT LEAVE
      if(step.index === null){
        step.index = m;
      }
    } else { //LEAVE
      if(step.index != null){
        steps.push(step)
        step = {index: null, add:0, removed:[]};
      }
    }
    switch(edits[i]){
      case LEAVE:
        n++;
        m++;
        break;
      case ADD:
        step.add++;
        m++;
        break;
      case DELELE:
        step.removed.push(arr1[n])
        n++;
        break;
      case UPDATE:
        step.add++;
        step.removed.push(arr1[n])
        n++;
        m++;
        break;
    }
  }
  if(step.index != null){
    steps.push(step)
  }
  return steps
}
function diffArray(arr2, arr1, diff, diffFn) {
  if(!diff) return _.simpleDiff(arr2, arr1);
  var matrix = ld(arr1, arr2, diffFn)
  var n = arr1.length;
  var i = n;
  var m = arr2.length;
  var j = m;
  var edits = [];
  var current = matrix[i][j];
  while(i>0 || j>0){
  // the last line
    if (i === 0) {
      edits.unshift(3);
      j--;
      continue;
    }
    // the last col
    if (j === 0) {
      edits.unshift(2);
      i--;
      continue;
    }
    var northWest = matrix[i - 1][j - 1];
    var west = matrix[i - 1][j];
    var north = matrix[i][j - 1];

    var min = Math.min(north, west, northWest);

    if (min === west) {
      edits.unshift(2); //delete
      i--;
      current = west;
    } else if (min === northWest ) {
      if (northWest === current) {
        edits.unshift(0); //no change
      } else {
        edits.unshift(1); //update
        current = northWest;
      }
      i--;
      j--;
    } else {
      edits.unshift(3); //add
      j--;
      current = north;
    }
  }
  var LEAVE = 0;
  var ADD = 3;
  var DELELE = 2;
  var UPDATE = 1;
  var n = 0;m=0;
  var steps = [];
  var step = { index: null, add:0, removed:[] };

  for(var i=0;i<edits.length;i++){
    if(edits[i] > 0 ){ // NOT LEAVE
      if(step.index === null){
        step.index = m;
      }
    } else { //LEAVE
      if(step.index != null){
        steps.push(step)
        step = {index: null, add:0, removed:[]};
      }
    }
    switch(edits[i]){
      case LEAVE:
        n++;
        m++;
        break;
      case ADD:
        step.add++;
        m++;
        break;
      case DELELE:
        step.removed.push(arr1[n])
        n++;
        break;
      case UPDATE:
        step.add++;
        step.removed.push(arr1[n])
        n++;
        m++;
        break;
    }
  }
  if(step.index != null){
    steps.push(step)
  }
  return steps
}
function diffArray ( arr2 , arr1 , diff , diffFn ) { if ( ! diff ) return _ . simpleDiff ( arr2 , arr1 ) ; var matrix = ld ( arr1 , arr2 , diffFn ) var n = arr1 . length ; var i = n ; var m = arr2 . length ; var j = m ; var edits = [ ] ; var current = matrix [ i ] [ j ] ; while ( i > 0 || j > 0 ) { // the last line if ( i === 0 ) { edits . unshift ( 3 ) ; j -- ; continue ; } // the last col if ( j === 0 ) { edits . unshift ( 2 ) ; i -- ; continue ; } var northWest = matrix [ i - 1 ] [ j - 1 ] ; var west = matrix [ i - 1 ] [ j ] ; var north = matrix [ i ] [ j - 1 ] ; var min = Math . min ( north , west , northWest ) ; if ( min === west ) { edits . unshift ( 2 ) ; //delete i -- ; current = west ; } else if ( min === northWest ) { if ( northWest === current ) { edits . unshift ( 0 ) ; //no change } else { edits . unshift ( 1 ) ; //update current = northWest ; } i -- ; j -- ; } else { edits . unshift ( 3 ) ; //add j -- ; current = north ; } } var LEAVE = 0 ; var ADD = 3 ; var DELELE = 2 ; var UPDATE = 1 ; var n = 0 ; m = 0 ; var steps = [ ] ; var step = { index : null , add : 0 , removed : [ ] } ; for ( var i = 0 ; i < edits . length ; i ++ ) { if ( edits [ i ] > 0 ) { // NOT LEAVE if ( step . index === null ) { step . index = m ; } } else { //LEAVE if ( step . index != null ) { steps . push ( step ) step = { index : null , add : 0 , removed : [ ] } ; } } switch ( edits [ i ] ) { case LEAVE : n ++ ; m ++ ; break ; case ADD : step . add ++ ; m ++ ; break ; case DELELE : step . removed . push ( arr1 [ n ] ) n ++ ; break ; case UPDATE : step . add ++ ; step . removed . push ( arr1 [ n ] ) n ++ ; m ++ ; break ; } } if ( step . index != null ) { steps . push ( step ) } return steps }
arr2 - new array arr1 - old array
arr2 - new array arr1 - old array