Safe expressions for rules, filters, and templates.

Bonsai gives you a constrained expression language for pricing rules, search filters, workflow conditions, and user-authored logic. Replace fragile eval()-style glue with typed errors, cacheable compilation, and real sandbox controls.

$ bun add bonsai-js
// Transform pipelines
" hello world " |> trim |> upper
// → "HELLO WORLD"

// Null-safe navigation
user?.profile?.avatar ?? "default.png"
// → "default.png"

// Lambda predicates
users |> filter(.age >= 18) |> map(.name)
// → ["Alice"]

Use It Where eval() Would Be Reckless

Bonsai is strongest when expressions come from config, admin tooling, product builders, or end users and still need to run safely in production.

Pricing & Eligibility

Business Rules

A pricing rule changes every quarter. Right now that's a ticket, a PR, and a deploy. With Bonsai it's a text field in your admin panel that evaluates at runtime.

order.total >= freeShippingThreshold && customer.tier == "gold"
Search & Admin UIs

Filter Builders

Users want saved views like "active this week" or "high-value pending." Store the condition as a string, evaluate it per row — no custom query parser needed.

orders |> filter(.status == "pending" && .total > 500)
Notifications & Emails

Templates

Your notification service pulls a template from the database and fills it with user data. The expressions live alongside the copy, not buried in application code.

`Hi ${user.firstName}, your order ${order.id} ships ${shipDate |> formatDate}`
Platforms & Builders

User-Authored Logic

You're building a form builder, workflow tool, or internal platform where users configure conditions. They need expression power — you need sandbox controls.

field.country == "US" ? "required" : "hidden"

What You Get

Not just syntax. Bonsai is designed to be usable in hot paths, safe around untrusted expressions, and boring to ship.

🛡

Safe Sandbox

Blocks prototype access, enforces depth and timeout limits. Whitelist or blacklist properties for full control.

💨

Pipe Operator

Chain transforms with |> for readable data pipelines. Compose trim, upper, filter, map, and more.

True Async

Awaits async transforms and functions natively. Mix sync and async operations in a single expression.

🧩

Plugin System

Extend with custom transforms and functions. Modular stdlib included for strings, arrays, math, and more.

📦

Zero Dependencies

Lightweight, ESM-only, TypeScript-first. No runtime dependencies - just add and go.

Blazing Fast

Compile once, cache the AST, and evaluate repeatedly in microseconds with benchmark-gated performance checks.

Built for Production, Not Just Demos

If your alternative is eval(), handwritten condition trees, or a growing pile of special-case parsers, Bonsai gives you a narrower and more operationally sane surface.

Safer Than Raw JavaScript

Expressions are parsed as data, not executed as host code. You get property controls, blocked prototype access, depth limits, and explicit safety options.

Faster Than Ad-hoc Glue

Compile once, cache the AST, and evaluate repeatedly in microseconds. Use compile() for request handlers, rules engines, and hot loops.

More Trustworthy to Ship

The package is zero-dependency, ESM-only, typed, smoke-tested as a packed artifact, benchmark-gated, and documented with a SemVer stability boundary.

Try a Real Use Case

Start from a preset, tweak the expression, then open the full playground with the same context.

Open in Playground
"HELLO WORLD"
Context
{
  "name": "  hello world  "
}

Documentation

Everything you need to build with Bonsai, from a five-minute quick start to safety guidance, performance notes, and transform/function extension patterns.