Skip to content

Math

Load the math stdlib for rounding, clamping, and summarizing numeric values in pipelines.

ts
import { math } from 'bonsai-js/stdlib'
expr.use(math)

Transforms

TransformExampleResult
roundaverageRating |> round4
floormonthlySeats |> floor3
ceilstorageUnits |> ceil4
absbalanceDelta |> abs42
sumorderTotals |> sum60
avgorderTotals |> avg20
clamp(min, max)requestedDiscount |> clamp(0, 25)25

Functions

FunctionExampleResult
min(a, b)min(order.total, budgetCap)smaller value
max(a, b)max(order.total, minimumBillable)larger value

Common patterns:

ts
products |> map(.price) |> avg |> round
// average price, rounded
// 25
ts
orders |> map(.total) |> sum
// sum all order totals
// 347.5

Keep the types clean: math transforms expect numbers, and sum/avg expect arrays of numbers. If your input is textual, convert it first with toNumber or a custom transform.