fix: 调整筛选条目排序权重 #71
@ -26,25 +26,12 @@ export function fuzzyMatch<T extends object>(
|
|||||||
source: T[],
|
source: T[],
|
||||||
options: FuzzyFilterOptions<T>
|
options: FuzzyFilterOptions<T>
|
||||||
): T[] {
|
): T[] {
|
||||||
const {
|
const { keyword, keys, threshold = 0.35, minMatchCharLength = 1 } = options;
|
||||||
keyword,
|
|
||||||
keys,
|
|
||||||
threshold = 0.35,
|
|
||||||
minMatchCharLength = 1,
|
|
||||||
locale = 'auto',
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
// --- 多语言比较器 ---
|
|
||||||
const collator = new Intl.Collator(locale === 'auto' ? undefined : locale, {
|
|
||||||
sensitivity: 'base',
|
|
||||||
ignorePunctuation: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
// --- 文本标准化函数 ---
|
// --- 文本标准化函数 ---
|
||||||
const normalizeText = (text: string): string => {
|
const normalizeText = (text: string): string => {
|
||||||
const normalizedText = text.normalize('NFKC').toLowerCase().trim();
|
const normalizedText = text.normalize('NFKC').toLowerCase().trim();
|
||||||
const translit = transliterateText(normalizedText);
|
return normalizedText;
|
||||||
return `${normalizedText} ${translit}`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,29 +89,14 @@ export function fuzzyMatch<T extends object>(
|
|||||||
.replace(/\s+/g, '')
|
.replace(/\s+/g, '')
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.trim();
|
.trim();
|
||||||
const translit = transliterateText(normalized);
|
return normalized;
|
||||||
logger.debug(`${normalized} => ${translit}`);
|
|
||||||
// 拼接原文 + 转写,提升中外文混合匹配效果
|
|
||||||
return `${normalized} ${translit}`;
|
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
sortFn: (a, b) => {
|
|
||||||
const itemA = a.item as T;
|
|
||||||
const itemB = b.item as T;
|
|
||||||
const key = keys[0];
|
|
||||||
const aValue = itemA[key];
|
|
||||||
const bValue = itemB[key];
|
|
||||||
if (typeof aValue === 'string' && typeof bValue === 'string') {
|
|
||||||
return collator.compare(aValue, bValue);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.debug('Fuzzy search options:', options);
|
const result = fuse.search(k);
|
||||||
logger.debug('Fuzzy search keyword:', k);
|
return result.map((result) => result.item);
|
||||||
return fuse.search(k).map((result) => result.item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return source.filter((item) =>
|
return source.filter((item) =>
|
||||||
|
|||||||
@ -29,7 +29,6 @@
|
|||||||
"meilisearch": "^0.53.0",
|
"meilisearch": "^0.53.0",
|
||||||
"nuxt": "^4.0.3",
|
"nuxt": "^4.0.3",
|
||||||
"nuxt-directus": "5.7.0",
|
"nuxt-directus": "5.7.0",
|
||||||
"pinyin-pro": "^3.27.0",
|
|
||||||
"sass": "^1.90.0",
|
"sass": "^1.90.0",
|
||||||
"sharp": "^0.34.3",
|
"sharp": "^0.34.3",
|
||||||
"vue": "^3.5.18",
|
"vue": "^3.5.18",
|
||||||
|
|||||||
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
@ -62,9 +62,6 @@ importers:
|
|||||||
nuxt-directus:
|
nuxt-directus:
|
||||||
specifier: 5.7.0
|
specifier: 5.7.0
|
||||||
version: 5.7.0(magicast@0.3.5)
|
version: 5.7.0(magicast@0.3.5)
|
||||||
pinyin-pro:
|
|
||||||
specifier: ^3.27.0
|
|
||||||
version: 3.27.0
|
|
||||||
sass:
|
sass:
|
||||||
specifier: ^1.90.0
|
specifier: ^1.90.0
|
||||||
version: 1.92.1
|
version: 1.92.1
|
||||||
@ -4402,9 +4399,6 @@ packages:
|
|||||||
typescript:
|
typescript:
|
||||||
optional: true
|
optional: true
|
||||||
|
|
||||||
pinyin-pro@3.27.0:
|
|
||||||
resolution: {integrity: sha512-Osdgjwe7Rm17N2paDMM47yW+jUIUH3+0RGo8QP39ZTLpTaJVDK0T58hOLaMQJbcMmAebVuK2ePunTEVEx1clNQ==}
|
|
||||||
|
|
||||||
pkg-types@1.3.1:
|
pkg-types@1.3.1:
|
||||||
resolution: {integrity: sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ==}
|
resolution: {integrity: sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ==}
|
||||||
|
|
||||||
@ -10624,8 +10618,6 @@ snapshots:
|
|||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
typescript: 5.9.2
|
typescript: 5.9.2
|
||||||
|
|
||||||
pinyin-pro@3.27.0: {}
|
|
||||||
|
|
||||||
pkg-types@1.3.1:
|
pkg-types@1.3.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
confbox: 0.1.8
|
confbox: 0.1.8
|
||||||
|
|||||||
Reference in New Issue
Block a user