ex_uc v1.0.1 ExUc.Units

Units and conversion accessor

Allow efficient access to units and conversions defined in config.

Summary

Functions

Gets all defined units as a Keyword

Gets all the conversions for the given kind

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 list of units as path for conversion

Gets a map with every kind of unit defined in config

Gets the defined precision for outputs

Functions

all()

Gets all defined units as a Keyword.

Units can be defined in config files or modules under Units namespace.

Returns Keyword/List

all_conversions(kind)

Gets all the conversions for the given kind.

Retuns Map

Parameters

  • kind: Atom for the kind of units.
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}

# This relation has not been defined either but
# there is a traversable path among units.
iex>ExUc.Units.get_conversion(:km, :ft)
{:ok, [:km, :m, :ft]}
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"
get_path_in(kind, from, to)

Gets a list of units as path for conversion.

Returns List of units.

Parameters

  • kind: Atom as the kind of unit.
  • from: Atom as initial unit.
  • to: Atom as target unit.

Examples

iex>ExUc.Units.get_path_in(:length, :km, :ft)
[:km, :m, :ft]

iex>ExUc.Units.get_path_in(:length, :km, :zzx)
false
init_graphs()
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

precision()

Gets the defined precision for outputs.

By default is 2 decimals.

Examples

iex>ExUc.Units.precision
2