Streamlining Data Processing with the Pipe Class in MonsterJS.


The Pipe class in MonsterJS is a powerful tool for chaining data processing functions, streamlining multiple operations into a coherent workflow. This class is particularly useful when direct JavaScript embedding isn’t viable, such as in HTML contexts. Instead of sequentially calling functions in JavaScript, Pipe allows developers to define a series of transformations through a simple string. This approach is ideal for HTML attributes or similar situations where only string definitions make sense, offering a neat and efficient way to articulate complex data processing logic without cluttering the markup.

Key Features

  • Flexible Data Processing: Easily chain multiple functions together to process data in a streamlined and readable manner.
  • Pathfinder Integration: Utilizes the Pathfinder class to access nested data within objects, providing a straightforward way to retrieve and manipulate deep values.
  • Transformer Utilization: Leverages the Transformer class to apply various transformations to the data as it moves through the pipe.

How It Works

  1. Initialization: Create a Pipe instance by specifying a string that defines the sequence of operations, separated by the pipe symbol |.
  2. Execution: Call the run method on your Pipe instance, passing in the initial data. The data will be processed through the series of functions you’ve defined.

Example Usage

import { Pipe } from '@schukai/monster/source/data/pipe.mjs';

let data = {
    a: {
        b: {
            c: {
                d: "hello"

let pipe = new Pipe('path:a.b.c.d | toupper | prefix:Hello, ');
console.log(pipe.run(data)); // Outputs: "Hello, HELLO"

In this example, the Pipe instance first retrieves the value at the specified path within the object, converts it to uppercase, and then prefixes it with “Hello, “.

Methods and Properties

  • constructor(pipe): Initializes a new Pipe instance with the specified processing sequence.
  • setCallback(name, callback, context): Allows you to define custom callbacks within the pipe’s sequence.
  • run(value): Executes the pipe’s sequence of functions on the input value, returning the transformed result.