Tagscript

StringTransformer

Expose a piece of text, whole or split into words.

StringTransformer holds text. Without a parameter the whole string renders. With one, the template picks out words or a run of words.

For template authors

Syntax

{name}
{name(index)}
{name(+index)}
{name(index+)}
{name(index):separator}

Indexes start at 1. The text splits on runs of whitespace unless the payload gives a different separator.

Examples

Given args holds Hi, How are you?:

{args}
# Hi, How are you?

{args(1)}
# Hi,

{args(2)}
# How

{args(2+)}
# How are you?

{args(+2)}
# Hi, How

Use + after the index for "this word onwards", and before it for "up to and including this word".

Pass a separator as the payload to split on something else. Given args holds a,b,c:

{args(2):,}
# b

An index past the end gives the whole string back rather than an empty result.

For developers

Seeding one

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

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

const response = await ts.run('Hi {args}, first word {args(1)}', {
	args: new StringTransformer('How are you?'),
});

A variable parser has to be registered for the tag to resolve.

Escaping

The second constructor argument escapes TagScript syntax in the value before it renders.

new StringTransformer(userInput, true);

With it on, a {, }, (, ), : or | in the value comes out with a backslash in front, so text a user typed cannot turn into syntax that a later tag reads. Turn it on for anything a person typed.

Escaping is off by default. Seeding raw user input without it means a value containing {stop(1==1)} is treated as a tag by any parser registered after the variable resolves.

API reference

StringTransformer

Last updated on

On this page

Edit on Github