blob: b85b9623e206c32e495b9783757934503c0544a3 (
plain)
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
|
/**
* Implements a fuzzy filter
* Iterates through chars of `search_term` and checks if each char exists in consecutive order in `text`.
* For example, this means that `text="Gecko"` and `search_term="geo"` will return `true`
* but using `text="Gecko"` and `search_term="goe"` will return `false`
*
* Implements a subset of "fuzzy string searching"
* https://en.wikipedia.org/wiki/Approximate_string_matching
*/
function fuzzy_filter(text: string, search_term: string): boolean {
const lowercase_text = text.toLowerCase();
const lowercase_search_term = search_term.toLowerCase();
let previousIndex = -1;
for (let i = 0; i < lowercase_search_term.length; i++) {
const char = lowercase_search_term[i];
const currentIndex = lowercase_text.indexOf(char, previousIndex + 1);
if (currentIndex === -1) {
return false;
}
previousIndex = currentIndex;
}
return true;
}
export { fuzzy_filter };
|