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.
This commit is contained in:
parent
016930f933
commit
3f9f218ed0
1 changed files with 37 additions and 0 deletions
37
composables/string.ts
Normal file
37
composables/string.ts
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue