Tagscript

IntegerTransformer

Expose a number that a template can step up or down.

IntegerTransformer holds a whole number. The parameter ++ increments it and -- decrements it, and the change sticks for the rest of that render.

For template authors

Syntax

{name}
{name(++)}
{name(--)}

Examples

Given n starts at 5:

{n} {n(++)} {n(++)} {n(--)}
# 5 6 7 6

Each ++ moves the counter and renders the new value. Reading {n} on its own leaves it where it is.

For developers

Seeding one

import { IntegerTransformer, Interpreter, StrictVarsParser } from 'tagscript';

const ts = new Interpreter(new StrictVarsParser());

const response = await ts.run('{count(++)}', { count: new IntegerTransformer('5') });

The constructor takes a numeric string, typed as `${bigint | number}`, and runs it through parseInt with radix 10.

Behaviour

The counter lives on the transformer instance, not on the response. Reusing one instance across two run calls carries the count over, which is almost never what you want. Build a fresh one per render.

Any parameter other than ++ or -- renders the current value unchanged.

API reference

IntegerTransformer

Last updated on

On this page

Edit on Github