AbstractCreate a quantity from an expression, a scalar and units, or a parsed definition.
A full expression such as "3 m", a scalar paired with
initUnits, or a QuantityDefinition / existing Quantity.
Nonempty unit expression for a separate scalar. A scalar in
this expression is replaced by initValue. Ignored for quantity and definition inputs.
Parser used for string expressions; supplied by the configured entry point. Required even for definitions; retained for subsequent string operations.
If inputs cannot be parsed, an absolute temperature is below absolute zero, or absolute-temperature units occur in a compound expression.
Numbers, bigints, native backend scalars, numeric strings and objects with numeric toString()
output create dimensionless quantities when units are omitted or empty. Use a
decimal string or Decimal when preserving all input digits matters.
An existing quantity keeps its units and converts its scalar to this backend.
Absolute readings account for any difference between the represented absolute-zero boundaries.
Native destinations use numeric conversion; rational-capable backends can exchange
exact terms. Other custom representations fall back to numeric text. Destination range and
representation limits apply, and precision already lost cannot be recovered.
StaticwithCreate a class with isolated numerical settings and optional cache limits.
Nested numeric options and cache policies, merged with this class's settings.
A new class whose instances and derived results retain these settings.
Subclasses with a different constructor signature must override the
protected constructQuantity hook. Configuration preserves a constructor's
signature; it does not supply additional subclass arguments to derived results.
Numeric settings require a configurable adapter. Omitted settings inherit from
this class; cache: false disables optional Quantity caches and true resets them.
The original scalar/string input, or a normalized value-and-units snapshot for Quantity and definition inputs. Numeric toString() objects are saved as their parsed backend scalar. Copies do not retain the source object. Snapshots are created on first access, frozen, and reused on subsequent reads.
Numerical value in this quantity’s units, represented by the selected backend.
Use qty.numeric.toString(qty.scalar) for numeric text across backends, or
qty.numeric.toNumber(qty.scalar) for a JavaScript number (which may lose precision).
Read-only; create a new quantity to change the value.
Dimensional signature. Prefer isCompatible over interpreting this string.
Format the counted units as a unit expression.
The expression without its scalar; an empty string for a unitless quantity.
Spelling and grouping may differ from the input. Powers use compact
notation such as m2; numerator multiplication uses * and denominator
multiplication uses tightly coupled ., as in kg/m.s. Output aliases preserve
unit identity when parsed by any of the three bundled parsers. The result is cached when
cache.metadata is enabled; custom parsers may use a different input syntax.
Convert to compatible units, or reciprocal units by inverting the quantity.
Target unit expression or Quantity. Only the units are used; any target scalar is ignored, including zero or negative values.
The quantity expressed in the target units. Empty target strings and unchanged units return this instance; repeated conversions can return cached objects.
If parsing fails, dimensions are neither compatible nor reciprocal, or reciprocal conversion requires inverting a zero value or absolute temperature. Invalid absolute-temperature results throw; valid results requiring a negative-infinite reading or unrepresentable absolute-zero boundary throw RangeError.
An existing target supplies units only, including when its backend differs. The result keeps this quantity's backend, configuration and parser.
import { Quantity as NumberQuantity } from '@neutrium/quantity/number';
const mass = new Quantity('25 kg');
mass.to('g').scalar.toString(); // "25000"
mass.to(new Quantity('3 g')).scalar.toString(); // "25000"
mass.to(new NumberQuantity('0 g')).scalar.toString(); // "25000"
new Quantity('2 m').to('m^-1').scalar.toString(); // "0.5"
Add a compatible quantity, accounting for its unit scale.
A quantity expression, definition, Quantity, or scalar input. Scalars without units are dimensionless and require a compatible receiver.
A new quantity, normally in this quantity's units. Adding degrees to an absolute temperature returns an absolute temperature.
Divide by a scalar or combine units with another quantity.
A number, backend scalar, numeric toString() object, quantity expression, definition, or Quantity.
A new quotient. Divisors without unit tokens preserve the current units, including numeric strings and dimensionless Quantity instances. Compatible quantities align to the left operand's units, except for temperature degrees. Decimal rounds the complete quotient once; other backends follow their own arithmetic.
If parsing fails, the divisor is an absolute temperature, or an absolute temperature is divided by a value with units. Incompatible backends throw TypeError. Invalid absolute readings throw; valid but unrepresentable negative readings throw RangeError.
Multiply by a scalar or combine units with another quantity.
A number, backend scalar, numeric toString() object, quantity expression, definition, or Quantity.
A new product. Scalars and quantities without unit tokens preserve the other operand's units. Compatible quantities align to the left operand's units, except for temperature degrees. Decimal rounds the complete product once; other backends follow their own arithmetic and intermediate rounding.
Raise the scalar and units to an exact rational power.
Exponent as a number, decimal or fraction string, backend scalar, or rational record. Negative exponents invert the unit expression.
A new quantity with the powered scalar and units.
If the exponent is invalid, the adapter cannot evaluate a fractional power, an even root has a negative scalar, or the resulting units violate absolute-temperature restrictions. Exponents and resulting counts outside the safe-integer magnitude range throw RangeError. Decimal exponent expansion is limited to 4096 places.
Exponent zero returns the dimensionless identity, with scalar one. Fractional powers require rational extraction and construction or a scientific adapter. Both Decimal adapter tiers support them. If a custom adapter lacks rational extraction and rounds its exponent text, pass an explicit exponent string or rational record. Units are stored as counters; powers never allocate repeated unit entries. Exponent validation is independent of the numeric backend's range settings; those settings apply to the resulting scalar.
Subtract a compatible quantity, accounting for its unit scale.
A quantity expression, definition, Quantity, or scalar input. Scalars without units are dimensionless and require a compatible receiver.
A new quantity in this quantity's units, except that subtracting two absolute temperatures returns temperature degrees.
Compare compatible physical values, or a numeric value in the current units.
-1 if this quantity is smaller, 0 if equal, or 1 if larger;
undefined when either value is NaN. Boolean comparisons return false for NaN.
Test whether this physical value is equal to another compatible value.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether this physical value is greater than another compatible value.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether this physical value is greater than or equal to another compatible value.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether two quantities have the same dimensional signature.
A Quantity or unit expression; numeric inputs return false.
Whether dimensions match, without comparing scalars or converting units. Absolute temperatures and temperature degrees share a signature. This check does not establish backend compatibility or permission for temperature arithmetic.
Test whether another quantity has reciprocal dimensions.
A Quantity or expression describing the reciprocal units.
Whether all dimensional exponents are the negatives of those in b.
Scalars are ignored, including zero. Absolute temperatures and temperature
degrees share a dimension; this check does not imply that inversion is allowed.
Test whether this physical value is less than another compatible value.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test whether this physical value is less than or equal to another compatible value.
Whether the comparison holds. Numeric operands compare directly with the scalar; Quantity and string operands compare compatible physical values. NaN returns false.
compareTo for three-way comparison.
Test exact scalar equality and matching normalized unit records.
Whether scalars and ordered unit/prefix/exponent records match. Different compatible units return false even when they represent the same physical value. NaN returns false.
Test for a standalone, unprefixed temperature unit, including absolute temperatures.
True for both degC-style intervals and tempC-style absolute temperatures.
To identify an unprefixed interval, combine this with !qty.isTemperature().
Prefixed and compound interval units return false; use isCompatible('degK')
for a dimensional check.
Complete immutable configuration, including the adapter's numeric option types, retained by derived quantities.
Effective conversion-cache limits, retained by derived quantities.
Arithmetic backend fixed when this instance is constructed.
Effective parser settings, including an explicitly supplied parser's settings when exposed.
StaticconfigEffective settings; isolated classes keep this frozen snapshot for their lifetime.
StaticconversionImmutable limits applied separately to each instance's conversion caches.
StaticparserEffective parser settings; configuring parser options creates a separate cache scope.
A numeric scalar paired with units, with conversion, arithmetic, and comparison operations.
Import from
@neutrium/quantity. Construct quantities withnewand read the numerical result through scalar; units returns the unit expression.Remarks
Existing Quantity operands used for arithmetic or value comparisons must have compatible numeric backends. Use
new DestinationQuantity(source)to convert explicitly. Dimensional predicates and conversion targets use units only. Arithmetic produces quantities without changing the operands. Conversions may return the same instance or a cached result. Treat quantities and their unit arrays as immutable. Public values are getter-only; create a new quantity to change them. Counted unit records and arrays are frozen and may be shared.Methods are shared on the prototype and require a Quantity receiver. When passing a method as a callback, use an arrow wrapper or bind it to the instance.
Example
See