algebraicsystems/elm-unicode-normalize/1.1.0

Unicode.Normalize

Unicode normalization.

There are multiple ways to normalize a string, referred to as Normalization Forms. Which one you want depends on what you want to do with the string, but the most common is NFC, which preserves the exact visual appearance of the string. If you don't care about the details, just use normalizeNFC.

import Unicode.Normalize exposing (Form(..), normalize)


normalizeNFC "é"
--> "é"


normalizeNFC (String.fromList [ 'ᄉ', 'ᅥ', 'ᆼ' ])
--> "성"

The different ways to normalize a string are:

  • NFC: Canonical decomposition followed by canonical composition.
  • NFD: Canonical decomposition without recomposition.
  • NFKC: Compatibility decomposition followed by canonical composition.
  • NFKD: Compatibility decomposition without recomposition.

Canonical decomposition splits single characters into sequences of equivalent combining characters. Compatibility decomposition extends canonical decomposition by also replacing variants of the same logical character (such as replacing ¼ with 1/4).

Canonical composition undoes the process of canonical decomposition, combining sequences into single characters wherever possible.

For example, the character "ñ" can be represented as a single code point (U+00F1) or as two code points ("n" followed by a combining tilde, U+006E U+0303). The normalization forms handle this differently:

-- NFC composes into a single character
normalize NFC "ñ"  -- (n + combining tilde)
--> "ñ"            -- (single ñ)


-- NFD decomposes into base + combining character
normalize NFD "ñ"  -- (single ñ)
--> "ñ"            -- (n + combining tilde)

The compatibility forms also replace visual variants:

-- NFKC replaces fi ligature with "fi" and composes
normalize NFKC "fiñ"
--> "fiñ"


-- NFKD replaces fi ligature with "fi" and decomposes
normalize NFKD "fiñ"
--> "fiñ"

Normalization Forms

normalizeNFC : String -> String

Get the NFC normalization of a string.

normalizeNFD : String -> String

Get the NFD normalization of a string.

normalizeNFKC : String -> String

Get the NFKC normalization of a string.

normalizeNFKD : String -> String

Get the NFKD normalization of a string.

JavaScript-style API

This function mirrors the String.prototype.normalize function from JavaScript, which takes an argument specifying which normalization form to produce.

type Form
= NFC
| NFD
| NFKC
| NFKD

Choose a specific normalization form.

normalize : Form -> String -> String

Normalize a string to a particular Normalization Form.

-- Two equivalent representations of "é" become identical after NFC
normalize NFC "é" == normalize NFC "é"
--> True


-- ASCII strings are unaffected
normalize NFC "hello"
--> "hello"


-- Compatibility normalization replaces special forms
normalize NFKC "①②③"
--> "123"


normalize NFKC "ffi"
--> "ffi"