@emmetio/math-expression

中文文档
1.0.5发布于 2023/05/10MITTypeScriptESM + CJS近 30 天下载 27.3万

Parse and evaluate simple math expressions

原始使用文档

Math expression parser and evaluator

Fast and small (less than 2KB minified and gzipped) zero-dependency math expression parser and evaluator.

import evaluate from '@emmetio/math-expression';

console.log(evaluate('2 * (-3 + 5)')); // 4

Motivation

In JavaScript, you can use built-in eval() function to evaluate expressions:

console.log(eval('2 * (-3 + 5)')); // 4

But in most modern JavaScript environments eval() function is considered harmful since it can evaluate any arbitrary JS code. And in some cases (like in Atom editor) it’s disabled by default due to security reasons.

With @emmetio/math-expression module you can safely parse & evaluate basic math expressions in any JS environment.

Extract expression from text

This module is used by Emmet project to evaluate math expression in-place in code source. A default user workflow is to enter math expression somewhere in source code and run action to evaluate it. You can use extract function to extract math expression from given source code, starting at specified position:

import evaluate, { extract } from '@emmetio/math-expression';

const code = 'Here goes math: 2 + 3 foo bar';
const [start, end] = extract(code, 21); // 16, 21
const expr = code.substring(start, end); // 2 + 3
console.log(evaluate(expr)); // 5

版本与发布记录

@emmetio/math-expression 共发布 8 个版本,首次发布于 2017/04/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.5latest2023/05/103 年前
1.0.42020/04/156 年前
1.0.32020/04/126 年前
1.0.22020/04/126 年前
1.0.12019/10/197 年前
1.0.02019/10/167 年前

关于 @emmetio/math-expression 的常见问题

@emmetio/math-expression 是什么?

Parse and evaluate simple math expressions

如何安装 @emmetio/math-expression?

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

@emmetio/math-expression 支持 TypeScript 吗?

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

@emmetio/math-expression 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@emmetio/math-expression 可以商用吗?

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

@emmetio/math-expression 的下载量有多少?

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

@emmetio/math-expression 还在维护吗?

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