// Writes crates/ui/src/fuzzy/accents.rs: what fuzzysort's `remove_accents` makes of each character it changes, from // this runtime's Unicode data. It decomposes Latin letters (NFD) and drops the marks U+0300 to U+036F. Marks are not // Latin, so a run of Latin letters decomposes letter by letter. Sorted by character for a binary search. const removeAccents = (text: string) => text.replace(/\p{Script=Latin}+/gu, (match) => match.normalize("NFD")).replace(/[\u0300-\u036f]/g, "") const entries = Array.from({ length: 0x110000 }, (_, code) => code) .filter((code) => code < 0xd800 || code > 0xdfff) .map((code) => [String.fromCodePoint(code), removeAccents(String.fromCodePoint(code))] as const) .filter(([char, folded]) => char !== folded) const escape = (text: string) => Array.from(text, (char) => /[ -~]/.test(char) && !`'"\\`.includes(char) ? char : `\\u{${char.codePointAt(0)!.toString(16)}}`, ).join("") const lines = [ "// Generated by script/gen/accents.ts from fuzzysort's `remove_accents`. Do not edit.", "", "/// `(character, folded)`, sorted by character.", "pub const FOLD: &[(char, &str)] = &[", ...entries.map(([char, folded]) => ` ('${escape(char)}', "${escape(folded)}"),`), "];", "", ] const file = new URL("../../crates/ui/src/fuzzy/accents.rs", import.meta.url).pathname await Bun.write(file, lines.join("\n")) console.log(`accents: ${entries.length} characters`)