Unity.Aliases (Unity v0.6.0)

Copy Markdown View Source

Maps user-friendly unit abbreviations and common names to CLDR unit identifiers recognized by Localize.Unit.

The alias table is built at compile time from a hand-curated abbreviation map. Resolution tries the alias table first, then falls back to passing the name directly to Localize.Unit.new/1 to see if it is already a valid CLDR name.

Summary

Functions

Returns all known unit names (both aliases and CLDR base names).

Returns a list of all known alias names (the keys of the alias table).

Resolves a user-provided unit name to a CLDR unit identifier.

Finds the closest matching unit names for a given unknown name using Jaro distance for fuzzy matching.

Functions

all_known_names()

@spec all_known_names() :: [String.t()]

Returns all known unit names (both aliases and CLDR base names).

known_aliases()

@spec known_aliases() :: [String.t()]

Returns a list of all known alias names (the keys of the alias table).

resolve(name)

@spec resolve(String.t()) :: {:ok, String.t()} | {:error, :unknown_unit}

Resolves a user-provided unit name to a CLDR unit identifier.

Tries the alias table first, then checks if the name is already a valid CLDR unit name. Returns {:ok, cldr_name} or {:error, :unknown_unit}.

Arguments

  • name - a string unit name or abbreviation.

Returns

  • {:ok, cldr_name} if the name resolves to a known unit.

  • {:error, :unknown_unit} if the name cannot be resolved.

Examples

iex> Unity.Aliases.resolve("km")
{:ok, "kilometer"}

iex> Unity.Aliases.resolve("meter")
{:ok, "meter"}

iex> Unity.Aliases.resolve("frobnicator")
{:error, :unknown_unit}

suggest(name, options \\ [])

@spec suggest(
  String.t(),
  keyword()
) :: [{String.t(), float()}]

Finds the closest matching unit names for a given unknown name using Jaro distance for fuzzy matching.

Arguments

  • name - the unknown unit name to match against.

  • options - keyword list of options.

Options

  • :max_results - maximum number of suggestions to return. Defaults to 5.

  • :threshold - minimum Jaro distance to include. Defaults to 0.7.

Returns

A list of {cldr_name, distance} tuples, sorted by distance descending.