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

formulex

v0.0.59

Published

Ast sql/js converter

Readme

🧠 Formulex (Formula + Expression + Exec)

Formulex is a lightweight and extensible library that parses user-defined formulas into SQL expressions or executable JavaScript functions — with built-in AST support.

Perfect for low-code platforms, dashboards, calculated fields, and dynamic logic engines.

🚧 This project is a work in progress. Expect bugs and frequent updates.


🚀 Features

  • ✅ Convert formulas like {Field 1} + {Field 2} * 2 into SQL
  • ✅ Generate executable JavaScript functions from formulas
  • ✅ Parse formulas into abstract syntax trees (AST)
  • ✅ Support for custom field mappings and types
  • ✅ Zero runtime dependencies

📦 Installation

npm install formulex

📗 Usage

import { Parser } from 'formulex';

const fields = [
  { id: '1', name: 'Field 1', type: 'number' },
  { id: '2', name: 'Field 2', type: 'number' },
];

const expression = '{Field_1} + {Field_2} * 2';

const parser = new Parser(expression, fields);

const sql = parser.toSqlWithVariables()
// => (123 + (321 * 2))

const jsFormula = parser.toJs();
// => VARIABLES["1"] + (VARIABLES["2"] * 2)

const result = parser.runJs(jsFormula, { 1: 10, 2: 5 });
// => 20

🛠 API

new Parser(expression: string, fields?: IField[], fieldAttribute?: keyof IField) Creates a new parser instance.

expression: your input formula (e.g. {Field 1} + 10)

fields: optional array of fields (with id, name, type)

fieldAttribute: defines how variables are resolved (id, name, etc.)

parser.toSqlWithVariables(): string Converts the formula into a valid SQL expression string.

parser.toJs(): string Returns a JS-compatible string (to be evaluated with new Function).

parser.runJs(js: string, values: Record<string, unknown>): unknown Executes a previously generated JS string with given values.

parser.getAst(): StatementsNode Returns the abstract syntax tree (AST) of the formula.

parser.walkAst(visitor: (node: Node) => void): void Traverses all nodes of the AST in depth-first order and invokes the visitor callback on each node. Useful for custom validation, metadata extraction, or modifying behavior. Supported node types: Number, Variable, BinaryExpression, CallExpression, UnaryExpression, etc.

parser.getVariables(): string[] Returns all unique variable names used in the formula.

🧮 Supported Operators

| Type | Operators | Example | |--------------|------------------------------------|--------------------------------| | Arithmetic | +, -, *, /, % | {price} * {quantity} + 1 | | Comparison | ==, !=, >, <, >=, <= | {amount} > 100 | | Logical | AND, OR, NOT | active == true AND score > 5| | Grouping | Parentheses ( ) | (a + b) * c | | Variables | Dynamic keys from your data | user.age, order.total |

🧩 Use Cases

  • Dynamic calculated fields in dashboards or CRMs

  • Low-code formula engines

  • Report builders

  • Pricing rules and financial modeling

  • Serverless logic execution

📄 License

MIT