Expressions#
Expressions are short formulas, like a spreadsheet's: they read values and compute a new one, and they never change anything. The syntax is close to JavaScript's, but several JavaScript habits are errors here (see "Habits that break" below).
Where the = goes#
There are two kinds of places, and the = is the difference:
| Place | How to write it | Fields |
|---|---|---|
| Always an expression | No =: "derive": "state.goal - state.saved" |
derive, allow, where, becomes, a node's or a section's when, an if (rule, step or transition), a require entry's that, a step's each, a node's each.in, a source's when |
| A value that may hold expressions | = before an expression, anything else is literal: "value": "=state.goal", "label": "Save" |
props, a view's title, a section's label, to, value, with, return, initial, message, once, orElse, config |
In a value, objects and lists are walked, so { "id": "=item.id", "status": "read" } mixes an expression and a literal. A literal text that must start with = is written with two: "==x" is the text that starts with one =.
An input field's default is in neither row: it is a plain value, never an expression.
JSON
"=env.today"
For "today when left out", let the field be null and read it in the step with default:
JSON
[
{ "op": "add", "path": "/actions/addBook/input/addedOn", "value": { "type": "date?", "description": "The day it went on the list; today when left out.", "default": null } },
{ "op": "replace", "path": "/actions/addBook/steps/1/value/addedOn", "value": "=default(input.addedOn, env.today)" }
]
Values#
There are six kinds of value: null, booleans, numbers, strings, lists and maps (JSON objects). There is one number kind; an integer is a number with no fraction. Dates, datetimes and durations are strings ("2026-11-13"). Strings are written in single or double quotes; inside a JSON string, single quotes are easier: "=state.city == 'Lisbon'".
A template is written in backticks, with ${...} around each expression: "=`${state.today} of ${state.goal} glasses`". A template turns any value into text; it is the way to join text and numbers.
Operators#
| Expression | Gives |
|---|---|
state.toRead + state.finished, -state.toRead |
Arithmetic: +, -, *, /, %, and - before a number. / is exact: 7 / 2 is 3.5. |
state.toRead == 0 && state.finished > 0 |
Comparison and logic: ==, !=, <, <=, >, >=, &&, \|\|, !. |
state.toRead > 0 ? 'Keep reading' : 'All done' |
Choose one of two values. |
'read' in ['read', 'reading'] |
Whether a list holds a value, or a map a key. |
state.books[0].title |
A list element (from 0) and a member. |
{id: 'a', count: state.toRead} |
A map; a bare key is the key's name. |
{toRead: 'To read', reading: 'Reading', read: 'Read'}[state.books[0].status] |
A map read by a key: a stored code in words. A key it lacks fails, so wrap it in default. |
`${state.toRead} to read` |
A template. |
== compares whole values, lists and maps included, and never fails. < and its kind compare two numbers or two strings, nothing else: compare a field that may be null with null first. A date compares in time order with a date, so a record's date compares with env.today as it is (state.books.filter(b, b.addedOn < env.today)); against a datetime it compares as text, so take the datetime's date with toDate first. Each of these is true:
EXPR
'2026-11-20' < '2026-11-24'
{main: 'Main', side: 'Side'}['main'] == 'Main'
default({main: 'Main'}['dessert'], 'dessert') == 'dessert'
What an expression can read#
Each place sees certain names (roots). The ones you use most:
| Root | Holds | Where |
|---|---|---|
state |
Every state entry. | Almost everywhere; not in a stored entry's initial. |
sources |
The status of each sourced entry. | Where state is, but in no initial. |
grants |
For each grant request, held and declined. |
As sources. |
user |
Who is acting: id, name, kind, roles, person. |
Where state is. |
env |
today, now, timezone, locale, currency, online. |
Everywhere. |
input |
The action's input. | An action's allow, require and steps. |
event |
What happened: a control event's payload, a rule's trigger. | Handlers, rules and flow transitions. |
view |
The view's input and view state. | A view's props, when, handlers and view state initial (input only). |
item, index |
The current element and its position. | A slot, a node's each, a step's each, where and with of remove and patch. |
error |
code and message of a failed step. |
onError steps and orElse. |
A name that is not a root at that place is expression.unknownRoot. A stored entry's initial sees only env; a view state entry's initial sees env, state, user and the view's input; a grant request's where sees only input and env.
Functions#
Every function can be called as a function or as a method of its first argument: size(state.books) is state.books.size(). The exceptions are can, canSend, has, default and let, which are only written as calls, and the list macros, which are only written as methods.
| Expression | Gives |
|---|---|
size(state.books) |
Elements of a list, keys of a map, characters of a string. |
sum([1, 2, 3]), avg([1, 2]), min([3, 1]), max(state.toRead, 1) |
Totals and extremes; avg, min and max of an empty list are null. |
first(state.books), last(state.books) |
An end of a list, or null. |
slice(state.books, 0, 5), reverse(state.books) |
Part of a list (or string), and a list reversed. |
contains(['a', 'b'], 'a'), indexOf('hello', 'l') |
Whether a list or string holds a value, and where (-1 when not). |
distinct(['a', 'a', 'b']), flatten([[1], [2, 3]]) |
Without repeats; one level of lists joined. |
keys({b: 1, a: 2}), values({a: 1}), entries({a: 1}) |
A map's keys, values, or pairs as maps with key and value, in key order. |
merge({a: 1}, {b: 2}), compact({a: 1, b: null}) |
Two maps joined; a map without its null members. |
range(3), enumerate(state. |
[0, 1, 2]; each element as a map with index and value. |
trim(' x '), lower('A'), upper('a') |
Text cleaned up. |
split('a,b', ','), join(['a', 'b'], ', '), replace('a-b', '-', ' ') |
Text taken apart and put together. |
substring('hello', 0, 2), startsWith('hello', 'he'), endsWith('hello', 'lo') |
Parts of text. Text cannot be indexed with [0]. |
padStart('7', 2, '0'), matches('AB12', '^[A-Z]{2}[0-9]{2}$') |
Padding; a pattern test (no lookaround or backreferences). |
round(2.345, 2), floor(2.7), ceil(2.1), abs(-1) |
Rounding (round rounds halves away from zero). |
int('42'), number('4.5'), string(42), bool('true'), type(state.books) |
Conversions, and a value's kind as text. |
formatNumber(12.5, {style: 'currency'}) |
A number for people: style is decimal, percent, currency or compact, with digits. Money is in env.currency, the person's currency, or the option currency. |
`${state.toRead} ${plural(state.toRead, {one: 'book', other: 'books'})}` |
The right word for a count. |
The formatting functions give text for people to read. Never compare, parse or store what they return.
env.currency is null on a host that does not know the person's currency. Money is then shown with two decimals and no symbol, so the same expression works either way; do not name a currency the person did not name:
EXPR
formatNumber(12.5, {style: 'currency'}) == '12.50'
Dates and times#
A date is a string YYYY-MM-DD, a datetime a UTC string such as "2026-11-13T09:00:00Z", a duration a string such as "PT1H" (see state). There is no function for the present: read env.now (a datetime) and env.today (the date in the person's time zone).
| Expression | Gives |
|---|---|
addDays(env.today, 7), addMonths(env.today, 1) |
A date some days or months later (a negative count goes back). On a datetime, addDays adds whole days of 24 hours. |
addDuration(env.now, 'PT1H') |
A datetime a duration later: one hour from now. It takes a datetime, not a date. |
daysBetween(env.today, '2026-12-25'), durationBetween(env.now, '2026-12-25T00:00:00Z') |
Whole days from one date to another; seconds from one datetime to another. |
seconds('PT30M') |
A duration in seconds; one with years or months fails. |
datePart(env.today, 'weekday'), startOf(env.today, 'month'), toDate(env.now) |
A part (year, month, day, weekday from 1 for Monday, hour, minute, second); the start of a day, week, month or year; the date of a datetime. |
atTime(env.today, '09:00'), dateIn(env.now, 'Asia/Tokyo') |
The datetime at a time of day in the person's time zone; the date in a time zone. |
formatDate(env.today, 'long'), relativeTime(env. |
A date for people (short, medium, long, time); "3 days ago". |
Each of these is true:
EXPR
addDays('2026-11-24T09:00:00Z', 1) == '2026-11-25T09:00:00Z'
addDuration('2026-11-24T09:00:00Z', 'PT1H') == '2026-11-24T10:00:00Z'
daysBetween('2026-11-24', '2026-12-25') == 31
atTime('2026-11-24', '09:00', 'UTC') == '2026-11-24T09:00:00Z'
Dates in their usual form compare in time order with < and >. Apart from atTime and dateIn, these functions work in UTC. To act at a later time, schedule a wake (see rules).
List macros#
A macro walks a list, naming each element with a name you choose. In state.books.map(b, b.title) the list is state.books, the first argument names each element, and the body after it is worked out for each element.
| Expression | Gives |
|---|---|
state.books.map(b, b.title) |
A new list of the body for each element. |
state.books.filter(b, b.status == 'read') |
The elements for which the body is true. |
state.books.find(b, b.status == 'reading') |
The first such element, or null when there is none. |
state.books.exists(b, b.author == ''), state.books.all(b, b.title != '') |
Whether some, or every, element passes. |
state.books.count(b, b.status == 'toRead') |
How many pass. |
state.books.sortBy(b, b.title) |
Sorted by the key, smallest first; reverse it for largest first. |
state.books.groupBy(b, b.status), state.books.indexBy(b, b.id) |
A map from each key to its elements, or to one element. |
state.books.sumBy(b, size(b.title)), state.books.minBy(b, b.addedOn), state.books.maxBy(b, b.addedOn) |
A total; the element with the smallest or largest key. |
state.books.distinctBy(b, b.author) |
The first element for each key. |
find gives the element it found even when that is 0, false or empty text. An element that is itself null cannot be told from none found: test with exists. To count a run, such as days in a row, find where it breaks: with range and find, the first position that does not continue the run is its length, and default covers a run longer than the range. Each of these is true:
EXPR
[0, 1].find(n, n == 0) == 0
[true, false].find(b, !b) == false
['a', ''].find(s, s == '') == ''
[1, 2].find(n, n > 5) == null
-(2 + 3) == -5
let(done, ['2026-11-24', '2026-11-23', '2026-11-21'], default(range(366).find(i, !(addDays('2026-11-24', -i) in done)), 366)) == 2
The last is a streak: the days in a row, back from 2026-11-24, that are in the list.
Three more macros are written as calls:
| Expression | Gives |
|---|---|
default(first(state.books).title, 'No books') |
The first value, unless reading it fails for a missing member or gives null. |
has(state. |
Whether every step of the path is there. |
let(open, state.books.filter(b, b.status != 'read'), size(open) == 0 ? 'All read' : first(open).title) |
Names a value once, to use it in the body. |
Questions#
can is true when an action would accept that input now (its input checks, allow and require pass); canSend when a flow would take that event, for that element, now. Use them to show a button only to someone who can use it (see views):
EXPR
canSend('approval', 'approve', input.id)
can('askForTimeOff', {from: env.today, to: env.today})
The names are literals in quotes. Both are allowed in views and steps, never in a derive or a guard (expression.effectNotAllowed).
Habits that break#
No truthiness. Conditions must be booleans. !state.books and state.books && size(state.books) > 0 are errors; compare instead:
JSON
"state.books"
Write size(state.books) > 0, state.city != null, state.city != ''.
+ does not convert. + adds two numbers, joins two strings or joins two lists. Text and a number together is an error (expression.typeMismatch): write "=`To read: ${state.toRead}`", not "='To read: ' + state.toRead".
No methods of other languages. A list has no members: state.books.length is an error (expression.typeMismatch). Use size(state.books).
No functions of your own. For logic written once and read in several places, use a derived state entry when it takes no input, and a code module when it does (see code).
A missing member fails. Reading a member that is not there, or a member of null, fails with missing. An action's optional input field that the caller left out is missing, not null. Read such a value with default(input.author, '') or test it with has(input.author), or give the field a plain default in the action's input. A failure in a derive gives null; in a guard, false; in a step, it fails the action.
uuid() and random() only in steps. A value worked out again must come out the same, so these are allowed only in a step, such as a let step whose result you store:
JSON
"size(uuid())"
Patterns are escaped twice. A pattern in matches is a string inside a JSON string, so each backslash in it is written four times in the file. Classes such as [0-9] need no backslash, so prefer them.
Size#
Each expression has a fixed budget of work, far above ordinary ones. A filter inside a map over the same long list works on every pair of elements: build a lookup once with indexBy and let.