Formatter

Monster.Text. Formatter

Messages can be formatted with the formatter. To do this, an object with the values must be passed to the formatter. The message can then contain placeholders.

Look at the example below. The placeholders use the logic of Pipe.

You can create an instance via the monster namespace new Monster.Text.Formatter().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/monster.js';
new Monster.Text.Formatter()
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {Formatter} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/text/formatter.js';
new Formatter()
</script>

Marker in marker

Markers can be nested. Here, the inner marker is resolved first ${subkey} ↦ 1 = ${mykey2} and then the outer marker ${mykey2}.

const text = '${mykey${subkey}}';
let obj = {
 mykey2: "1",
 subkey: "2"
};

new Formatter(obj).format(text);
// ↦ 1

Callbacks

The values in a formatter can be adjusted via the commands of the Transformer or thePipe. There is also the possibility to use callbacks.

const formatter = new Formatter({x: '1'}, { callbacks: { quote: (value) => { return '"' + value + '"' } } });

formatter.format('${x | call:quote}')) // ↦ "1"

Marker with parameter

A string can also bring its own values. These must then be separated from the key by a separator ::. The values themselves must be specified in key/value pairs. The key must be separated from the value by a separator =.

When using a pipe, you must pay attention to the separators.

Constructor

# new Formatter(object)

Default values for the markers are ${ and }

Parameters:
Name Type Description
object object
Since:
  • 1.12.0
Throws:

value is not a object

Type
TypeError
Example
import {Formatter} from 'https://cdn.jsdelivr.net/npm/@schukai/[email protected]/dist/modules/text/formatter.js';

new Formatter({
      a: {
          b: {
              c: "Hello"
          },
          d: "world",
      }
  }).format("${a.b.c} ${a.d | ucfirst}!"); // with pipe

// ↦ Hello World!

Members

# defaults

Properties
Name Type Default Description
marker object
Properties
Name Type Default Description
open array ["${"
close array ["${"
parameter object
Properties
Name Type Default Description
delimiter string "::"
assignment string "="
callbacks object {}

Methods

# format(text) → {string}

Parameters:
Name Type Description
text string
Throws:
  • value is not a string

    Type
    TypeError
  • too deep nesting

    Type
    Error
Returns:
Type
string

# setMarker(open, close) → {Formatter}

Set new Marker

Default values for the markers are ${ and }

formatter.setMarker('#'); // open and close are both #
formatter.setMarker('[',']');
formatter.setMarker('i18n{','}');
Parameters:
Name Type Description
open array | string
close array | string | undefined
Since:
  • 1.12.0
Throws:

value is not a string

Type
TypeError
Returns:
Type
Formatter

# setParameterChars(delimiter, assignment) → {Formatter}

Set new Parameter Character

Default values for the chars are :: and =

formatter.setParameterChars('#');
formatter.setParameterChars('[',']');
formatter.setParameterChars('i18n{','}');
Parameters:
Name Type Description
delimiter string
assignment string
Since:
  • 1.24.0
Throws:

value is not a string

Type
TypeError
Returns:
Type
Formatter