Canonicalises CLDR unit identifier ASTs and names.
Provides functions to sort unit components into CLDR canonical order
(as defined by the unitQuantity simple base unit ordering in the
supplemental units data), consolidate repeated terms, cross-cancel
matching terms between numerator and denominator, and format the
result as a canonical unit identifier string.
Summary
Functions
Returns the canonical name and normalised AST for a parsed unit.
Builds a canonical name and normalised AST from raw numerator and denominator component lists.
Functions
@spec canonicalize({:unit, keyword()} | {:mixed_unit, term()}) :: {String.t(), {:unit, keyword()} | {:mixed_unit, term()}}
Returns the canonical name and normalised AST for a parsed unit.
The AST is normalised by sorting numerator and denominator components into CLDR canonical order.
Arguments
parsedis a{:unit, keyword}AST as returned by the parser.
Returns
{canonical_name, normalised_ast}wherecanonical_nameis a string andnormalised_asthas canonically ordered components.
Builds a canonical name and normalised AST from raw numerator and denominator component lists.
Applies consolidation of duplicate terms, cross-cancellation of matching terms, and canonical sorting before formatting.
Arguments
numeratoris a list of single_unit or constant AST tuples.denominatoris a list of single_unit or constant AST tuples.
Returns
{canonical_name, normalised_ast}or{:dimensionless, nil}when all units cancel.