Reference · Strings

normalize

signature
normalize(str, str | null=) -> str

What 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")))
output
false true5 4 5

Edge cases

print(normalize("file", "nfkc"), normalize("file"), normalize("Ⅻ", "NFKD"), normalize(""))
output
file file XII 

Errors

A form that is none of the four is a runtime error.

this one fails
normalize("x", "NFX")
output
line 1: `normalize`: the form is "NFC", "NFD", "NFKC" or "NFKD", got "NFX"

Chaining

print("  Cafe\u{301}  " | trim() | normalize() | lower())
output
café

See also

lower, trim, len

Thread
4Useful? Like it, or add to its thread below.

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