forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

search-text.ts83 lines, 3.3 KiBHistoryRawDownload
1// Turkish search folding. Every list filter in the app used
2// `toLocaleLowerCase().includes(...)`, which made a search box feel broken the
3// moment the typed text skipped a diacritic: "ogretmen" missed "Öğretmen",
4// "sinav" missed "sınav", and an "İ" pasted from Word matched nothing.
5const FOLD: Record<string, string> = {
6 ı: "i", İ: "i", ş: "s", Ş: "s", ğ: "g", Ğ: "g",
7 ü: "u", Ü: "u", ö: "o", Ö: "o", ç: "c", Ç: "c",
8 â: "a", Â: "a", î: "i", Î: "i", û: "u", Û: "u",
9};
10
11/**
12 * Case-, diacritic- and punctuation-insensitive form of a string, for
13 * comparison only. Punctuation goes because a class reads "9-A" on screen and
14 * nobody types the dash: folding both sides to "9a" is what makes the box feel
15 * like it works.
16 */
17export function foldSearchText(value: string | null | undefined): string {
18 if (!value) return "";
19 let folded = "";
20 for (const char of value) folded += FOLD[char] ?? char;
21 // Anything the table above misses (accented Latin pasted from elsewhere)
22 // loses its combining marks here.
23 return folded
24 .toLowerCase()
25 .normalize("NFD")
26 .replace(/\p{Diacritic}/gu, "")
27 .replace(/[^\p{L}\p{N}\s]+/gu, "");
28}
29
30/**
31 * Whether every term in `query` appears in one of `fields`, in any order — so
32 * "ali 9a" finds Ali in class 9-A, and a stray double space does not empty the
33 * list. An empty query matches everything, which is what a filter should do.
34 */
35export function matchesSearch(query: string, ...fields: (string | null | undefined)[]): boolean {
36 const terms = foldSearchText(query).split(/\s+/).filter(Boolean);
37 if (terms.length === 0) return true;
38 const haystack = fields.map(foldSearchText).join(" ");
39 return terms.every((term) => haystack.includes(term));
40}
41
42export type SearchMatchRange = { start: number; end: number };
43
44/**
45 * Finds the visible ranges for a query while keeping the original string's
46 * offsets. Search stays Turkish-aware, but the caller can still render the
47 * matching characters in their original spelling.
48 */
49export function searchMatchRanges(value: string, query: string): SearchMatchRange[] {
50 const terms = foldSearchText(query).split(/\s+/).filter(Boolean);
51 if (terms.length === 0) return [];
52
53 const foldedChars: string[] = [];
54 const sourceRanges: Array<{ start: number; end: number }> = [];
55 let sourceOffset = 0;
56 for (const char of value) {
57 const folded = foldSearchText(char);
58 for (const foldedChar of folded) {
59 foldedChars.push(foldedChar);
60 sourceRanges.push({ start: sourceOffset, end: sourceOffset + char.length });
61 }
62 sourceOffset += char.length;
63 }
64
65 const foldedValue = foldedChars.join("");
66 const ranges: SearchMatchRange[] = [];
67 for (const term of terms) {
68 let from = 0;
69 while (from < foldedValue.length) {
70 const index = foldedValue.indexOf(term, from);
71 if (index < 0) break;
72 const endIndex = index + term.length - 1;
73 const start = sourceRanges[index]?.start;
74 const end = sourceRanges[endIndex]?.end;
75 if (start !== undefined && end !== undefined) ranges.push({ start, end });
76 from = index + Math.max(term.length, 1);
77 }
78 }
79
80 return ranges
81 .filter((range, index, all) => all.findIndex((other) => other.start === range.start && other.end === range.end) === index)
82 .sort((a, b) => a.start - b.start || a.end - b.end);
83}