diff options
Diffstat (limited to 'src-vue/src/utils')
-rw-r--r-- | src-vue/src/utils/filter.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src-vue/src/utils/filter.ts b/src-vue/src/utils/filter.ts new file mode 100644 index 00000000..b85b9623 --- /dev/null +++ b/src-vue/src/utils/filter.ts @@ -0,0 +1,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 }; |