@danielsimonjr/mathts-expression

中文文档
0.12.7发布于 2026/11/11MITTypeScriptES Module近 30 天下载 5215

Expression parser and evaluator for MathTS

原始使用文档
文档目录4 节
  1. Install
  2. What it provides
  3. Serializing an AST
  4. License

@danielsimonjr/mathts-expression

Expression parser & evaluator for MathTS. The package root also re-exports the MathNode and StringOptions types used by scalarToNode / scalarToTex in @danielsimonjr/mathts-functions.

The expression engine for MathTS: a parser (string → AST), an AST node set, a compiler, and an evaluator. Includes a sandbox (getSafeProperty / getSafeMethod) for safe property access.

Limits on untrusted strings: max length 1,000,000 characters, max parenthesis nesting 125 (and 500 nested unary, power, assignment or conditional rules), max compile depth 512, which evaluate also applies before validation (see expression/tests/security/limits.test.ts and docs/api/expression.md).

Install

npm install @danielsimonjr/mathts-expression

What it provides

  • createParse / createParserClass / createParser — parse to an AST.
  • 16 AST node constructors (createConstantNode, createOperatorNode, …).
  • compile / createEvaluate / compileExpression — evaluate expressions.
  • createScalarBuilder — build a scalar node tree (number, symbol, op, call, from). ln lowers to natural log and log lowers to log10. A formula string is rejected. Evaluate with evaluateScalar from @danielsimonjr/mathts-functions.
  • scalarCanonical / scalarCanonicalKey / scalarSize / scalarDepth / scalarComplexity / scalarSymbols — structural toolkit for an existing ScalarNode. The canonical key flattens + and *, rewrites - and /, and does not fold 2^3 to 8.
  • enumerateScalars / scalarGrammarAccepts — deterministic size-ordered enumeration of scalar trees from a grammar, with deduplication by scalarCanonicalKey. It is a generator that returns an EnumerationStop reason (exhausted-space, candidate-limit, time-limit, cancelled). budget.maxCandidates, budget.maxWallClockMs, and an AbortSignal end it; the wall clock and the signal are checked every 128 candidates considered, so a grammar that yields few trees still stops promptly.
import { createScalarBuilder, scalarEquals } from '@danielsimonjr/mathts-expression';

const b = createScalarBuilder();
const aa = b.op('*', [b.symbol('a'), b.symbol('a')]);
const aSquared = b.op('^', [b.symbol('a'), b.number(2)]);
console.log(scalarEquals(aa, aSquared)); // true
  • getStringSubset / setStringSubset — string indexing used by accessor/assign and by functions subset (the barrel is the only public entry).
  • AST serializers on every node: toString(), toTex() (LaTeX), toHTML(), and toMathML() (MathML).
  • Focused re-exports are also published: @danielsimonjr/mathts-{parser,ast,evaluator}.

Serializing an AST

Every node serializes to several formats. toMathML() returns a MathML fragment (like toTex() returns a LaTeX fragment, without delimiters); wrap it in a <math> element with mathMLDocument(node) to render. MathML is typeset natively by modern browsers — no external dependencies.

import { mathMLDocument, mathMLError } from '@danielsimonjr/mathts-expression';

const node = parse('c = 1 / sqrt(eps0 * mu0)'); // parse from the functions package
node.toString(); // 'c = 1 / sqrt(eps0 * mu0)'
node.toTex(); // ' c=\\frac{1}{\\sqrt{ eps0\\cdot mu0}}'
node.toMathML(); // '<mrow><mi>c</mi><mo>=</mo><mfrac>…</mfrac></mrow>'
mathMLDocument(node); // '<math xmlns="…" display="block">…</math>'  (renderable)

mathMLError(src) produces a <math><merror> element for a parse failure.

License

MIT (c) Daniel Simon Jr.

版本与发布记录

@danielsimonjr/mathts-expression 共发布 46 个版本,首次发布于 2026/04/05;过去 12 个月发布了 46 个版本。

版本发布日期距今
0.12.7latest2026/11/11今天
0.12.62026/11/10今天
0.12.52026/11/101 天前
0.12.42026/11/092 天前
0.12.32026/11/092 天前
0.12.22026/11/073 天前

关于 @danielsimonjr/mathts-expression 的常见问题

@danielsimonjr/mathts-expression 是什么?

Expression parser and evaluator for MathTS

如何安装 @danielsimonjr/mathts-expression?

在项目目录运行 npm install @danielsimonjr/mathts-expression,或使用 pnpm add / yarn add / bun add。

@danielsimonjr/mathts-expression 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@danielsimonjr/mathts-expression 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@danielsimonjr/mathts-expression 可以商用吗?

@danielsimonjr/mathts-expression 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@danielsimonjr/mathts-expression 的下载量有多少?

根据 npm 官方下载统计,@danielsimonjr/mathts-expression 在 2026/10/12 – 2026/11/10 这 30 天内被下载 5,215 次,日均约 174 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@danielsimonjr/mathts-expression 还在维护吗?

最新版本 0.12.7 发布于 2026/11/11(今天);至今共发布 46 个版本,其中过去 12 个月发布了 46 个;首次发布于 2026/04/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。