ChemPal Documentation - v1.7.0
    Preparing search index...

    Module helpers/quantity

    Functions - Helpers

    toMetricQuantity

    Converts a quantity to its "standard" (metric) form, leaving metric and countable units untouched. Imperial mass (lb, oz) becomes grams (then normalized up to kg via normalizeQuantity); imperial volume (gal, qt, fl oz) becomes millilitres (then normalized up to l). Built on toBaseQuantity for the conversion factors so suppliers can present a single, comparable unit system regardless of how the source lists it.

    Modules

    <internal>

    Quantity - Helpers

    normalizeQuantity

    Normalizes a quantity object to its base unit of measure.

    parseQuantity

    Parses a quantity string into a structured object containing the numeric value and unit of measure. Handles various formats including foreign number formats (e.g., 1.234,56). Uses regex pattern matching to extract quantity and unit information.

    stripQuantityFromString

    Strips the quantity from a string. This is useful for when some suppliers don't have products listed as variants, but instead have multiple products with a quantity in the name. Using this function, we can get the name of the product without the quantity, which may be identical to the other variations. Making it easy to group the products into a single listing with multiple variants.

    standardizeUom

    Standardizes a unit of measure (UOM) to its canonical form. Uses the uomAliases mapping to convert various representations to standard forms.

    toBaseQuantity

    Converts a quantity from its current unit to a common unit of mass or volume. This is to make it easier to compare quantities of different units.

    getUomKind

    Classifies a unit of measure as mass, volume, or countable. Standardizes the input first (via standardizeUom), so aliases like "grams" or "lbs" are accepted. Returns undefined for unrecognized units.

    toCostBaseQuantity

    Normalizes a quantity to the base unit used for per-unit cost comparison: grams for mass, millilitres for volume, and the unchanged countable unit for pieces/each. Unlike toBaseQuantity (which returns milligrams for mass), this steps mass up to grams so a cost-per-gram reads naturally. Returns undefined for an unrecognized unit or a non-positive quantity, so callers can safely divide by the result.

    formatUomForDisplay

    Formats a unit of measure (UOM) for display.