/
search.js
59 lines (51 loc) · 1.69 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/** @odoo-module */
/**
* This private function computes a score that represent the fact that the
* string contains the pattern, or not
*
* - If the score is 0, the string does not contain the letters of the pattern in
* the correct order.
* - if the score is > 0, it actually contains the letters.
*
* Better matches will get a higher score: consecutive letters are better,
* and a match closer to the beginning of the string is also scored higher.
*/
function match(pattern, str) {
let totalScore = 0;
let currentScore = 0;
let len = str.length;
let patternIndex = 0;
pattern = pattern.toLowerCase();
str = str.toLowerCase();
for (let i = 0; i < len; i++) {
if (str[i] === pattern[patternIndex]) {
patternIndex++;
currentScore += 100 + currentScore - i / 200;
} else {
currentScore = 0;
}
totalScore = totalScore + currentScore;
}
return patternIndex === pattern.length ? totalScore : 0;
}
/**
* Return a list of things that matches a pattern, ordered by their 'score' (
* higher score first). An higher score means that the match is better. For
* example, consecutive letters are considered a better match.
*/
export function fuzzyLookup(pattern, list, fn) {
const results = [];
list.forEach((data) => {
const score = match(pattern, fn(data));
if (score > 0) {
results.push({ score, elem: data });
}
});
// we want better matches first
results.sort((a, b) => b.score - a.score);
return results.map((r) => r.elem);
}
// Does `pattern` fuzzy match `string`?
export function fuzzyTest(pattern, string) {
return match(pattern, string) !== 0;
}