ucum

UCUM, the Unified Code for Units of Measure, for Elixir: read, check, convert and name unit codes such as kg/m2, mm[Hg], 10*3/uL and {request}/s, as the standard defines them.

iex> Ucum.convert!(1500, "ms", "s")
1.5
iex> Ucum.conversion("Cel", "[degF]")
{:ok, {{9, 5}, {32, 1}}}
iex> Ucum.convert(7, "[pH]", "mol/l")
{:ok, 1.0e-7}
iex> Ucum.parse("sec")
{:error, {:unknown_atom, "sec"}}
iex> Ucum.name("km/h")
{:ok, "(kilometer) / (hour)"}
iex> Ucum.case_sensitive("MM[HG]")
{:ok, "mm[Hg]"}

Installation

def deps do
  [{:ucum, "~> 0.7"}]
end

On Hex, documented on HexDocs, with its source on GitHub. It needs Elixir 1.18 or later.

What it does

Every function answers {:ok, value} or {:error, reason} (spec/03); its ! twin answers the value or raises Ucum.Error. The library has no runtime dependency, starts no process and reads no file.

Conformance

The library is measured by the UCUM functional tests (spec/04), the UcumFunctionalTests.xml of Ucum-java. It passes 572 of 573 cases: every validation, conversion, multiplication and division case, and 8 of the 9 display-name cases. The ninth names the empty string, which the same file and UCUM's grammar call no code; spec/04 §3 argues it. mix conformance prints the count.

Specification

spec/ is the full specification: what the library reads, what it answers, how it fails, and how its conformance is measured.

Attribution

UCUM is maintained by the Regenstrief Institute, and its essence table is included here unmodified. This library is an independent implementation, not affiliated with or endorsed by Regenstrief.