LedgerUI.Components.PhoneInput.Format (LedgerUI v0.2.0)

Copy Markdown View Source

Country data and number formatting for LedgerUI.Components.PhoneInput.

e164/2 reads a submitted number the way the field does and normalizes it to E.164. It rejects what cannot be a number but does not know which numbers a carrier issues; for that, check the E.164 string with a library that has number metadata.

iex> LedgerUI.Components.PhoneInput.Format.e164("(11) 91234-5678", "BR")
{:ok, "+5511912345678"}
iex> LedgerUI.Components.PhoneInput.Format.e164("+351 912 345 678", "BR")
{:ok, "+351912345678"}
iex> LedgerUI.Components.PhoneInput.Format.e164("12", "BR")
{:error, :too_short}

Summary

Functions

All the countries known to the field.

Reads a submitted number and normalizes it to E.164.

Formats national digits in the pattern of country (a map from all/0 or an ISO code). A separator only appears when a digit follows it, and extra digits are kept.

A country by ISO 3166-1 alpha-2 code, or nil.

Functions

all()

All the countries known to the field.

e164(input, default_country \\ "BR")

@spec e164(String.t() | nil, String.t()) :: {:ok, String.t()} | {:error, atom()}

Reads a submitted number and normalizes it to E.164.

A number starting with + (or 00) carries its own country; any other is read as a national number of default_country (its trunk prefix is dropped). Returns {:ok, "+5511912345678"} or {:error, reason} with :empty, :unknown_country, :too_short or :too_long.

format_national(digits, iso)

Formats national digits in the pattern of country (a map from all/0 or an ISO code). A separator only appears when a digit follows it, and extra digits are kept.

iex> LedgerUI.Components.PhoneInput.Format.format_national("11912345678", "BR")
"(11) 91234-5678"
iex> LedgerUI.Components.PhoneInput.Format.format_national("119", "BR")
"(11) 9"

get(iso)

A country by ISO 3166-1 alpha-2 code, or nil.