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

Function countLeaves

src/math-json/utils.ts:474–498  ·  view source on GitHub ↗
(expr: MathJsonExpression | null)

Source from the content-addressed store, hash-verified

472
473/** The number of leaves (atomic expressions) in the expression */
474export function countLeaves(expr: MathJsonExpression | null): number {
475 if (expr === null) return 0;
476 if (typeof expr === 'number' || typeof expr === 'string') return 1;
477 if (isNumberExpression(expr) || isSymbolObject(expr) || isStringObject(expr))
478 return 1;
479
480 if (Array.isArray(expr)) return countFunctionLeaves(expr);
481 if ('fn' in expr) return countFunctionLeaves(expr.fn);
482
483 const dict = dictionaryFromExpression(expr);
484 if (dict) {
485 const dictRecord = dict as unknown as Record<
486 string,
487 MathJsonExpression | null
488 >;
489 const keys = Object.keys(dictRecord);
490 return (
491 1 +
492 keys.length +
493 keys.reduce<number>((acc, x) => acc + countLeaves(dictRecord[x]), 0)
494 );
495 }
496
497 return 0;
498}
499
500/** True if the string matches the expected pattern for a number */
501export function matchesNumber(s: string): boolean {

Callers 3

getFractionStyleFunction · 0.90
isSinglePowerDenominatorFunction · 0.90
countFunctionLeavesFunction · 0.70

Calls 6

isNumberExpressionFunction · 0.85
isSymbolObjectFunction · 0.85
isStringObjectFunction · 0.85
countFunctionLeavesFunction · 0.85
dictionaryFromExpressionFunction · 0.85
keysMethod · 0.65

Tested by

no test coverage detected