Domma Reactive User Manual

Expressions and Content Security Policy

Updated by Darryl Waterhouse on 17 October 2026 4 min read

Bindings need more than a dotted path, so the package ships a small expression language - parsed by hand, never by the Function constructor.

import {compileExpression, registerHelper} from 'domma-reactive';

registerHelper('upper', s => String(s).toUpperCase());

const evaluate = compileExpression("count > 0 ? upper(label) : 'none'");

evaluate({count: 3, label: 'items'});   // 'ITEMS'
evaluate({count: 0, label: 'items'});   // 'none'

compileExpression parses once and returns a function; call that per update. It returns null if the source does not parse, so a caller can skip the binding rather than render a lie.

This is the engine every binding runs on: data-bind-*, data-model, data-if, data-on-*, non-path {{ }} interpolations and the default renderer all evaluate through it.

expressionDependencies is what lets you wire one effect per binding without guessing:

expressionDependencies("label === 'name'");   // Set { 'label' } - not 'name'
expressionDependencies('user.profile.email'); // Set { 'user' }  - root names only
expressionDependencies('$parent.name');       // Set {} - position, not state

What it supports

Category Forms
Paths a, a.b.c, a[0], a[key], a['x']
Literals 'str', "str", 1, 1.5, 1e3, true, false, null
Objects {a: x, 'b-c': y} - name or string keys
Arrays [a, b, 'c'], and [a, b][i]
Arithmetic + - * / % (+ also concatenates)
Comparison === !== < <= > >=
Logical && || ! - short-circuiting
Ternary a ? b : c
Unary - + !
Calls helper(arg, …) - built-in or registered helpers only
Context $data, $root, $parent, $parents, $parentContext, $index, $length

Precedence and associativity are JavaScript's. 1 + 2 * 3 is 7; 10 - 3 - 2 is 5. Nesting is capped at 64 levels.

Object and array literals build a fresh value on every evaluation, so a binding never shares one object between renders. They are deliberately plain: no computed keys ({[k]: 1}), no shorthand ({a}), no spread, no methods, no holes ([1,,2]) and no trailing commas - each refused with a message saying so - and __proto__, constructor and prototype are refused as keys, so a literal is no way round the read guard. Use them in attribute bindings and helper calls (data-bind-style="{color: tone}", {{fmt(price, {currency: 'GBP'})}} inside an attribute). Inside {{ }} text a closing }} ends the interpolation, so keep literals with braces out of mustache text.

Built-in helpers

Expressions cannot call methods, so these ship ready to call - no registerHelper needed:

Helper Returns
len(list) length of an array or string, size of a Set or Map, 0 for anything else (including null)
includes(list, value) whether the list holds value (===), or a string contains it
some(list, key[, value]) whether any item's key is truthy - or === value, when given
every(list, key[, value]) whether every item matches; true for an empty list
count(list[, key[, value]]) how many items match; with no key, how many items
where(list, key[, value]) the matching items, as a new array
sum(list[, key]) total of the items, or of each item's key; non-numbers count as 0
pluck(list, key) each item's key, as a new array
sortBy(list[, key[, 'desc']]) a sorted copy; strings by localeCompare, missing values last
first(list) / last(list) an end item, or undefined
join(list[, separator]) a string, ', ' between items by default

key is a property name or a dotted path ('meta.tag'); pass null to test the items themselves (where(xs, null, 2)). The list may be an observable or observableArray, and a field may be an observable - both are read for you, and the reads are tracked, so a binding re-runs when the list changes or when one row's field does:

<p data-if="!len(todos)">Nothing to do.</p>
<span data-bind-text="count(todos, 'done') + ' of ' + len(todos)"></span>
<ul data-each="where(todos, 'done', false) key=id"><li data-bind-text="title"></li></ul>
<span data-bind-text="join(pluck(tags, 'name'))"></span>

None of them mutates its input. A helper you register under the same name takes precedence; unregistering it brings the built-in back.

What it does not support, and will not

Assignment. new. Member calls - user.toUpperCase() does not work, and neither does alert(1); the only callable things are helpers - built-in or registered. (data-on-* is the single exception, and only because an event fires outside every effect - see data-on-<event>.) Loose equality (==), nullish coalescing (??), regular expressions, template literals, comma sequences. Reads of __proto__, constructor and prototype, in any form - including a[key] where key holds '__proto__' at runtime.

Most of those are recognised specifically so they can be refused with a message that says what to do instead. Anything more complicated than the grammar above belongs in a computed, not in a template.

Failure is never fatal

A malformed expression logs one warning naming the source (and the template, if you passed {template: 'user-card'}) and yields null from parseExpression / undefined from evaluateExpression. An evaluation error - a helper that threw, a nesting depth beyond 64 - does the same. Nothing in this module throws on expression input, so one bad binding cannot blank a page.

The exception is registerHelper, which throws a TypeError on a bad name or a non-function. That is a bug in your code, not input, and it should be loud.

It runs under a strict Content Security Policy

There is no eval and no Function constructor anywhere in the package - asserted against the source in the unit suite and against all three built bundles in npm run test:dist. Bindings therefore work under script-src 'self' without unsafe-eval.