npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

math-evaluate

v0.1.0

Published

Lightweight math expression evaluator without eval or third-party dependencies.

Readme

math-evaluate

English

轻量级数学表达式求值库,适用于 JavaScript 与 TypeScript。实现上不使用 evalnew Function,也没有第三方运行时依赖。

特性

  • 支持四则运算与取余、幂运算(+-*/%^),比较运算(>>=<<===!=),逻辑运算(&&||!
  • 内置常量:epitruefalse
  • 内置函数:absacosasinatanceilcosexpfloorlnlogmaxminpowroundsinsqrttan
  • 可通过第二个参数传入自定义变量(作用域对象)
  • 出错时抛出 MathEvaluateError,信息中包含出错位置

安装方式

npm install math-evaluate
yarn add math-evaluate
pnpm add math-evaluate

使用方式

ESM(import 导入)

import {
  evaluateMathExpression,
  evaluate,
  MathEvaluateError,
} from 'math-evaluate'

console.log(evaluateMathExpression('2 + 3 * 4')) // 14
console.log(evaluate('x ^ 2', { x: 3 })) // 9

try {
  evaluate('不存在的变量')
} catch (e) {
  if (e instanceof MathEvaluateError) {
    console.error(e.message)
  }
}

CommonJS(require 导入)

const {
  evaluateMathExpression,
  evaluate,
  MathEvaluateError,
} = require('math-evaluate')

console.log(evaluateMathExpression('sin(pi / 2)')) // 1

浏览器(<script> 标签,全局变量)

先在仓库根目录执行 npm run build 生成 dist/index.global.js。在 HTML 里用相对路径指向该文件即可(请按页面文件与 dist 的实际位置调整,例如页面位于 demo/browser/index.html 时可写 ../../dist/index.global.js):

<script src="../../dist/index.global.js"></script>
<script>
  // 全局对象名:MathEvaluate
  console.log(MathEvaluate.evaluateMathExpression('1 + 2')) // 3
  console.log(MathEvaluate.evaluate('a > b', { a: 2, b: 1 })) // true
</script>

API 说明

| 导出项 | 说明 | |--------|------| | evaluateMathExpression(expression, scope?) | 计算表达式 expression;可选参数 scope 为变量名到 numberboolean 的映射。 | | evaluate | 与 evaluateMathExpression 等价,命名上接近常见 evaluate 习惯。 | | MathEvaluateError | 语法错误、未知变量或函数、非有限数值结果等情况下抛出。 | | MathEvaluateValue | 类型:number \| boolean | | MathEvaluateScope | 类型:Record<string, MathEvaluateValue> |

开源协议

MIT