(expr: MathJsonExpression | null)
| 472 | |
| 473 | /** The number of leaves (atomic expressions) in the expression */ |
| 474 | export 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 */ |
| 501 | export function matchesNumber(s: string): boolean { |
no test coverage detected