Skip to content

Arrays

Load the array stdlib when your expressions need collection work: counting, searching, projection, deduping, and simple aggregation.

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

Basic transforms

TransformExampleResult
countselectedRegions |> count3
firstplanTiers |> first"starter"
lastreleaseStages |> last"production"
reverseapprovalSteps |> reverse["legal", "finance", "ops"]
flattentagGroups |> flatten["billing", "finance", "ops"]
uniqueregions |> unique["gb", "us", "de"]
join(sep)invoiceIds |> join(", ")"INV-1, INV-2"
sortpriorityScores |> sort[1, 2, 3]

sort sorts numbers numerically and falls back to string comparison for other values.

Higher-order transforms

These accept lambda predicates as arguments for filtering, mapping, and searching.

TransformExampleResult
filter(pred)users |> filter(.age >= 18)Array of matching items
map(fn)users |> map(.name)Array of extracted values
find(pred)users |> find(.age >= 18)First matching item
some(pred)users |> some(.age >= 18)true if any match
every(pred)users |> every(.age >= 18)true if all match

Practical examples:

ts
users |> filter(.age >= 18) |> map(.name)
// get names of adult users
// ["Alice", "Charlie"]
ts
orders |> some(.total > 100)
// check if any order exceeds $100
// true
ts
products |> map(.category) |> unique |> sort
// deduplicate and sort categories
// ["electronics", "food", "toys"]

Most array errors are type mismatches: these transforms expect arrays. If the input might be nullable or mixed, normalize it before you reach the expression or guard it with your own custom transform.