dist/fuzzy-match-utils.js
function typeaheadSimilarity(a, b) {
    var aLength = a.length;
    var bLength = b.length;
    var table = [];

    if (!aLength || !bLength) {
        return 0;
    }

    // Ensure `a` isn't shorter than `b`.
    if (aLength < bLength) {
        var _ref2 = [b, a];
        a = _ref2[0];
        b = _ref2[1];
    }

    // Early exit if `a` includes `b`; these will be scored higher than any
    // other options with the same `b` (filter string), with a preference for
    // shorter `a` strings (option labels).
    if (a.indexOf(b) !== -1) {
        return bLength + 1 / aLength;
    }

    // Initialize the table axes:
    //
    //    0 0 0 0 ... bLength
    //    0
    //    0
    //
    //   ...
    //
    // aLength
    //
    for (var x = 0; x <= aLength; ++x) {
        table[x] = [0];
    }
    for (var y = 0; y <= bLength; ++y) {
        table[0][y] = 0;
    }

    // Populate the rest of the table with a dynamic programming algorithm.
    for (var _x = 1; _x <= aLength; ++_x) {
        for (var _y = 1; _y <= bLength; ++_y) {
            table[_x][_y] = a[_x - 1] === b[_y - 1] ? 1 + table[_x - 1][_y - 1] : Math.max(table[_x][_y - 1], table[_x - 1][_y]);
        }
    }

    return table[aLength][bLength];
}
function typeaheadSimilarity(a, b) {
    var aLength = a.length;
    var bLength = b.length;
    var table = [];

    if (!aLength || !bLength) {
        return 0;
    }

    // Ensure `a` isn't shorter than `b`.
    if (aLength < bLength) {
        var _ref2 = [b, a];
        a = _ref2[0];
        b = _ref2[1];
    }

    // Early exit if `a` includes `b`; these will be scored higher than any
    // other options with the same `b` (filter string), with a preference for
    // shorter `a` strings (option labels).
    if (a.indexOf(b) !== -1) {
        return bLength + 1 / aLength;
    }

    // Initialize the table axes:
    //
    //    0 0 0 0 ... bLength
    //    0
    //    0
    //
    //   ...
    //
    // aLength
    //
    for (var x = 0; x <= aLength; ++x) {
        table[x] = [0];
    }
    for (var y = 0; y <= bLength; ++y) {
        table[0][y] = 0;
    }

    // Populate the rest of the table with a dynamic programming algorithm.
    for (var _x = 1; _x <= aLength; ++_x) {
        for (var _y = 1; _y <= bLength; ++_y) {
            table[_x][_y] = a[_x - 1] === b[_y - 1] ? 1 + table[_x - 1][_y - 1] : Math.max(table[_x][_y - 1], table[_x - 1][_y]);
        }
    }

    return table[aLength][bLength];
}
function typeaheadSimilarity ( a , b ) { var aLength = a . length ; var bLength = b . length ; var table = [ ] ; if ( ! aLength || ! bLength ) { return 0 ; } // Ensure `a` isn't shorter than `b`. if ( aLength < bLength ) { var _ref2 = [ b , a ] ; a = _ref2 [ 0 ] ; b = _ref2 [ 1 ] ; } // Early exit if `a` includes `b`; these will be scored higher than any // other options with the same `b` (filter string), with a preference for // shorter `a` strings (option labels). if ( a . indexOf ( b ) !== - 1 ) { return bLength + 1 / aLength ; } // Initialize the table axes: // //    0 0 0 0 ... bLength //    0 //    0 // //   ... // // aLength // for ( var x = 0 ; x <= aLength ; ++ x ) { table [ x ] = [ 0 ] ; } for ( var y = 0 ; y <= bLength ; ++ y ) { table [ 0 ] [ y ] = 0 ; } // Populate the rest of the table with a dynamic programming algorithm. for ( var _x = 1 ; _x <= aLength ; ++ _x ) { for ( var _y = 1 ; _y <= bLength ; ++ _y ) { table [ _x ] [ _y ] = a [ _x - 1 ] === b [ _y - 1 ] ? 1 + table [ _x - 1 ] [ _y - 1 ] : Math . max ( table [ _x ] [ _y - 1 ] , table [ _x - 1 ] [ _y ] ) ; } } return table [ aLength ] [ bLength ] ; }
Scores the similarity between two strings by returning the length of the
longest common subsequence. Intended for comparing strings of different
lengths; eg. when matching a typeahead search input with a school name.

Meant for use in an instant search box where results are being fetched
as a user is typing.

@param  a  The longer string (though, we flip them if it's shorter).
@param  b  The shorter string, eg. a typeahead search input.

@return The length of the longest common subsequence. Higher scores indicate
closer matches.
Scores the similarity between two strings by returning the length of the longest common subsequence . Intended for comparing strings of different lengths ; eg . when matching a typeahead search input with a school name .