Transformer
The Transformer
class is used to transform data. The transformation is defined as a string.
import {Transformer} from '@schukai/monster/source/data/transformer.mjs';
const transformer = new Transformer("tolower")
console.log(transformer.run("HELLO"))
// ↦ hello
The following transformations are available:
command | parameter | alias | description |
---|---|---|---|
call | function:param1:param2:… | Calling a callback function. The function can be defined in three places: either globally, in the context addCallback or in the passed object | |
concat | path or static:path or static:… | This function concatenates the values of the passed paths. If no value in the path is found, the path is used as a static value. | |
contains | Checks if an array or string contains a value (since 3.17) | ||
currency | maximumFractionDigits:roundingIncrement | money | Convert a currency and amount to a local string (since 3.17) |
date | Convert a date string to a localized date string (since 3.16) | ||
datetime | Convert a date string to a localized date-time string (since 3.17) | ||
day | Returns the day of a date (since 3.16) | ||
debug | The passed value is output (console) and returned | ||
default | value:type | ?? | If the value is undefined the first argument is returned, otherwise the value. The third optional parameter specifies the desired type. If no type is specified, string is used. Valid types are bool, string, int, float, undefined and object. An object default value must be specified as a base64 encoded json string. (since 1.12) |
element-by-id | plain | All HTML tags are removed (*) | |
empty | Return empty String “” | ||
equals | value | Compare the comparison value with the specified value and return true if they match. | |
first-key | default | Can be applied to objects and returns the value of the first key. All keys of the object are fetched and sorted. (since 1.23) | |
from-base64 | atob | Converts the value from base64 | |
fromjson | Type conversion from a JSON string (since 1.12) | ||
has-entries | hasentries | Checks whether an array or an object has elements. (since 3.17) | |
hours | hour | Returns the hours of a date (since 3.16) | |
if | statement1:statement2 | ? | Is the ternary operator, the first parameter is the valid statement, the second is the false part. To use the current value in the queue, you can set the value keyword. On the other hand, if you want to have the static string “value”, you have to put one backslash \ in front of it and write value. the follow values are true: ‘on’, true, ‘true’. If you want to have a space, you also have to write \ in front of the space. |
index | key:default | property, key | Fetches a value from an object, an array, a map or a set |
is-array | isarray | Checks if the value is a array (since 3.17) | |
is-float | isfloat | Checks if the value is a float (since 3.17) | |
is-null | isnull | Checks if the value is null (since 3.17) | |
is-number | isnumber | Checks if the value is a number (since 3.17) | |
is-object | isobject | Checks if the value is a object (since 3.17) | |
is-set | isset | Checks if the value is undefined and not null (since 3.17) | |
is-undefined | isundefined | Checks if the value is undefined (since 3.17) | |
last-key | default | Can be applied to objects and returns the value of the last key. All keys of the object are fetched and sorted. (since 1.23) | |
length | count | Length of the string or entries of an array or object | |
map | key:value:key:value…. | Map a value with an existing mapping (since 3.16) | |
minutes | minute | Returns the minutes of a date (since 3.16) | |
month | Returns the month of a date (since 3.16) | ||
nop | Do nothing | ||
not | Converts a bolian value into the opposite one (since 3.17) | ||
nth-key | index:default | Can be applied to objects and returns the value of the nth key. All keys of the object are fetched and sorted. (since 1.23) | |
nth-last-key | index:default | Can be applied to objects and returns the value of the nth key from behind. All keys of the object are fetched and sorted. (since 1.23) | |
path-exists | path | Check if the specified path is available in the value (since 1.24) | |
path | path | The access to an object is done via a Pathfinder object | |
plaintext | plain | All HTML tags are removed (*) | |
prefix | text | Adds a prefix | |
rawurlencode | URL coding | ||
seconds | second | Returns the seconds of a date (since 3.16) | |
static | none | The Arguments value is used and passed to the value. Special characters \ | |
substring | start:length | Returns a substring | |
suffix | text | Adds a suffix | |
time | Convert a date string to a localized time string (since 3.16) | ||
timestamp | Convert a date string to a time stamp (since 3.16) | ||
to-base64 | base64, btob | Converts the value to base64 | |
tointeger | Type conversion to an integer value | ||
tojson | Type conversion to a JSON string (since 1.8) | ||
tolower | strtolower, tolowercase | The input value is converted to lowercase letters | |
tostring | Type conversion to a string. | ||
toupper | strtoupper, touppercase | The input value is converted to uppercase letters | |
translation | key:default | i18n | Translations can be applied. The translations must be stored in the DOM. (*) To use the current value as key the first parameter must be set to undefined. |
trim | Remove spaces at the beginning and end | ||
ucfirst | First character large | ||
ucwords | Any word beginning large | ||
undefined | Return undefined | ||
uniqid | Creates a string with a unique value (**) | ||
weekday | Returns the day of the week of a date (since 3.16) | ||
year | Convert a date string to a year (since 3.16) |
(*) for this functionality the extension jsdom must be loaded in the nodejs context.
// polyfill
if (typeof window !== "object") {
const {window} = new JSDOM('', {
url: 'http://example.com/',
pretendToBeVisual: true
});
[
'self',
'document',
'Node',
'Element',
'HTMLElement',
'DocumentFragment',
'DOMParser',
'XMLSerializer',
'NodeFilter',
'InputEvent',
'CustomEvent'
].forEach(key => (global[key] = window[key]));
}
(**) for this command the crypt library is necessary in the nodejs context.
import * as Crypto from "@peculiar/webcrypto";
global['crypto'] = new Crypto.Crypto();
With the method setCallback(name, callback, context)
you can define your own transformation functions.
The context parameter is optional and can be used to define the context in which the callback function is called.
If no context is specified, the global context is used. In the pipe the name of the callback function is used by
prefixing it with a call:
.
import {Transformer} from '@schukai/monster/source/data/transformer.mjs';
const transformer = new Transformer("call:myCallback");
transformer.setCallback("myCallback", function (value) {
return value + " world";
});
console.log(transformer.run("hello")); // hello world