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

Function getExpressionScale

src/compute-engine/numerics/unit-data.ts:469–503  ·  view source on GitHub ↗
(expr: UnitExpression)

Source from the content-addressed store, hash-verified

467 }
468
469 if (op === 'Power') {
470 if (expr.length !== 3) return null;
471 const d = getExpressionDimension(expr[1]);
472 const exp = expr[2];
473 if (!d || typeof exp !== 'number') return null;
474 return d.map((v) => v * exp) as DimensionVector;
475 }
476
477 return null;
478}
479
480/**
481 * Compute the scale factor for a MathJSON unit expression relative to
482 * coherent SI.
483 *
484 * - If `expr` is a string, delegates to `getUnitScale`.
485 * - `["Multiply", a, b, ...]` — multiplies scales.
486 * - `["Divide", a, b]` — a.scale / b.scale.
487 * - `["Power", base, exp]` — base.scale ^ exp.
488 *
489 * Returns `null` if any component is unrecognised.
490 */
491export function getExpressionScale(expr: UnitExpression): number | null {
492 if (typeof expr === 'string') return getUnitScale(expr);
493
494 if (!Array.isArray(expr) || expr.length < 2) return null;
495
496 const op = expr[0];
497
498 if (op === 'Multiply') {
499 let result = 1;
500 for (let i = 1; i < expr.length; i++) {
501 const s = getExpressionScale(expr[i]);
502 if (s === null) return null;
503 result *= s;
504 }
505 return result;
506 }

Callers 7

units.test.tsFile · 0.90
quantityCompareFunction · 0.90
units.tsFile · 0.90
quantityAddFunction · 0.90
quantityDivideFunction · 0.90
simplifyQuantityUnitFunction · 0.90
convertCompoundUnitFunction · 0.85

Calls 2

getUnitScaleFunction · 0.85
powMethod · 0.65

Tested by

no test coverage detected