Something went wrong on our end
Select Git revision
event-bus_test.go
-
Volker Schukai authoredVolker Schukai authored
transformer.mjs 17.87 KiB
/**
* Copyright schukai GmbH and contributors 2023. All Rights Reserved.
* Node module: @schukai/monster
* This file is licensed under the AGPLv3 License.
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
*/
import { getLocaleOfDocument } from "../dom/locale.mjs";
import { Base } from "../types/base.mjs";
import { getGlobal, getGlobalObject } from "../types/global.mjs";
import { ID } from "../types/id.mjs";
import { isArray, isObject, isString, isPrimitive } from "../types/is.mjs";
import {
getDocumentTranslations,
Translations,
} from "../i18n/translations.mjs";
import {
validateFunction,
validateInteger,
validateObject,
validatePrimitive,
validateString,
validateBoolean,
} from "../types/validate.mjs";
import { clone } from "../util/clone.mjs";
import { Pathfinder } from "./pathfinder.mjs";
export { 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
* ```
*
* @see {@link https://monsterjs.org/en/doc/#transformer|Monster Docs}
*
* @externalExample ../../example/data/transformer.mjs
* @license AGPLv3
* @since 1.5.0
* @copyright schukai GmbH
* @memberOf Monster.Data
*/
class Transformer extends Base {
/**
*
* @param {string} definition
*/
constructor(definition) {
super();
this.args = disassemble(definition);
this.command = this.args.shift();
this.callbacks = new Map();
}
/**
*
* @param {string} name
* @param {function} callback
* @param {object} context
* @returns {Transformer}
* @throws {TypeError} value is not a string
* @throws {TypeError} value is not a function
*/
setCallback(name, callback, context) {
validateString(name);
validateFunction(callback);