Formatter
No summary available
import { Formatter } from "@schukai/monster/source/text/formatter.mjs";Introduction
The Formatter replaces markers inside strings with values from a data object. It supports nested markers, parameter blocks, and the full pipe syntax for transformations.
Basic Usage
import { Formatter } from "@schukai/monster/source/text/formatter.mjs";
const formatter = new Formatter({
title: "Hello",
name: "World"
});
formatter.format("${title} ${name}!");
Pipes and Callbacks
You can use pipe commands to transform values. Custom callbacks can be injected through the callbacks option.
const formatter = new Formatter({ value: "1" }, {
callbacks: {
quote: (v) => `"${v}"`
}
});
formatter.format("${value | call:quote}");
Marker Parameters
A marker can carry its own parameter list using :: and = separators. Parameters are forwarded to pipe commands.
formatter.format("${price::currency=EUR | money}");
/** this example does not use an extra script **/Exported
FormatterDerived from
BaseWithOptionsOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the BaseWithOptions.
Properties
The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the BaseWithOptions.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the BaseWithOptions.
Constructor
constructor(object,options)object: objectoptions: options
${ and }Structural methods
setMarker(open,close)1.12.0open{array|string}: openclose{array|string|undefined}: close
- {Formatter}
{TypeError}value is not a string
${ and } ``formatter.setMarker('#'); // open and close are both # formatter.setMarker('[',']'); formatter.setMarker('i18n{','}');``setParameterChars(delimiter,assignment)1.24.0delimiter{string}: delimiterassignment{string}: assignment
- {Formatter}
{TypeError}value is not a string
:: and = ``formatter.setParameterChars('#'); formatter.setParameterChars('[',']'); formatter.setParameterChars('i18n{','}');``Other methods
format(text)text{string}: text
- {string}
{TypeError}value is not a string{Error}too deep nesting
Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.