MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / convertCompoundUnit

Function convertCompoundUnit

src/compute-engine/numerics/unit-data.ts:641–661  ·  view source on GitHub ↗
(
  value: number,
  fromUnit: UnitExpression,
  toUnit: UnitExpression
)

Source from the content-addressed store, hash-verified

639 }
640
641 for (const sym of members) {
642 if (sym === rep) continue;
643 const e = result.get(sym)!;
644 const s = getUnitScale(sym)!;
645 magnitudeScale *= (s / repScale) ** e;
646 result.set(rep, result.get(rep)! + e);
647 result.delete(sym);
648 }
649 if (result.get(rep) === 0) result.delete(rep);
650 }
651
652 return { factors: result, magnitudeScale };
653}
654
655/**
656 * Rebuild a canonical `UnitExpression` from a factor map (symbol → exponent).
657 *
658 * Numerator factors (positive exponents) become a bare symbol, a `Power`, or
659 * a `Multiply`; denominator factors (negative exponents) go under a `Divide`.
660 * When there are only denominator factors, negative `Power` forms are emitted
661 * directly (matching what `quantityDivide`'s scalar/Quantity path produces),
662 * since a numeric `1` is not a valid unit symbol.
663 *
664 * Returns `null` for an empty map (fully cancelled → dimensionless).

Callers 3

units.test.tsFile · 0.90
units.tsFile · 0.90
quantityAddFunction · 0.90

Calls 4

convertUnitFunction · 0.85
getExpressionDimensionFunction · 0.85
dimensionsEqualFunction · 0.85
getExpressionScaleFunction · 0.85

Tested by

no test coverage detected