lib/diff.js
function matrixToOperations (matrix, i, j) {
  if (i === 0 && j === 0) {
    return [];
  } else if (i === 0) {
    return matrixToOperations(matrix, i, j - 1).concat("add");
  } else if (j === 0) {
    return matrixToOperations(matrix, i - 1, j).concat("remove");
  } else {
    var left = matrix[i][j - 1];
    var up = matrix[i - 1][j];
    var upleft = matrix[i - 1][j - 1];

    if (upleft <= up && upleft <= left) {
      if (upleft === matrix[i][j]) {
        return matrixToOperations(matrix, i - 1, j - 1).concat("eq");
      } else {
        return matrixToOperations(matrix, i - 1, j - 1).concat("replace");
      }
    } else if (left <= upleft && left <= up) {
      return matrixToOperations(matrix, i, j - 1).concat("add");
    } else {
      return matrixToOperations(matrix, i - 1, j).concat("remove");
    }
  }
}
function matrixToOperations (matrix, i, j) {
  if (i === 0 && j === 0) {
    return [];
  } else if (i === 0) {
    return matrixToOperations(matrix, i, j - 1).concat("add");
  } else if (j === 0) {
    return matrixToOperations(matrix, i - 1, j).concat("remove");
  } else {
    var left = matrix[i][j - 1];
    var up = matrix[i - 1][j];
    var upleft = matrix[i - 1][j - 1];

    if (upleft <= up && upleft <= left) {
      if (upleft === matrix[i][j]) {
        return matrixToOperations(matrix, i - 1, j - 1).concat("eq");
      } else {
        return matrixToOperations(matrix, i - 1, j - 1).concat("replace");
      }
    } else if (left <= upleft && left <= up) {
      return matrixToOperations(matrix, i, j - 1).concat("add");
    } else {
      return matrixToOperations(matrix, i - 1, j).concat("remove");
    }
  }
}
function matrixToOperations ( matrix , i , j ) { if ( i === 0 && j === 0 ) { return [ ] ; } else if ( i === 0 ) { return matrixToOperations ( matrix , i , j - 1 ) . concat ( "add" ) ; } else if ( j === 0 ) { return matrixToOperations ( matrix , i - 1 , j ) . concat ( "remove" ) ; } else { var left = matrix [ i ] [ j - 1 ] ; var up = matrix [ i - 1 ] [ j ] ; var upleft = matrix [ i - 1 ] [ j - 1 ] ; if ( upleft <= up && upleft <= left ) { if ( upleft === matrix [ i ] [ j ] ) { return matrixToOperations ( matrix , i - 1 , j - 1 ) . concat ( "eq" ) ; } else { return matrixToOperations ( matrix , i - 1 , j - 1 ) . concat ( "replace" ) ; } } else if ( left <= upleft && left <= up ) { return matrixToOperations ( matrix , i , j - 1 ) . concat ( "add" ) ; } else { return matrixToOperations ( matrix , i - 1 , j ) . concat ( "remove" ) ; } } }
Backtrace an optimal path through a levenshtein distance matrix.

@returns {Array} List of operations
Backtrace an optimal path through a levenshtein distance matrix .