Cldr.Unit.Parser.parse_unit

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

Specs

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.

Arguments

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

Returns

  • {:ok, normalized_unit} or

  • {:error, {exception, reason}}

Notes

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.

Example

iex> Cldr.Unit.Parser.parse_unit "kilogram per light year"
{:ok,
 {[
    {:kilogram,
     %Cldr.Unit.Conversion{
       base_unit: [:kilogram],
       factor: Ratio.new(144115188075855875, 144115188075855872),
       offset: 0
     }}
  ],
  [
    {:light_year,
     %Cldr.Unit.Conversion{
       base_unit: [:meter],
       factor: 9460730000000000,
       offset: 0
     }}
  ]}}