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
Gets all defined units as a Keyword.
Units can be defined in config files or modules under Units namespace.
Returns Keyword/List
Gets all the conversions for the given kind.
Retuns Map
Parameters
- kind: Atom for the kind of units.
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]}
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
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"
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
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