Search: Use case-insensitive substring matching in fuzzySearch fallback (#107661)

This commit is contained in:
Leon Sorokin
2025-07-08 14:14:00 -05:00
committed by GitHub
parent efe46c8aad
commit dd1fce5c8a
2 changed files with 13 additions and 1 deletions
@@ -48,6 +48,14 @@ describe('fuzzySearch', () => {
expect(result.map((idx) => haystack[idx])).toEqual(['A水']);
});
it('should do case-insensitive substring match when needle contains non-ascii characters', () => {
const haystack = ['Über'];
const needle = 'ü';
const result = fuzzySearch(haystack, needle);
expect(result.map((idx) => haystack[idx])).toEqual(['Über']);
});
it('should handle multiple non-latin characters', () => {
const haystack = ['台灣省', '台中市', '台北市', '台南市', '南投縣', '高雄市', '台中第一高級中學'];
const needle = '南';
@@ -1,5 +1,7 @@
import uFuzzy from '@leeoniya/ufuzzy';
import { escapeRegex } from '../text/string';
// https://catonmat.net/my-favorite-regex :)
const REGEXP_NON_ASCII = /[^ -~]/m;
// https://www.asciitable.com/
@@ -36,11 +38,13 @@ export function fuzzySearch(haystack: string[], needle: string): number[] {
needle.length > maxNeedleLength ||
uf.split(needle).length > maxFuzzyTerms
) {
const needleRegex = new RegExp(escapeRegex(needle), 'i');
const indices: number[] = [];
for (let i = 0; i < haystack.length; i++) {
let item = haystack[i];
if (item.includes(needle)) {
if (needleRegex.test(item)) {
indices.push(i);
}
}