From 678ea26f993921bc86385a37f15445f69fe95a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Tue, 28 Feb 2017 13:52:18 +0100 Subject: [PATCH] [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. --- addons/web/static/lib/fuzzy-master/fuzzy.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/addons/web/static/lib/fuzzy-master/fuzzy.js b/addons/web/static/lib/fuzzy-master/fuzzy.js index 0103eaa85bc..bdaf6af5adf 100644 --- a/addons/web/static/lib/fuzzy-master/fuzzy.js +++ b/addons/web/static/lib/fuzzy-master/fuzzy.js @@ -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; }