You're seeing just the function parse_unit, go back to Cldr.Unit.Parser module for more information.


parse_unit(String.t()) ::
  {:ok, [Cldr.Unit.base_conversion()]}
  | {:ok, {[Cldr.Unit.base_conversion()], [Cldr.Unit.base_conversion()]}}
  | {:error, {module(), String.t()}}

Parses a unit name expressed as a string and returns the parsed name or an error.


  • unit_string is a unit name (such as "meter") as a String.t()


  • {:ok, normalized_unit} or

  • {:error, {exception, reason}}


A normalised unit is a 2-tuple with the first element a list of standard units that are before the first "per" in the unit name. The second element is a list of standard units after the first "per" (if any).

The structure of the standard unit is {standard_unit, conversion_to_base_unit}.

This function is not normally called by consumers of this library. It is called by Cldr.Unit.validate_unit/1 which is the main public API.


iex> Cldr.Unit.Parser.parse_unit "kilogram per light year"
       base_unit: [:kilogram],
       factor:, 144115188075855872),
       offset: 0
       base_unit: [:meter],
       factor: 9460730000000000,
       offset: 0