### Install UOM and UOM-Units Source: https://context7.com/dividab/uom/llms.txt Install the core UOM library and the companion package for additional units using npm. ```bash npm install --save uom npm install --save uom-units # For additional units ``` -------------------------------- ### Retrieve and use unit formatting Source: https://context7.com/dividab/uom/llms.txt Get display labels and decimal precision for units, or create custom formatting configurations. ```typescript import { UnitFormat } from "uom"; import { Units, UnitsFormat } from "uom-units"; // Get format for a unit const meterFormat = UnitFormat.getUnitFormat(Units.Meter, UnitsFormat); // Result: { label: "m", decimalCount: 2 } // Create custom format const customFormat = UnitFormat.createUnitFormat("meters", 3); // Result: { label: "meters", decimalCount: 3 } // Usage in formatting output const amount = Amount.create(123.456789, Units.Meter); const format = UnitFormat.getUnitFormat(Units.Meter, UnitsFormat); const formatted = `${amount.value.toFixed(format.decimalCount)} ${format.label}`; // Result: "123.46 m" ``` -------------------------------- ### Create Custom Units Source: https://github.com/dividab/uom/blob/master/README.md Define your own units by dividing or multiplying existing units. This example creates a custom inch unit based on feet. ```javascript import { Amount, Unit } from "uom"; import { Units } from "uom-units"; const myInchUnit = Unit.divideNumber(12.0, Units.Foot); const amount = Amount.create(10, myInchUnit); const meter = Amount.valueAs(Units.Meter, amount); ``` -------------------------------- ### Convert Amounts Between Units Source: https://github.com/dividab/uom/blob/master/README.md Use this to convert a given amount to a different unit. Ensure 'uom-units' is installed for access to predefined units. ```javascript import { Amount } from "uom"; import { Units } from "uom-units"; const amount = Amount.create(10, Units.Meter); const inch = Amount.valueAs(Units.Inch, amount); ``` -------------------------------- ### Get Absolute Value and Negate Amount Source: https://context7.com/dividab/uom/llms.txt Returns the absolute value or negation of an amount. This is useful for ensuring positive values or reversing the sign of an amount. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const negative = Amount.create(-50, Units.Meter); const positive = Amount.create(50, Units.Meter); // Absolute value Amount.abs(negative); // { value: 50, unit: Units.Meter, decimalCount: 0 } Amount.abs(positive); // { value: 50, unit: Units.Meter, decimalCount: 0 } // Negation Amount.neg(positive); // { value: -50, unit: Units.Meter, decimalCount: 0 } Amount.neg(negative); // { value: 50, unit: Units.Meter, decimalCount: 0 } ``` -------------------------------- ### Publishing Minor Version Source: https://github.com/dividab/uom/blob/master/README.md Use this command to publish a minor version of the library. Ensure the changelog is updated before running. ```shell yarn version --minor ``` -------------------------------- ### Publishing Major Version Source: https://github.com/dividab/uom/blob/master/README.md Use this command to publish a major version of the library. Ensure the changelog is updated before running. ```shell yarn version --major ``` -------------------------------- ### Publishing Patch Version Source: https://github.com/dividab/uom/blob/master/README.md Use this command to publish a patch version of the library. Ensure the changelog is updated before running. ```shell yarn version --patch ``` -------------------------------- ### Format Unit Display with Labels and Decimals Source: https://github.com/dividab/uom/blob/master/README.md Retrieve formatting information (label, decimal count) for a unit to customize how amounts are displayed. Requires an external unit package like 'uom-units'. ```typescript import { Amount, Format } from "uom"; import { Units, UnitsFormat } from "uom-units"; const format = UnitFormat.getUnitFormat(Units.Meter, UnitsFormat); console.log( "The amount is " + Math.round(Amount.valueAs(Units.Meter, amount), format.decimalCount) + // Assuming 'amount' is defined elsewhere " " + format.label ); ``` -------------------------------- ### Compare Amounts for Equality, Less Than, and Greater Than Source: https://context7.com/dividab/uom/llms.txt Utilize Amount comparison functions like equals, lessThan, and greaterThan to compare two amounts of the same quantity. These functions handle unit conversions automatically. Additional functions include lessOrEqualTo, greaterOrEqualTo, min, max, and clamp. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const a = Amount.create(1, Units.Meter); const b = Amount.create(100, Units.Centimeter); const c = Amount.create(2, Units.Meter); // Equality check (handles unit conversion) Amount.equals(a, b); // true (1m == 100cm) Amount.equals(a, c); // false // Less than Amount.lessThan(a, c); // true (1m < 2m) Amount.lessThan(c, a); // false // Greater than Amount.greaterThan(c, a); // true (2m > 1m) Amount.greaterThan(a, c); // false // Additional comparison functions Amount.lessOrEqualTo(a, b); // true Amount.greaterOrEqualTo(a, b); // true // Min, max, and clamp Amount.min(a, c); // Returns a (1m) Amount.max(a, c); // Returns c (2m) Amount.clamp(Amount.create(0, Units.Meter), c, Amount.create(3, Units.Meter)); // Returns 2m ``` -------------------------------- ### Amount Operations Source: https://context7.com/dividab/uom/llms.txt Utilities for rounding, absolute values, negation, and string representation of Amount objects. ```APIDOC ## Amount.roundUp and Amount.roundDown ### Description Rounds an amount to the nearest step value, either up or down. ## Amount.abs and Amount.neg ### Description Returns the absolute value or negation of an amount. ## Amount.toString ### Description Returns a string representation of an amount with its derived unit symbol. ``` -------------------------------- ### Create SI-prefixed units Source: https://context7.com/dividab/uom/llms.txt Use UnitPrefix helper functions to define new units based on existing base units. ```typescript import { Unit, UnitPrefix, BaseUnits } from "uom"; // Create prefixed units const Kilometer = UnitPrefix.Kilo("Kilometer", BaseUnits.Meter); const Millimeter = UnitPrefix.Milli("Millimeter", BaseUnits.Meter); const Centimeter = UnitPrefix.Centi("Centimeter", BaseUnits.Meter); const Micrometer = UnitPrefix.Micro("Micrometer", BaseUnits.Meter); const Milligram = UnitPrefix.Milli("Milligram", BaseUnits.Kilogram); const Megawatt = UnitPrefix.Mega("Megawatt", Watt); const Gigabyte = UnitPrefix.Giga("Gigabyte", Byte); ``` -------------------------------- ### Amount Comparison Functions Source: https://context7.com/dividab/uom/llms.txt Compares two amounts of the same quantity for equality, less than, or greater than relationships. ```APIDOC ## Amount Comparison Functions ### Description Compares two amounts of the same quantity for equality, less than, or greater than relationships. ### Methods - `Amount.equals(amount1: Amount, amount2: Amount): boolean` - `Amount.lessThan(amount1: Amount, amount2: Amount): boolean` - `Amount.greaterThan(amount1: Amount, amount2: Amount): boolean` - `Amount.lessOrEqualTo(amount1: Amount, amount2: Amount): boolean` - `Amount.greaterOrEqualTo(amount1: Amount, amount2: Amount): boolean` - `Amount.min(amount1: Amount, amount2: Amount): Amount` - `Amount.max(amount1: Amount, amount2: Amount): Amount` - `Amount.clamp(min: Amount, value: Amount, max: Amount): Amount` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const a = Amount.create(1, Units.Meter); const b = Amount.create(100, Units.Centimeter); const c = Amount.create(2, Units.Meter); // Equality check (handles unit conversion) Amount.equals(a, b); // true (1m == 100cm) Amount.equals(a, c); // false // Less than Amount.lessThan(a, c); // true (1m < 2m) Amount.lessThan(c, a); // false // Greater than Amount.greaterThan(c, a); // true (2m > 1m) Amount.greaterThan(a, c); // false // Additional comparison functions Amount.lessOrEqualTo(a, b); // true Amount.greaterOrEqualTo(a, b); // true // Min, max, and clamp Amount.min(a, c); // Returns a (1m) Amount.max(a, c); // Returns c (2m) Amount.clamp(Amount.create(0, Units.Meter), c, Amount.create(3, Units.Meter)); // Returns 2m ``` ### Response #### Success Response (200) - **boolean** - Result of the comparison (for equals, lessThan, greaterThan, etc.). - **Amount** - The resulting amount (for min, max, clamp). #### Response Example ```json true ``` ```json { "value": 1, "unit": { "name": "Meter", "symbol": "m", "quantity": "Length" }, "decimalCount": 0 } ``` ``` -------------------------------- ### Create Amounts with Automatic Decimal Detection Source: https://context7.com/dividab/uom/llms.txt Use Amount.create to create amounts with automatic decimal precision detection from the input value. Explicit decimal counts can also be provided. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; // Create amounts with automatic decimal detection const length = Amount.create(10.5, Units.Meter); // Result: { value: 10.5, unit: Units.Meter, decimalCount: 1 } const precise = Amount.create(3.14159, Units.Meter); // Result: { value: 3.14159, unit: Units.Meter, decimalCount: 5 } // Create with explicit decimal count const rounded = Amount.create(100, Units.Kilogram, 2); // Result: { value: 100, unit: Units.Kilogram, decimalCount: 2 } ``` -------------------------------- ### Amount.create Source: https://context7.com/dividab/uom/llms.txt Creates an amount representing an exact value in the specified unit. Automatically detects decimal precision from the input value. ```APIDOC ## Amount.create ### Description Creates an amount representing an exact value in the specified unit. Automatically detects decimal precision from the input value. ### Method `Amount.create(value: number, unit: Unit, decimalCount?: number): Amount` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; // Create amounts with automatic decimal detection const length = Amount.create(10.5, Units.Meter); // Result: { value: 10.5, unit: Units.Meter, decimalCount: 1 } const precise = Amount.create(3.14159, Units.Meter); // Result: { value: 3.14159, unit: Units.Meter, decimalCount: 5 } // Create with explicit decimal count const rounded = Amount.create(100, Units.Kilogram, 2); // Result: { value: 100, unit: Units.Kilogram, decimalCount: 2 } ``` ### Response #### Success Response (200) - **value** (number) - The numeric value of the amount. - **unit** (Unit) - The unit of measurement. - **decimalCount** (number) - The number of decimal places. #### Response Example ```json { "value": 10.5, "unit": { "name": "Meter", "symbol": "m", "quantity": "Length" }, "decimalCount": 1 } ``` ``` -------------------------------- ### Amount API Source: https://github.com/dividab/uom/blob/master/docs/api.md Handles operations and manipulations for amount objects, including creation, comparison, arithmetic, and conversions. ```APIDOC ## Amount API ### Description Handles operations and manipulations for amount objects, including creation, comparison, arithmetic, and conversions. ### Methods #### Amount.defaultComparer(left, right) Default comparer for amounts. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. ##### Returns - (number) - Comparer value. #### Amount.create(value, unit, decimalCount) Creates an amount that represents an exact/absolute value in the specified unit. ##### Parameters - **value** (number) - The numeric value of the amount. - **unit** (Unit) - The unit of the amount. - **decimalCount** (number | undefined) - The decimal count of the amount. ##### Returns - (Amount) - The created amount. #### Amount.toString(amount) Returns a string representation of an Amount. ##### Parameters - **amount** (Amount) - The amount. ##### Returns - (string) - String representation of the Amount. #### Amount.neg(amount) Negation unary operator. ##### Parameters - **amount** (Amount) - The amount. #### Amount.isQuantity(quantity, amount) Determines if an Amount is of a quantity. ##### Parameters - **quantity** (Quantity) - Quantity to check for. - **amount** (Amount) - The amount to check. #### Amount.plus(left, right) Adds two amounts together. The two amounts must have the same quantity. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. ##### Returns - (Amount) - The sum of the two amounts. #### Amount.minus(left, right) Subtracts the right amount from the left amount. The two amounts must have the same quantity. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. ##### Returns - (Amount) - The result of the subtraction. #### Amount.times(left, right) Multiplies two amounts together. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. #### Amount.divide(left, right) Divides the left amount by the right amount. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. #### Amount.equals(left, right) Checks if two amounts are equal. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. ##### Returns - (boolean) - True if the amounts are equal, false otherwise. #### Amount.lessThan(left, right) Checks if the left amount is less than the right amount. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. ##### Returns - (boolean) - True if the left amount is less than the right amount, false otherwise. #### Amount.greaterThan(left, right) Checks if the left amount is greater than the right amount. ##### Parameters - **left** (Amount) - The left-hand amount. - **right** (Amount) - The right-hand amount. ##### Returns - (boolean) - True if the left amount is greater than the right amount, false otherwise. #### Amount.roundDown(step, amount) Rounds an amount down to the nearest step. ##### Parameters - **step** (Amount) - The step to round down to. - **amount** (Amount) - The amount to round down. #### Amount.roundUp(step, amount) Rounds an amount up to the nearest step. ##### Parameters - **step** (Amount) - The step to round up to. - **amount** (Amount) - The amount to round up. #### Amount.abs(amount) Returns the absolute value of an amount. ##### Parameters - **amount** (Amount) - The amount. #### Amount.valueAs(toUnit, amount) Converts an amount to a different unit. ##### Parameters - **toUnit** (Unit) - The unit to convert to. - **amount** (Amount) - The amount to convert. ``` -------------------------------- ### Amount Operations Source: https://github.com/dividab/uom/blob/master/docs/api.md Provides methods for arithmetic operations, comparisons, and transformations on amounts. ```APIDOC ## Amount.minus(left, right) ### Description Subtracts two amounts from each other. The two amounts must have the same quantity. The resulting amount will be of the same quantity as the two amounts. The resulting amount will have its decimal count set from the most granular amount. ### Method Amount.minus ### Parameters #### Path Parameters - **left** (Amount) - Description: The left-hand amount. - **right** (Amount) - Description: The right-hand amount. ### Response #### Success Response (200) - **Amount** (Amount) - The result of the subtraction. ## Amount.times(left, right) ### Description Multiplies an amount with a number. The resulting amount has the same unit and decimal count as the original amount. ### Method Amount.times ### Parameters #### Path Parameters - **left** (Amount) - Description: The amount to multiply. - **right** (Number) - Description: The number to multiply with. ### Response #### Success Response (200) - **Amount** (Amount) - The result of the multiplication. ## Amount.divide(left, right) ### Description Divides an amount with a number. The resulting amount has the same unit and decimal count as the original amount. ### Method Amount.divide ### Parameters #### Path Parameters - **left** (Amount) - Description: The amount to divide. - **right** (Number) - Description: The number to divide by. ### Response #### Success Response (200) - **Amount** (Amount) - The result of the division. ## Amount.equals(left, right) ⇒ boolean ### Description Compares two amounts for equality. ### Method Amount.equals ### Parameters #### Path Parameters - **left** (Amount) - Description: The left-hand Amount. - **right** (Amount) - Description: The right-hand Amount. ### Response #### Success Response (200) - **boolean** - True if the amounts are equal, false otherwise. ## Amount.lessThan(left, right) ⇒ boolean ### Description Checks if one Amount is less than another. ### Method Amount.lessThan ### Parameters #### Path Parameters - **left** (Amount) - Description: The left-hand Amount. - **right** (Amount) - Description: The right-hand Amount. ### Response #### Success Response (200) - **boolean** - True if the left-hand is less than the right-hand, false otherwise. ## Amount.greaterThan(left, right) ⇒ boolean ### Description Checks if one Amount is greater than another. ### Method Amount.greaterThan ### Parameters #### Path Parameters - **left** (Amount) - Description: The left-hand Amount. - **right** (Amount) - Description: The right-hand Amount. ### Response #### Success Response (200) - **boolean** - True if the left-hand is less than the right-hand, false otherwise. ## Amount.roundDown(step, amount) ### Description Rounds an amount down to the nearest step. ### Method Amount.roundDown ### Parameters #### Path Parameters - **step** (Number) - Description: Rounding step, for example 5.0 Celsius will round 23 to 20. - **amount** (Amount) - Description: Amount to round. ### Response #### Success Response (200) - **Amount** (Amount) - The rounded amount. ## Amount.roundUp(step, amount) ### Description Rounds an amount up to the nearest step. ### Method Amount.roundUp ### Parameters #### Path Parameters - **step** (Number) - Description: Rounding step, for example 5.0 Celsius will round 23 to 25. - **amount** (Amount) - Description: Amount to round. ### Response #### Success Response (200) - **Amount** (Amount) - The rounded amount. ## Amount.abs(amount) ### Description Gets the absolute amount (equivalent of Math.Abs()). ### Method Amount.abs ### Parameters #### Path Parameters - **amount** (Amount) - Description: The amount to get the absolute amount from. ### Response #### Success Response (200) - **Amount** (Amount) - The absolute amount. ## Amount.valueAs(toUnit, amount) ### Description Gets the value of the amount as a number in the specified unit. ### Method Amount.valueAs ### Parameters #### Path Parameters - **toUnit** (Unit) - Description: The unit to get the amount in. - **amount** (Amount) - Description: The amount to get the value from. ### Response #### Success Response (200) - **Number** - The value of the amount in the specified unit. ``` -------------------------------- ### Serialize API Source: https://github.com/dividab/uom/blob/master/docs/api.md Provides methods for converting between string representations and deserialized objects for units and amounts. ```APIDOC ## Serialize API ### Description Provides methods for converting between string representations and deserialized objects for units and amounts. ### Methods #### Serialize.stringToUnit(unitString) Converts a units serialized representation to its deserialized representation. ##### Parameters - **unitString** (string) - The serialized unit string. #### Serialize.unitToString(unit) Converts a unit to its serialized representation. ##### Parameters - **unit** (object) - The unit object to serialize. #### Serialize.amountToString(amount) Converts an amount to its serialized representation. ##### Parameters - **amount** (object) - The amount object to serialize. #### Serialize.stringToAmount(amountString) Converts a serialized amount to its deserialized representation. ##### Parameters - **amountString** (string) - The serialized amount string. ``` -------------------------------- ### Unit Definition and Conversion Source: https://context7.com/dividab/uom/llms.txt Utilities for creating base units, derived units, transformed units, and performing unit conversions. ```APIDOC ## Unit.createBase ### Description Creates a base unit for a specific quantity. Base units are the foundation for all derived units. ## Unit.times and Unit.divide ### Description Creates derived units by multiplying or dividing existing units. ## Unit.timesNumber and Unit.divideNumber ### Description Creates transformed units by applying a numeric factor to an existing unit. ## Unit.plus and Unit.minus ### Description Creates transformed units with an offset (useful for temperature scales). ## Unit.convert ### Description Converts a numeric value directly from one unit to another without creating Amount objects. ``` -------------------------------- ### Serialize and Deserialize Amounts Source: https://github.com/dividab/uom/blob/master/README.md Convert amounts to a string representation for storage or transmission, and then reconstruct them back into amount objects. ```typescript import { Amount, Serialize } from "uom"; import { Units } from "uom-units"; const length = Amount.create(10, Units.Meter); const serialized = Serialize.amountToString(length); const deserialized = Serialize.stringToAmount(serialized); ``` -------------------------------- ### Create Transformed Units with Offsets Source: https://context7.com/dividab/uom/llms.txt Creates transformed units with an offset, which is particularly useful for temperature scales like Celsius and Fahrenheit. ```typescript import { Unit, BaseUnits } from "uom"; // Create Celsius from Kelvin (offset by -273.15) const Celsius = Unit.minus("Celsius", 273.15, BaseUnits.Kelvin); // Create Fahrenheit from Rankine const Rankine = Unit.timesNumber("Rankine", 5/9, BaseUnits.Kelvin); const Fahrenheit = Unit.minus("Fahrenheit", 459.67, Rankine); ``` -------------------------------- ### Unit Operations Source: https://github.com/dividab/uom/blob/master/docs/api.md Provides methods for creating and manipulating units, including conversions. ```APIDOC ## Unit.One ### Description Holds the dimensionless unit ONE. ### Method Unit.One ### Response #### Success Response (200) - **Unit** (Unit) - The dimensionless unit ONE. ## Unit.createBase(quantity, symbol) ### Description Creates a base unit having the specified symbol. ### Method Unit.createBase ### Parameters #### Path Parameters - **quantity** (Number) - Description: The quantity of the resulting unit. - **symbol** (String) - Description: The symbol of this base unit. ### Response #### Success Response (200) - **Unit** (Unit) - The created base unit. ## Unit.createAlternate(symbol, parent) ### Description Creates an alternate unit for the specified unit identified by the specified symbol. ### Method Unit.createAlternate ### Parameters #### Path Parameters - **symbol** (String) - Description: The symbol for this alternate unit. - **parent** (Unit) - Description: Parent the system unit from which this alternate unit is derived. ### Response #### Success Response (200) - **Unit** (Unit) - The created alternate unit. ## Unit.times(quantity, left, right) ⇒ ### Description Returns the product of the specified units. ### Method Unit.times ### Parameters #### Path Parameters - **quantity** (Number) - Description: The quantity of the resulting unit. - **left** (Unit) - Description: The left unit operand. - **right** (Unit) - Description: The right unit operand. ### Response #### Success Response (200) - **Unit** (Unit) - The resulting unit from multiplication. ## Unit.divide(quantity, left, right) ⇒ ### Description Returns the quotient of the specified units. ### Method Unit.divide ### Parameters #### Path Parameters - **quantity** (Number) - Description: The quantity of the resulting unit. - **left** (Unit) - Description: The dividend unit operand. - **right** (Unit) - Description: The divisor unit operand. ### Response #### Success Response (200) - **Unit** (Unit) - The resulting unit from division. ## Unit.convert(value, fromUnit, toUnit) ### Description Converts a value from one unit to another. ### Method Unit.convert ### Parameters #### Path Parameters - **value** (Number) - Description: The value to convert. - **fromUnit** (Unit) - Description: The unit to convert from. - **toUnit** (Unit) - Description: The unit to convert to. ### Response #### Success Response (200) - **Number** - The converted value. ``` -------------------------------- ### Amount.plus and Amount.minus Source: https://context7.com/dividab/uom/llms.txt Adds or subtracts two amounts of the same quantity. The result uses the most granular unit between the two operands. ```APIDOC ## Amount.plus and Amount.minus ### Description Adds or subtracts two amounts of the same quantity. The result uses the most granular unit between the two operands. ### Method `Amount.plus(amount1: Amount, amount2: Amount): Amount` `Amount.minus(amount1: Amount, amount2: Amount): Amount` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const length1 = Amount.create(5, Units.Meter); const length2 = Amount.create(200, Units.Centimeter); // Addition - result in more granular unit (Centimeter) const sum = Amount.plus(length1, length2); // Result: { value: 700, unit: Units.Centimeter, decimalCount: 0 } // Subtraction const difference = Amount.minus(length1, length2); // Result: { value: 300, unit: Units.Centimeter, decimalCount: 0 } // Type safety: This would cause a TypeScript compile error // const invalid = Amount.plus(length1, mass); // Error: incompatible quantities ``` ### Response #### Success Response (200) - **amount** (Amount) - The resulting amount after addition or subtraction. #### Response Example ```json { "value": 700, "unit": { "name": "Centimeter", "symbol": "cm", "quantity": "Length" }, "decimalCount": 0 } ``` ``` -------------------------------- ### Convert Amount to String Source: https://context7.com/dividab/uom/llms.txt Returns a string representation of an amount, including its derived unit symbol. This is useful for displaying quantities in a human-readable format. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const length = Amount.create(10, Units.Meter); Amount.toString(length); // "10 m" const speed = Amount.create(100, Units.MeterPerSecond); Amount.toString(speed); // "100 m/s" const area = Amount.create(25, Units.SquareMeter); Amount.toString(area); // "25 m²" ``` -------------------------------- ### Create Base Unit Source: https://context7.com/dividab/uom/llms.txt Creates a base unit for a specific quantity. Base units are the foundation for all derived units in the UOM system. ```typescript import { Unit } from "uom"; // Create custom base units const CustomLength = Unit.createBase<"CustomLength"> ( "CustomLength", "CustomLength", "cl" ); const CustomMass = Unit.createBase<"CustomMass"> ( "CustomMass", "CustomMass", "cm" ); ``` -------------------------------- ### getUnitsForQuantity Source: https://github.com/dividab/uom/blob/master/docs/api.md Retrieves all units registered for a specific quantity. ```APIDOC ## getUnitsForQuantity(quantity) ### Description Get all units registered for quantity. ### Parameters - **quantity** (string) - Required - The quantity to retrieve units for. ``` -------------------------------- ### Create Derived Units by Multiplication and Division Source: https://context7.com/dividab/uom/llms.txt Creates derived units by multiplying or dividing existing units. This is fundamental for defining complex physical quantities. ```typescript import { Unit, BaseUnits } from "uom"; // Create velocity unit (meters per second) const MeterPerSecond = Unit.divide<"Velocity"> ( "MeterPerSecond", "Velocity", BaseUnits.Meter, BaseUnits.Second ); // Create area unit (square meters) const SquareMeter = Unit.times<"Area"> ( "SquareMeter", "Area", BaseUnits.Meter, BaseUnits.Meter ); // Create acceleration unit const MeterPerSecondSquared = Unit.divide<"Acceleration"> ( "MeterPerSecondSquared", "Acceleration", MeterPerSecond, BaseUnits.Second ); ``` -------------------------------- ### Add or Subtract Amounts Source: https://context7.com/dividab/uom/llms.txt Use Amount.plus and Amount.minus to add or subtract two amounts of the same quantity. The result uses the most granular unit between the two operands. Type safety prevents mixing incompatible quantities. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const length1 = Amount.create(5, Units.Meter); const length2 = Amount.create(200, Units.Centimeter); // Addition - result in more granular unit (Centimeter) const sum = Amount.plus(length1, length2); // Result: { value: 700, unit: Units.Centimeter, decimalCount: 0 } // Subtraction const difference = Amount.minus(length1, length2); // Result: { value: 300, unit: Units.Centimeter, decimalCount: 0 } // Type safety: This would cause a TypeScript compile error // const invalid = Amount.plus(length1, mass); // Error: incompatible quantities ``` -------------------------------- ### Retrieve units for a quantity Source: https://context7.com/dividab/uom/llms.txt Query all registered units associated with a specific physical quantity type. ```typescript import { UnitMap } from "uom"; import { Units } from "uom-units"; // Get all length units const lengthUnits = UnitMap.getUnitsForQuantity("Length", Units); // Result: [Units.Meter, Units.Foot, Units.Inch, Units.Kilometer, ...] // Get all temperature units const tempUnits = UnitMap.getUnitsForQuantity("Temperature", Units); // Result: [Units.Kelvin, Units.Celsius, Units.Fahrenheit] // Get all mass units const massUnits = UnitMap.getUnitsForQuantity("Mass", Units); // Result: [Units.Kilogram, Units.Gram, Units.PoundLb, ...] ``` -------------------------------- ### Unit API Source: https://github.com/dividab/uom/blob/master/docs/api.md Provides functionalities related to units, including retrieving units for a given quantity. ```APIDOC ## Unit API ### Description Provides functionalities related to units, including retrieving units for a given quantity. ### Methods #### getUnitsForQuantity(quantity) Get all units registered for a given quantity. ##### Parameters - **quantity** (Quantity) - The quantity for which to retrieve units. ``` -------------------------------- ### Derive Unit Symbols Source: https://github.com/dividab/uom/blob/master/README.md Automatically generate a symbolic representation (e.g., 'm/s') for a unit based on its constituent base units. ```typescript import { Amount, Format } from "uom"; import { Units } from "uom-units"; const length = Amount.create(10, Units.MeterPerSecond); const label = Unit.buildDerivedSymbol(length); console.log(label); // m/s ``` -------------------------------- ### Round Amount Up and Down Source: https://context7.com/dividab/uom/llms.txt Rounds an amount to the nearest step value, either up or down. Ensure the step and amount share compatible units. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const temperature = Amount.create(23, Units.Celsius); const step = Amount.create(5, Units.Celsius); // Round down to nearest 5 degrees const roundedDown = Amount.roundDown(step, temperature); // Result: { value: 20, unit: Units.Celsius, decimalCount: 0 } // Round up to nearest 5 degrees const roundedUp = Amount.roundUp(step, temperature); // Result: { value: 25, unit: Units.Celsius, decimalCount: 0 } const length = Amount.create(17, Units.Meter); const lengthStep = Amount.create(10, Units.Meter); Amount.roundDown(lengthStep, length); // 10m Amount.roundUp(lengthStep, length); // 20m ``` -------------------------------- ### Create Transformed Units with Numeric Factors Source: https://context7.com/dividab/uom/llms.txt Creates transformed units by applying a numeric factor to an existing unit. Useful for defining units that are scaled versions of base units. ```typescript import { Unit, BaseUnits } from "uom"; // Create inch from meter (1 inch = 1/12 foot = 0.0254 meters) const Inch = Unit.timesNumber("Inch", 0.0254, BaseUnits.Meter); // Create foot from meter const Foot = Unit.timesNumber("Foot", 0.3048, BaseUnits.Meter); // Create kilometer from meter const Kilometer = Unit.timesNumber("Kilometer", 1000, BaseUnits.Meter); // Create centimeter (1/100 of a meter) const Centimeter = Unit.divideNumber("Centimeter", 100, BaseUnits.Meter); ``` -------------------------------- ### Unit.convert Source: https://github.com/dividab/uom/blob/master/docs/api.md Converts a numeric value from one unit to another. ```APIDOC ## Unit.convert(value, fromUnit, toUnit) ### Description Converts numeric values from a unit to another unit. ### Parameters - **value** (number) - Required - The numeric value to convert. - **fromUnit** (string) - Required - The unit from which to convert the numeric value. - **toUnit** (string) - Required - The unit to which to convert the numeric value. ### Response - **Returns** (number) - The converted numeric value. ``` -------------------------------- ### Amount.valueAs Source: https://context7.com/dividab/uom/llms.txt Converts an amount's value to a different unit of the same quantity. Returns the numeric value in the target unit. ```APIDOC ## Amount.valueAs ### Description Converts an amount's value to a different unit of the same quantity. Returns the numeric value in the target unit. ### Method `Amount.valueAs(targetUnit: Unit, amount: Amount): number` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const meters = Amount.create(10, Units.Meter); // Convert meters to inches const inchValue = Amount.valueAs(Units.Inch, meters); // Result: 393.7007874015748 // Convert meters to feet const feetValue = Amount.valueAs(Units.Foot, meters); // Result: 32.80839895013123 // Temperature conversion const celsius = Amount.create(100, Units.Celsius); const fahrenheitValue = Amount.valueAs(Units.Fahrenheit, celsius); // Result: 212 ``` ### Response #### Success Response (200) - **value** (number) - The numeric value in the target unit. #### Response Example ```json 393.7007874015748 ``` ``` -------------------------------- ### Amount.times and Amount.divide Source: https://context7.com/dividab/uom/llms.txt Multiplies or divides an amount by a scalar number. Preserves the original unit and decimal count. ```APIDOC ## Amount.times and Amount.divide ### Description Multiplies or divides an amount by a scalar number. Preserves the original unit and decimal count. ### Method `Amount.times(amount: Amount, scalar: number): Amount` `Amount.divide(amount: Amount, scalar: number): Amount` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const distance = Amount.create(100, Units.Meter, 2); // Multiply by scalar const doubled = Amount.times(distance, 2); // Result: { value: 200, unit: Units.Meter, decimalCount: 2 } // Divide by scalar const halved = Amount.divide(distance, 2); // Result: { value: 50, unit: Units.Meter, decimalCount: 2 } const quarter = Amount.divide(distance, 4); // Result: { value: 25, unit: Units.Meter, decimalCount: 2 } ``` ### Response #### Success Response (200) - **amount** (Amount) - The resulting amount after multiplication or division. #### Response Example ```json { "value": 200, "unit": { "name": "Meter", "symbol": "m", "quantity": "Length" }, "decimalCount": 2 } ``` ``` -------------------------------- ### Type-Safe Amount Calculations in TypeScript Source: https://github.com/dividab/uom/blob/master/README.md Leverage TypeScript generics to ensure functions only accept and return amounts of a specific quantity type, preventing type errors. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const length1 = Amount.create(10, Units.Meter); const length2 = Amount.create(10, Units.Inch); const volume1 = Amount.create(10, Units.CubicMeter); const result = calculate(length1, length2); // OK // const result = calculate(volume1, length2); // Compile error function calculate(Amount length1, Amount length2): Amount { return Amount.plus(length1, length2); } ``` -------------------------------- ### Convert Numeric Values Between Units Source: https://context7.com/dividab/uom/llms.txt Converts a numeric value directly from one unit to another without creating Amount objects. This is efficient for simple conversions. ```typescript import { Unit, BaseUnits } from "uom"; import { Units } from "uom-units"; // Convert 10 meters to feet const feet = Unit.convert(10, Units.Meter, Units.Foot); // Result: 32.80839895013123 // Convert 100 Celsius to Fahrenheit const fahrenheit = Unit.convert(100, Units.Celsius, Units.Fahrenheit); // Result: 212 // Convert 1 kilogram to pounds const pounds = Unit.convert(1, Units.Kilogram, Units.PoundLb); // Result: 2.204622621848776 ``` -------------------------------- ### Implement type-safe calculations Source: https://context7.com/dividab/uom/llms.txt Use TypeScript generics to enforce unit compatibility and prevent invalid arithmetic operations. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; // Define a type-safe calculation function function calculateArea( width: Amount.Amount, height: Amount.Amount ): number { const widthMeters = Amount.valueAs(Units.Meter, width); const heightMeters = Amount.valueAs(Units.Meter, height); return widthMeters * heightMeters; } // Valid usage const width = Amount.create(10, Units.Meter); const height = Amount.create(5, Units.Foot); const area = calculateArea(width, height); // Result: 15.24 (square meters) // TypeScript will prevent mixing incompatible quantities: // const mass = Amount.create(10, Units.Kilogram); // calculateArea(width, mass); // Compile error! // Type-safe velocity calculation function calculateVelocity( distance: Amount.Amount<"Length">, time: Amount.Amount<"Duration"> ): number { const meters = Amount.valueAs(Units.Meter, distance); const seconds = Amount.valueAs(Units.Second, time); return meters / seconds; } ``` -------------------------------- ### Multiply or Divide Amounts by a Scalar Source: https://context7.com/dividab/uom/llms.txt Use Amount.times and Amount.divide to multiply or divide an amount by a scalar number. The original unit and decimal count are preserved. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const distance = Amount.create(100, Units.Meter, 2); // Multiply by scalar const doubled = Amount.times(distance, 2); // Result: { value: 200, unit: Units.Meter, decimalCount: 2 } // Divide by scalar const halved = Amount.divide(distance, 2); // Result: { value: 50, unit: Units.Meter, decimalCount: 2 } const quarter = Amount.divide(distance, 4); // Result: { value: 25, unit: Units.Meter, decimalCount: 2 } ``` -------------------------------- ### Convert Amount Value to Different Units Source: https://context7.com/dividab/uom/llms.txt Use Amount.valueAs to convert an amount's value to a different unit of the same quantity. This function returns the numeric value in the target unit. ```typescript import { Amount } from "uom"; import { Units } from "uom-units"; const meters = Amount.create(10, Units.Meter); // Convert meters to inches const inchValue = Amount.valueAs(Units.Inch, meters); // Result: 393.7007874015748 // Convert meters to feet const feetValue = Amount.valueAs(Units.Foot, meters); // Result: 32.80839895013123 // Temperature conversion const celsius = Amount.create(100, Units.Celsius); const fahrenheitValue = Amount.valueAs(Units.Fahrenheit, celsius); // Result: 212 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.