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

expr-formula

v0.1.0

Published

像写 Excel 一样写计算规则,然后直接算出结果。支持多行计算步骤、变量默认值、JS/Go 代码导出。

Downloads

153

Readme

expr-formula

像写 Excel 一样写计算规则,然后用代码直接算出结果。

这是什么

一个计算引擎。你把计算步骤写成文字,它就能帮你算出来。比如:

import { evaluate } from 'expr-formula'

const 结果 = evaluate(`
  面积 = 长 * 宽
  板材费 = 面积 * $板材单价
  总价 = 板材费 + 100
`, { 长: 10, 宽: 5 }, { 板材单价: 30 })

结果.总价  // → 1600

能做什么

  1. 写计算步骤:一行一个算式,下一行可以用上一行的结果
  2. 设默认价格$板材单价 = 120,如果外面不传就用这个值
  3. 传参数覆盖:外面传 { 板材单价: 200 } 就覆盖掉 120
  4. 生成 JS/Go 代码:导出到别的语言直接用
  5. 倒推需要哪些参数inspect() 告诉你需要填什么

怎么写计算步骤

和写数学作业差不多,从上往下:

$板材单价 = 120      ← 设定默认价格(外面可以覆盖掉)
$人工费 = 50

面积 = 长 * 宽         ← 每行一个算式
板材费 = 面积 * $板材单价
总价 = 板材费 + $人工费

OUTPUT: 总价           ← 标记"总价"是最终结果(不写的话,最后一行就是结果)

写完后,外面传入 { 长: 10, 宽: 5 },它能算出:

面积 → 50
板材费 → 6000
总价 → 6050

能用的函数(27个)

就像 Excel 里的那些函数:

数学

ABS(数) 取绝对值 · INT(3.9) → 3(向下取整) · MOD(10, 3) → 1(取余) · POWER(2, 3) → 8(几次方) · SQRT(16) → 4(开平方) · ROUND(3.7) → 4 · ROUNDUP(3.1) → 4 · ROUNDDOWN(3.9) → 3 · SUM(1,2,3) → 6 · AVERAGE(1,2) → 1.5 · MIN(5,3,9) → 3 · MAX(5,3,9) → 9

条件判断

  • IF(条件, 成立给这个, 不成立给这个) — 比如 IF(数量 >= 100, 80, 100),数量够 100 就给 80,不够给 100
  • AND(条件1, 条件2) — 都成立才是真
  • OR(条件1, 条件2) — 有一个成立就是真
  • NOT(条件) — 反过来

查表

SWITCH(查什么, 匹配值1, 返回1, 匹配值2, 返回2, ..., 都没匹配返回这个)

比如一个下拉框选了"柯式-彩白",你想返回 20:

面1费 = SWITCH(面1工艺, 0, 10, 1, 20, 2, 30, 0)
  • 选了 0 → 返回 10
  • 选了 1 → 返回 20
  • 选了 2 → 返回 30
  • 选了别的 → 返回 0

LOOKUPSWITCH 用法完全一样,只是名字不同。

日期时间

  • NOW_HOUR() — 现在是几点(0-23),比如用来判断"超过下午3点的单加急"
  • NOW_MINUTE() — 现在是几分
  • NOW() — 当前时间戳(毫秒)
  • DATEADD(时间戳, 几天) — 加几天(填负数就是减几天)
  • HOUR(时间戳) MINUTE(时间戳) DAY(时间戳) — 从时间戳里提取时分天

示例:超过下午 3 点就加急

急单加价 = IF(NOW_HOUR() >= 15, 50, 0)

导出给其他语言用

import { compile } from 'expr-formula'

// 导出为 JavaScript 函数
compile('面积 = 长 * 宽\n总价 = 面积 * $单价', 'js')

// 导出为 Go 函数
compile('面积 = 长 * 宽\n总价 = 面积 * $单价', 'go')

安装

npm install expr-formula