Monster.Data. Transformer

The transformer class is a swiss army knife for manipulating values. especially in combination with the pipe, processing chains can be built up.

A simple example is the conversion of all characters to lowercase. for this purpose the command tolower must be used.

let t = new Transformer('tolower').run('ABC'); // ↦ abc

all commands

in the following table all commands, parameters and existing aliases are described.

commandparameteraliasdescription
to-base64base64, btobConverts the value to base64
from-base64atobConverts the value from base64
callfunction: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
defaultvalue: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.0)
debugthe passed value is output (console) and returned
emptyReturn empty String ""
first-keydefaultCan be applied to objects and returns the value of the first key. All keys of the object are fetched and sorted. (since 1.23.0)
fromjsonType conversion from a JSON string (since 1.12.0)
ifstatement1: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.
indexkey:defaultproperty, keyFetches a value from an object, an array, a map or a set
last-keydefaultCan be applied to objects and returns the value of the last key. All keys of the object are fetched and sorted. (since 1.23.0)
lengthcountLength of the string or entries of an array or object
nopDo nothing
nth-keyindex:defaultCan be applied to objects and returns the value of the nth key. All keys of the object are fetched and sorted. (since 1.23.0)
nth-last-keyindex:defaultCan 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.0)
pathpathThe access to an object is done via a Pathfinder object
path-existspathCheck if the specified path is available in the value (since 1.24.0)
plaintextplainAll HTML tags are removed (*)
prefixtextAdds a prefix
rawurlencodeURL coding
staticnoneThe Arguments value is used and passed to the value. Special characters \and : can be quotet by a preceding .
substringstart:lengthReturns a substring
suffixtextAdds a suffix
tointegerType conversion to an integer value
tojsonType conversion to a JSON string (since 1.8.0)
tolowerstrtolower, tolowercaseThe input value is converted to lowercase letters
tostringType conversion to a string.
toupperstrtoupper, touppercaseThe input value is converted to uppercase letters
trimRemove spaces at the beginning and end
ucfirstFirst character large
ucwordsAny word beginning large
undefinedReturn undefined
uniqidCreates a string with a unique value (**)

(*) 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();

Constructor

new Transformer(definition)

Parameters:
NameTypeDescription
definitionstring
Since
  • 1.5.0
License
  • AGPLv3
Example
import {Transformer} from '@schukai/monster/source/data/transformer.mjs';

const transformer = new Transformer("tolower")

console.log(transformer.run("HELLO"))
// ↦ hello

console.log(transformer.run("WORLD"))
// ↦ world

Methods

run(value) → {*}

Parameters:
NameTypeDescription
value*
Throws:
  • unknown command

    Type
    Error
  • unsupported type

    Type
    TypeError
  • type not supported

    Type
    Error
Returns:
Type: 
*

setCallback(name, callback, context) → {Transformer}

Parameters:
NameTypeDescription
namestring
callbackfunction
contextobject
Throws:
  • value is not a string

    Type
    TypeError
  • value is not a function

    Type
    TypeError
Returns:
Type: 
Transformer