[FIX] web: fix fuzzy search algorithm in app switcher

The fuzzy search score algorithm is quite simple, but had an interesting
problem: for the search string 'AB DEF', the result 'XXAXXBXXXXXXX DEF' had
a higher score than the result 'ABX X DEF", because it has 4 consecutive
matching characters.

This commit slightly tweak the algorithm to take space into account, but
to make sure they are not used to compound the score for a string.  This
means that space characters are not ignored, but are not really
as important as a normal character.
This commit is contained in:
Géry Debongnie
2017-02-28 15:01:24 +01:00
parent e8ae724b6c
commit 678ea26f99
+12 -3
View File
@@ -57,15 +57,24 @@ fuzzy.match = function(pattern, string, opts) {
for(var idx = 0; idx < len; idx++) {
ch = string[idx];
if(compareString[idx] === pattern[patternIdx]) {
if (pattern[patternIdx] === ' ') {
// we don't want a space character to accumulate a larger score
currScore = 1 - idx/200;
} else {
// consecutive characters should increase the score more than linearly
currScore += 1 + currScore - idx/200;
}
ch = pre + ch + post;
patternIdx += 1;
// consecutive characters should increase the score more than linearly
currScore += 1 + currScore - idx/1000;
} else {
currScore = 0;
}
totalScore += currScore;
if (compareString[idx] === ' ') {
// we don't want characters after a space to accumulate a larger score
currScore = 0;
}
result[result.length] = ch;
}