const DIACRITIC_RE = /\p{Diacritic}/gu; /** * Latin letters that carry no combining mark: Unicode decomposition (NFD) leaves them whole, so stripping marks alone * would still tell "Łotwa" from "Lotwa" or "Weiß" from "Weiss". Mapped to what a keyboard without them types. */ const WHOLE_LETTER_REPLACEMENTS: Record = { ł: 'l', đ: 'd', ð: 'd', ø: 'o', ħ: 'h', ı: 'i', ß: 'ss', æ: 'ae', œ: 'oe', þ: 'th', }; const WHOLE_LETTER_RE = new RegExp(`[${Object.keys(WHOLE_LETTER_REPLACEMENTS).join('')}]`, 'g'); export default class SearchTextUtils { /** * The form two texts are compared in by a type-ahead search: lower case, accents and other diacritics removed, * surrounding whitespace trimmed. Fold the query AND the searched text with it, then `includes` / `===` them, so * "cesko" finds "Česko", "LODZ" finds "Łódź" and "francuzsko" finds "Francúzsko". */ static fold(text: string | null | undefined): string { if (text == null) { return ''; } return text .toString() .toLowerCase() .normalize('NFD') .replace(DIACRITIC_RE, '') .replace(WHOLE_LETTER_RE, letter => WHOLE_LETTER_REPLACEMENTS[letter]) .trim(); } }