Skip to content

Javascript workflow operator

Introduction

A Javascript operator block takes any input JSON and can produce any output JSON using JavaScript code.
For this reason, it is typically used to post-process the output of the last block of the workflow to get output that meets the needs of the final workflow user, but it can also be used in intermediate steps to alter the JSON that gets passed from one block to the next.

Block properties

General properties

The general properties of a Javascript block are:

  • The unique block ID.
  • Block name.
  • Operator version

Only the block name can be changed.

Type specific properties

The type specific property of a Javascript block is its JavaScript code. The properties dialog provides a code editor filled with a preset script.

Platform NL Flow Javascript conforms to the Standard ECMAScript 5.1 Language Specification.

You have also to consider that:

  • It does not support import and export of libraries or module.
  • The interpreter that takes care of interpreting the code written in the hexagon behaves in a very similar way to a script tag inside an HTML page.
  • It is possible to access the DOM if necessary, but there are no peculiar characteristics used in other contexts such as the use of imports.
  • Validation is also done on ECMAScript 5.1 standards

The preset script is:

let input = {};

let output = {};

function process(input){
  //here your code
  return input; 
};

The main function has to be named process. Its only parameter, input, is automatically associated at runtime to the global variable with the same name, which, at runtime represents the block input or a portion of it.
The function return value is validated with the output global variable, it must have the same structure.

For example, this code transforms the typical categorization output of a model it in a simple array of labels:

let input = { document: { categories: [] } };

let output = { categories: [] };

function process(input) {
    let ret = [];
    if (input.document && input.document.categories) { 
        for (let cat of input.document.categories)
        ret.push(cat.label);
    }

    return { categories: ret };
}

In the code above, global variable input is defined to match only one part of the input object, namely the categories array of the document object.
The process function iterates through the categories in input and for each of them it considers only the label property, which it pushes to an anonymous array.
Global variable output is an object containing a categories array: with the return statement the function sets and returns an object with the same structure.

Inventory

The Inventory panel is displayed on the left of the properties window if the block is linked—directly or indirectly—to previous blocks of the workflow and shows the hierarchical structure of the JSON objects returned by those blocks.

It serves as a guide to understanding which input elements are available, so to use them to produce the block's output.

To expand and collapse the Inventory panel, use the buttons at its top.
To expand and collapse items inside the panel, use the arrow buttons to the left of them.

Code validation

To validate the code, select the play button in the right upper corner of the code editor.

Code reset

To reset the script select the grid button in the right upper corner of the code editor.