aboutsummaryrefslogtreecommitdiff
path: root/src-vue/src/utils
diff options
context:
space:
mode:
authorGeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com>2023-05-15 17:50:39 +0200
committerGitHub <noreply@github.com>2023-05-15 17:50:39 +0200
commit5edffe63d3b6f503ad29da08f281ea88682f5f90 (patch)
treed11d54223f2e2a0fefcf53f9506cf3aa0edbfe30 /src-vue/src/utils
parent002d967aac31a7c7b883cc6f2003e2db7afa6247 (diff)
downloadFlightCore-5edffe63d3b6f503ad29da08f281ea88682f5f90.tar.gz
FlightCore-5edffe63d3b6f503ad29da08f281ea88682f5f90.zip
feat: Make search fuzzy (#342)
* feat: Enable fuzzy search for Thunderstore mods * feat: Enable fuzzy search for pull requests * refactor: Move filter into own util file * docs: Add comment explaining function in detail
Diffstat (limited to 'src-vue/src/utils')
-rw-r--r--src-vue/src/utils/filter.ts26
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 };