From f5b4c5d8973465812978f8eafe6560a8150d0837 Mon Sep 17 00:00:00 2001 From: TAKAHASHI Shuuji Date: Sun, 29 Dec 2024 03:50:00 +0900 Subject: [PATCH] fix: allow partial match for package title --- composables/usePackages.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/composables/usePackages.ts b/composables/usePackages.ts index 8f6bff5e..59e01af4 100644 --- a/composables/usePackages.ts +++ b/composables/usePackages.ts @@ -10,7 +10,12 @@ export function usePackages() { idField: 'title', fields: ['title', 'description'], storeFields: fields, + processTerm: (term, fieldName) => + fieldName === 'title' + ? suffixes(term, 1) + : MiniSearch.getDefault('processTerm')(term, fieldName), searchOptions: { + processTerm: MiniSearch.getDefault('processTerm'), prefix: true, fuzzy: 0.4, boost: { @@ -169,3 +174,23 @@ export function usePackages() { monthlyDownloads, } } + +/** + * Split the given term into array of suffixes. + * + * @example + * suffixes('12345', 2) + * // => ['12345', '2345, '345', '45'] + */ +function suffixes(term: string, minLength: number) { + if (term == null) + return + + const tokens = [] + + for (let i = 0; i <= term.length - minLength; i++) { + tokens.push(term.slice(i).toLowerCase()) + } + + return tokens +}