The transformer class is a swiss army knife for manipulating values. especially in combination with the pipe, processing chains can be built up.
You can call the method via the monster namespace new Monster.Data.Transformer()
.
<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.23.0/dist/modules/data/transformer.js';
console.log(new Monster.Data.Transformer())
</script>
Alternatively, you can also integrate this function individually.
<script type="module">
import {Transformer} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.23.0/dist/modules/data/transformer.js';
console.log(new Transformer())
</script>
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.
command |
parameter |
alias |
description |
base64 |
|
|
Converts the value to base64 |
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 |
default |
|
|
If the value is undefined the first argument is returned, otherwise the value. (since 1.12.0) |
empty |
|
|
Return empty String "" |
fromjson |
|
|
Type conversion from a JSON string (since 1.12.0) |
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' |
index |
key:default |
property, key |
Fetches a value from an object, an array, a map or a set |
length |
|
count |
Length of the string or entries of an array or object |
nop |
|
|
Do nothing |
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 |
static |
|
none |
The Arguments value is used and passed to the value. Special characters \ and : can be quotet by a preceding . |
substring |
start:length |
|
Returns a substring |
suffix |
text |
|
Adds a suffix |
tointeger |
|
|
Type conversion to an integer value |
tojson |
|
|
Type conversion to a JSON string (since 1.8.0) |
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 |
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 (**) |
(*) 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();