From 3f9f218ed0cab112928a94f4a3b2b467411f7321 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 27 Jun 2025 18:13:51 +0200 Subject: [PATCH] Add composable for string sort and search To avoid hydration matches string operations need to do the same thing on the client and server. Add utilities for performing sorting and fuzzy matching of strings that uses the account's locale. --- composables/string.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 composables/string.ts diff --git a/composables/string.ts b/composables/string.ts new file mode 100644 index 0000000..480b397 --- /dev/null +++ b/composables/string.ts @@ -0,0 +1,37 @@ +export function useStringSort() { + const accountStore = useAccountStore(); + const localeCompare = computed(() => Intl.Collator( + accountStore.activeLocale, + { + usage: "sort", + numeric: true, + sensitivity: "base", + }, + ).compare); + return function stringSort(a: string, b: string) { + return localeCompare.value(a, b); + } +} + +export function useFuzzyMatch() { + const accountStore = useAccountStore(); + const localeCompare = computed(() => Intl.Collator( + accountStore.activeLocale, + { + usage: "search", + sensitivity: "base", + }, + ).compare); + return function fuzzyMatch(pattern: string, text: string) { + let pos = 0; + for (const char of text) { + if (localeCompare.value(char, pattern[pos]) === 0) { + pos += 1; + if (pos === pattern.length) { + return true; + } + } + } + return pos === pattern.length; + } +}