Reference · Strings
normalize
normalize(str, str | null=) -> strWhat it does
The text in one Unicode form, so that text which looks the same is the same. é can be
written as one character or as e followed by a combining accent; a keyboard on one
system and a paste from another can give either, and ==, len and a database's unique
index tell them apart. normalize(s) writes the composed form, NFC; the second argument
picks another: "NFD" (decomposed), "NFKC" or "NFKD", which also fold look-alikes
such as fi into fi. It runs the same on the server and in the browser.
Examples
let typed = "café"let pasted = "cafe\u{301}"print(typed == pasted, normalize(typed) == normalize(pasted))print(len(pasted), len(normalize(pasted)), len(normalize(typed, "NFD")))false true5 4 5Edge cases
- Compare user names and emails after
normalize(lower(trim(s)), "NFKC"), and keep them so in the database, so two accounts cannot share one visible name. - The form's name may be in either case. Text already in the form comes back the same, and an empty text is empty.
print(normalize("file", "nfkc"), normalize("file"), normalize("Ⅻ", "NFKD"), normalize(""))file file XII Errors
A form that is none of the four is a runtime error.
normalize("x", "NFX")line 1: `normalize`: the form is "NFC", "NFD", "NFKC" or "NFKD", got "NFX"Chaining
print(" Cafe\u{301} " | trim() | normalize() | lower())caféSee also
Thread 0 posts
On normalize(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo