一个可序列化为 JSON 的表达式 DSL 库,提供类型安全的表达式构建、编译和求值。
- 类型安全 - 使用 TypeScript 泛型进行完整的类型推导
- 可序列化 - 编译后的表达式为纯 JSON 格式
- 短路求值 - 支持
&&、||、??和三元表达式的控制流优化 - 内联优化 - 自动内联只被引用一次的子表达式
import { variable, expr, compile, evaluate, t } from "@codehz/json-expr";
// 定义类型化变量(使用 TypeScript 泛型,无需 Zod)
const x = variable<number>();
const y = variable<number>();
// 构建表达式
const sum = expr({ x, y })("x + y");
const result = expr({ sum, x })("sum * x");
// 使用模板字符串
const name = variable<string>();
const greeting = t`Hello, ${name}!`;
// 编译并执行
const compiled = compile(result, { x, y });
const value = evaluate(compiled, { x: 2, y: 3 }); // => 10src/
├── index.ts # 导出入口
├── variable.ts # variable<T>() 函数
├── expr.ts # expr() 函数
├── template.ts # t() 标签模板函数 - 支持模板字符串插值
├── compile.ts # 编译器(内联优化、短路求值)
├── evaluate.ts # 运行时求值
├── parser.ts # 表达式 AST 解析器
├── type-parser.ts # TypeScript 类型级表达式解析
├── proxy-variable.ts # Proxy 变量实现 - 支持属性访问和方法调用
├── proxy-metadata.ts # Proxy 元数据管理
├── types.ts # 类型定义(Variable、Expression、ControlFlowNode 等)
└── *.test.ts # 测试文件
编译后的数据为 JSON 数组:[variableNames, ...expressions]
- 变量用
$N引用(N 为索引) - 支持控制流节点:
["br", condition, offset]- 条件跳转["jmp", offset]- 无条件跳转["phi"]- 取最近求值结果
表达式中可直接使用以下全局对象(无需在上下文中定义):
Math,JSON,Date,RegExpNumber,String,Boolean,Array,Objectundefined,NaN,InfinityisNaN,isFinite,parseInt,parseFloat
Default to using Bun instead of Node.js.
- Use
bun <file>instead ofnode <file>orts-node <file> - Use
bun testinstead ofjestorvitest - Use
bun installinstead ofnpm installoryarn installorpnpm install - Use
bun run <script>instead ofnpm run <script>oryarn run <script>orpnpm run <script> - Bun automatically loads .env, so don't use dotenv.
bun test- 运行测试bun run build- 构建(使用 tsdown)bun run lint- ESLint 检查bun run lint:fix- ESLint 自动修复bun run format- Prettier 格式化bun run type-check- TypeScript 类型检查