Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677/** * Folding, for matching only. * * uFuzzy's allowances cover *some* punctuation between characters (up to three * non-word characters inside a term), but not the cases people actually type: * * "wont do" ✗ "won't do" — an intra-term apostrophe is a term break * "dont stop" ✗ "Don't Stop" — same * "bjork" ✗ "Björk" — no diacritic folding at all * "acdc" ✗ "AC/DC" — an intra-term slash ends the term * * So both the haystack and the query are folded through this function before * uFuzzy sees them, and matching becomes what it should be: the same letters * in the same order mean the same thing, whatever punctuation and accents the * server's metadata happened to carry. * * Two decisions worth recording, because both are load-bearing: * * - Punctuation and symbols are **deleted**, not replaced with a space. The * apostrophe is the whole point: "won't" must become "wont" so the needle * "wont" matches it as one term. Spacing it out would give "won t", which no * amount of intra-term allowance repairs once the apostrophe is gone — the * characters uFuzzy would need to skip over are no longer there. Deletion is * also why "AC/DC" answers to "acdc": the parts join, which is the reading a * person typing without the slash intends. * * - Letters are *folded*, never dropped. Deleting what we do not understand * would erase a CJK or Cyrillic library entirely — an empty haystack cannot * match anything. So the Latin letters with no canonical decomposition (ø, * æ, ß…) get an explicit table, and everything outside Latin is left alone. * * The fold is lossy by design — "Invaders" and "Invaders!" are the same * searchable string — which is why nothing displayed ever runs through it. */
/** Combining marks, which only exist as separate code points after NFD. */const MARKS = /\p{M}/gu;/** Everything that is decoration rather than content: , . ' " / & — and ★. */const DECORATION = /[\p{P}\p{S}]/gu;
/** * Latin letters with no canonical decomposition, so NFD + mark-stripping * cannot reach them. Keys are lowercase; a missing key means "leave it". */const TRANSLITERATE: Record<string, string> = { ø: "o", æ: "ae", œ: "oe", ß: "ss", đ: "d", ð: "d", ł: "l", ŧ: "t", ı: "i", ħ: "h", ŋ: "n", þ: "th", ĸ: "k", ŀ: "l",};
const TRANSLITERABLE = new RegExp(`[${Object.keys(TRANSLITERATE).join("")}]`, "g");const SPACES = /\s+/g;
/** Case, accents, punctuation and spacing collapsed to what matching needs. */export function fold(text: string): string { if (!text) return ""; return text .normalize("NFD") .replace(MARKS, "") .toLowerCase() .replace(TRANSLITERABLE, (ch) => TRANSLITERATE[ch] ?? ch) .replace(DECORATION, "") .replace(SPACES, " ") .trim();}