Permalink
Browse files

Make all prefix-matches score the same.

This allows for alphabetical sorting in the case there are many items
with the same prefix.
  • Loading branch information...
ConradIrwin committed Jun 22, 2011
1 parent a183d78 commit 0fbf80ca54c92e3f236121ca459c8308e2dfb4d3
Showing with 14 additions and 12 deletions.
  1. +10 −8 jquery.fuzzymatch.js
  2. +3 −3 jquery.fuzzymatch.min.js
  3. +1 −1 spec.html
View
@@ -1,5 +1,5 @@
/**
- * jQuery.fuzzyMatch.js, version 0.1 (2011-06-19)
+ * jQuery.fuzzyMatch.js, version 0.2 (2011-06-22)
*
* https://github.com/rapportive-oss/jquery-fuzzymatch
*
@@ -50,14 +50,16 @@
// 1000 characters are inserted between matches.
PENALTY_CASE_MISMATCH = 0.9999,
- // The goodness of matches should decay slightly with trailing
- // characters.
+ // If the word has more characters than the user typed, it should
+ // be penalised slightly.
//
- // i.e. "quirk" is more likely than "quirkier" when "qu" is typed.
+ // i.e. "html" is more likely than "html5" if I type "html".
//
- // This will not change the order of suggestions based on SCORE_* until
- // 10000 characters are appended.
- PENALTY_TRAILING = 0.99999;
+ // However, it may well be the case that there's a sensible secondary
+ // ordering (like alphabetical) that it makes sense to rely on when
+ // there are many prefix matches, so we don't make the penalty increase
+ // with the number of tokens.
+ PENALTY_NOT_COMPLETE = 0.99;
/**
* Generates all possible split objects by splitting a string around a
@@ -123,7 +125,7 @@
$.fuzzyMatch = function (string, abbreviation) {
if (abbreviation === "") {
return {
- score: Math.pow(PENALTY_TRAILING, string.length),
+ score: string === "" ? SCORE_CONTINUE_MATCH : PENALTY_NOT_COMPLETE,
html: $('<div>').text(string).html()
};
}
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -21,7 +21,7 @@
it("should prefer exact matches over prefix matches", function () {
expect($.fuzzyMatch("grab", "grab").score).toBeGreaterThan($.fuzzyMatch("grabbed", "grab").score);
- expect($.fuzzyMatch("grabbed", "grab").score).toBeGreaterThan($.fuzzyMatch("grabbing", "grab").score);
+ expect($.fuzzyMatch("grabbed", "grab").score).toEqual($.fuzzyMatch("grabbing", "grab").score);
});
it("should prefer closer-together matches over further-apart matches", function () {

0 comments on commit 0fbf80c

Please sign in to comment.