(expr: UnitExpression)
| 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 | */ |
| 491 | export 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 | } |
no test coverage detected