ex_uc v0.1.5-dev ExUc.Units

Units and conversion accessor

Allow efficient access to units and conversions defined in config.

Summary

Functions

Gets the conversion factor for the units

Gets the unit among its aliases that can be used as a key in conversions

Gets the kind of unit for ther given unit

Gets a map with every kind of unit defined in config

Functions

get_conversion(from_alias, to_alias)

Gets the conversion factor for the units

If can find inverse relation when the conversion is a factor.

Returns Atom.t, Integer.t, Float.t

Parameters

  • from: Atom representing the unit to convert from
  • to: Atom representing the unit to convert to

Examples

iex>ExUc.Units.get_conversion(:g, :mg)
{:ok, 1000}

iex>ExUc.Units.get_conversion(:g, :zz)
{:error, "undetermined conversion"}

# This relation has not been defined but
# the inverse is based on a factor, so is valid.
iex>ExUc.Units.get_conversion(:km, :m)
{:ok, 1.0e3}
get_key_alias(alias, kind)

Gets the unit among its aliases that can be used as a key in conversions

Parameters

  • alias: Atom with a unit aliases.
  • kind: Atom or String for the kind where the alias is.

Examples

iex>ExUc.Units.get_key_alias(:meter, "length")
:m

iex>ExUc.Units.get_key_alias(:pounds, :mass)
:lb

iex>ExUc.Units.get_key_alias(:r4R3, :mass)
nil
get_kind(unit)

Gets the kind of unit for ther given unit.

Parameters

  • unit: Atom representing the unit to find the kind.

Examples

iex>ExUc.Units.get_kind(:kg)
"mass"

iex>ExUc.Units.get_kind(:meter)
"length"
map()

Gets a map with every kind of unit defined in config.

The result has a very traversable structure as:

%{
  kind_of_unit: [
    alias_0: :main,
    alias_N: :main,
  ],
  ...
}

Returns Map