ex_uc v0.3.0 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 list of units as path for conversion
Gets a map with every kind of unit defined in config
Functions
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 but
# the inverse is based on a factor, so is valid.
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