Transformers
Transformers are used to transform a value based on the tag at runtime. You can use builtin transformers or write your own transformer.
import { Interpreter, StringTransformer, StrictVarsParser } from 'tagscript';
const ts = new Interpreter(new StrictVarsParser());
await ts.run('Hi {user}', { user: new StringTransformer(args) });
// Hi Parbez
Builtin Transformers
Following is the list of builtin transformers:
Custom Transformer
You can write your own transformer by implementing ITransformer interface.
import type { ITransformer, Lexer } from 'tagscript';
/**
* Integer transformer transforms an integer based on the given parameters.
*
* If no parameters are given, the integer will be returned as is.
*
* if `++` parameter is given, the integer will be incremented.
* if `--` parameter is given, the integer will be decremented.
*/
export class IntegerTransformer implements ITransformer {
private integer: number;
public constructor(int: `${bigint | number}`) {
this.integer = Number.parseInt(int, 10);
}
public transform(tag: Lexer) {
if (tag.parameter === '++') {
return `${++this.integer}`;
}
if (tag.parameter === '--') {
return `${--this.integer}`;
}
return `${this.integer}`;
}
}
Last updated on December 11, 2022