Skip to content

Build Elements / spark

spark ​

js
import spark, { equals, gt, len, not, prop, truthy } from '@t2e1/kuba/spark'

The operator registry behind Echo arc filters. Each operator is a pure function of (data, value) that transforms a payload on its way from a publisher to a sink.

html
<kb-text on="user/changed:setter/textContent|prop=email"></kb-text>

The registry ​

Name(data, value) returns
propdata[value] — the named property of the payload
equalswhether data equals value
differentwhether data differs from value
notthe negation of data
truthywhether data is truthy
lenthe length of data
add / subtractdata plus / minus value
inc / decdata incremented / decremented
gt / gtewhether data is greater than / at least value
lt / ltewhether data is less than / at most value
alwaysvalue, ignoring data

Each is also importable directly, for use outside an arc.

spark.get(name) ​

Resolves an operator by name.

!> An unknown name resolves to the identity function, not an error. That's what makes a typo in an arc filter silent: the payload passes through untouched and nothing reports it.

spark.set(name, fn) ​

Registers an operator, or replaces one. Returns spark, so registrations chain.

js
import spark from '@t2e1/kuba/spark'

spark
  .set('uppercase', (value) => String(value).toUpperCase())
  .set('slice', (value, size) => String(value).slice(0, Number(size)))
html
<kb-text on="user/changed:setter/textContent|prop=name|uppercase"></kb-text>

Register before the arcs that use it connect — an element upgrading earlier resolves the name to identity and keeps that resolution for that arc.

What can't be an operator ​

Operators are synchronous transforms of a value. They receive a payload and return a payload; they can't defer, drop or batch the sink call.

That rules out a whole category people reach for first:

  • debounce / throttle — would need to delay the call. Throttle inside the sink method instead.
  • Anything async — a promise would be passed along as the payload.
  • Conditional routing — an operator can return false, but the sink is still invoked with it. There is no "don't call".