Custom modules

Modules

A module is a named library of reusable functions, types and values. You can create your own modules from the Hub and consume them in your applications.

Modules are only allowed to declared functions, types and values.

// This code may be placed in a module. We will call it MyModule.

// A function which reverses a text value.
function reverse(input: text) {
    let result = "";
    for c in input {
        set result = c & result;
    }
    return result;
}

// A value containing some placeholder text.
let placeholderText = "Lorem ipsum";

Given such a module, we can use it in a script step:

// In a script step in an application
// which refers to MyModule by dragging it onto the canvas:

return MyModule.reverse(MyModule.placeholderText); // Returns "muspi meroL";

Assertions

While developing a module, it is often useful to investigate the behavior of its functions before consuming them in an application. You can do this using assertions.

An assertion is a statement indicating what you expect to be true about a function. Similar to a unit test in other programming environments, an assertion can be checked while the code is being developed.

// In a module named "MyModule"

function reverse(input: text) {
    let result = "";
    for c in input {
        set result = c & result;
    }
    return result;
}

// A couple of assertions defining the behavior of the reverse function.
assert reverse("abc") == "cba";
assert reverse("a") == "a";
assert reverse("") == "";

To check the assertions in a module, press the Check Assertions button above the module editor. Assertions which evaluate to true are considered successful; assertions which evaluate to false (or raise a runtime error) are considered failed. The success or failure of each individual assertion will be indicated by a green check mark or a red cross, respectively, next to the line.

When applications are run, assert statements are ignored.

Making use of assertions is a highly recommended practice, as they help verify that the code in the modules retains its expected behavior as you continue to work on it.

Private

Functions and values in modules can be marked as private. A private function or value can be used by other functions and values inside the module but are not visible to applications that use the modules.

A private function or value is declared by adding the private keyword before the function or let keywords.

// Declare a private email validation regex pattern.
private let emailPattern = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";

// Declare a public email validation function
function isValidEmail(candidate: text) => regexMatch(candidate, emailPattern);

// Assertions are never visible from outside and do not need to be marked as private.
assert isValidEmail("info@novacura.com");
assert not isValidEmail("info.novacura.com");

FlowScript Playground

All modules that are open in the Designer are automatically made available in the FlowScript Playground. The Playground will use the current version of the module, regardless of whether it is saved.

Last updated