Skip to main content

Base

atob

Decodes a Base64-encoded string and returns the decoded string.

See also the MDN documentation for atob.

let normal_text = atob(base64);

Example:

let normalText = atob("aGVsbG8gd29ybGQ=");
log("normal Text: ", normalText); // => 'hello world'

btoa

Encodes a string to Base64 and returns the Base64-encoded string.

See also the MDN documentation for btoa.

let base_64 = btoa(normal_text);

Example:

let text = "hello world";
let base64 = btoa(text);
log("base64 Text: ", base64); // => 'aGVsbG8gd29ybGQ='

decodeHTML

Decodes HTML entities to their corresponding characters.

Returns the decoded string.

let result = decodeHTML(text);

See also:encodeHTML

Example :

let text = "Text with html symbols like <, &, or > inside";
let decoded = decodeHTML(text);
log("decoded Text: ", decoded);

encodeHTML

Encodes special characters for safe embedding in HTML.

Returns the HTML-encoded string.

let result = encodeHTML(text);

See also:decodeHTML

Example :

let text = "Text with html symbols like <, &, or > inside";
let encoded = encodeHTML(text);
log("encoded Text: ", encoded); // => 'Text with html symbols like &lt;, &amp;, or &gt; inside'

extend

Deeply merges source objects into a target object, recursively merging nested properties.

Returns the modified target object.

const result = extend(target_object, source1);
const result = extend(target_object, source1, source2);
const result = extend(target_object, source1, source2, source3);
const result = extend(target_object, source1, source2, source3, source4);

Example 1:

let ob_a = {
p1: "a",
};
let ob_b = {
p2: "b",
};
extend(ob_a, ob_b); // ob_a is now { p1: 'a', p2: 'b' }

Example 2: Deep merge (unlike Object.assign, nested objects are merged recursively by extend)

let ob_a = {
settings: { color: "red", size: 10 },
};
let ob_b = {
settings: { size: 20, visible: true },
};
extend(ob_a, ob_b); // ob_a is now { settings: { color: 'red', size: 20, visible: true } }

getAccountInfo

Retrieves a property value from an account configured in the "Accounts" tab of the profile.

Returns a string.

Valid info values are:

  • 'USERNAME' - Username of basic authentication
  • 'NAME' - name of the account
  • 'TYPE' - type of the account ('xurrent', 'http', etc)
  • 'URL' - URL of the account
  • 'SCRIPTUSERNAME' - the value of the 'Script accessible username / API key' field
  • 'SCRIPTPASSWORD' - the value of the 'Script accessible password / secret' field

If the info value is invalid for the account there will be an exception. For Xurrent the request for 'USERNAME' returns the api users email if already verified for token auth.

let info = getAccountInfo(account, info);

Example 1:

let user = getAccountInfo("datacenter", "USERNAME");
log(`The User used to access datacenter account is: '${user}'`);

Example 2:

get_info("wdc");

function get_info(account) {
let user = getAccountInfo(account, "USERNAME");
let name = getAccountInfo(account, "NAME");
let type = getAccountInfo(account, "TYPE");
let url = getAccountInfo(account, "URL");

log(`Account Info about ${name}\nUser: ${user}\nType: ${type}\nURL: ${url}`);
}

getMetaData

Extracts the __metadata property from an object.

Returns the metadata object, or { info: 'NO METADATA' } if none is present.

IMPORTANT: currently only implemented for Xurrent fetch requests that return lists (arrays) of objects.

let info = getMetaData(reqObj);

Example:

let services = fetchFilter("services", "disabled=0", account);
log("count", services.length);
log("meta", getMetaData(services));

getPropertyNames

Lists the property names of an object as a string array. Properties starting with _ are treated as private, and are not part of the result.

Returns an array of property name strings.

let names = getPropertyNames(data_object);

Example:

let ob = {
name: "Max Mustermann",
strasse_und_hausnummer: "Musterstrasse 42",
email_privat: "max.musterman@privat-test-mail.com",
email_arbeit: "max.musterman@arbeit-test-mail.com",
geburtsdatum: new Date(2001, 1, 1),
_invisible_property: "test invisible",
};

let names = getPropertyNames(ob);
for (let name of names) {
log("Property", `${name} = ${ob[name]}`);
}

loadFileToString

Loads file from URI to string.

const string = loadFileToString(uri);

loadFromUri

Loads data from URI.

const encoding = "utf8"; // Other possible values are 'base64' and 'buffer'
const data = loadFromUri(uri, encoding);
// data is an object with attributes content, headers, statusCode, and error

log

Logs a text message with optional data at info log level.

log("Log Description", value);
log("Log Message");
log(value);

Log Levels are:

  • error
  • info
  • verbose
  • debug

For each package the current log level can be adjusted. If the log level is set to error, all log outputs that are not at level error are ignored and do not produce any output to the logs.

Example:

let x = 25;
let y = { key: "value" };
let z = "My message";

log("Simple Text log");
log("Value of x:", x);
log("Value of y:", y);
log(z);

logDebug

Logs a text message with optional data at debug log level.

logDebug("Log Description", [variable]);

See also: log

logError

Logs a text message with optional data at error log level.

logError("Log Description", [variable]);

See also: log

logVerbose

Logs a text message with optional data at verbose log level.

logVerbose("Log Description", [variable]);

See also: log

moment

Creates a Moment.js object for date and time manipulation and formatting.

Returns a Moment object.

For help see Moment Website

let result = moment(date);

See also: date

Example 1:

let dt = new Date("2015-12-20");
let m = moment(dt);
m.add(4, "d");
let text = m.format("DD.MM.YYYY"); // 24.12.2015
log("formatted date :", text);

Example 2:

let dt = new Date();
let m = moment(dt);

let curDate = m.format("DD.MM.YYYY, HH:mm");
log("current Date Time on Server : ", curDate);

let tz = m.tz("Europe/Vienna");
let dateTimeInVienna = tz.format("DD.MM.YYYY, HH:mm");

log("Date Time in Europe / Vienna timezone : ", dateTimeInVienna);

tza = m.tz("America/New_York");
let dateTimeInNewYork = tza.format("DD.MM.YYYY, HH:mm");

log("Date Time in America / New York timezone : ", dateTimeInNewYork);

Example 3:

let dt = new Date(); // current date and time
let m = moment(dt);
m.add(4, "d"); // add 4 days
m.add(5, "w"); // add 5 weeks
m.add(3, "M"); // add 3 months
m.add(2, "Q"); // add 2 quaters
m.add(1, "y"); // add 1 year
m.add(2, "h"); // add 2 hours
m.add(5, "m"); // add 2 minutes
m.subtract(3, "d"); // subtract 3 days
let text = m.format("MMMM DD YYYY, h:mm:ss a");
log("calculated date", text);

Example 4:

let dt = new Date(); // current date and time
let m = moment(dt);

m.startOf("M"); // start of current month
let start = m.format("MMMM DD YYYY, h:mm:ss a");

m.endOf("M"); // end of current month
let end = m.format("MMMM DD YYYY, h:mm:ss a");

log("Start date:", start);
log("End date :", end);

Example 5:

let dt = new Date(); // current date and time
let m = moment(dt);
m.subtract(4, "d"); // subtract 4 days
m.subtract(5, "w"); // subtract 5 weeks
m.subtract(3, "M"); // subtract 3 months
m.subtract(2, "Q"); // subtract 2 quaters
m.subtract(1, "y"); // subtract 1 year
m.subtract(2, "h"); // subtract 2 hours
m.subtract(5, "m"); // subtract 2 minutes
m.add(3, "d"); // add 3 days
let text = m.format("MMMM DD YYYY, h:mm:ss a");
log("calculated date", text);

parse

Parses a JSON-formatted string into an object.

Returns the parsed object.

let result = parse(json_string);

See also: stringify

Example:

let jsonString = '{"name":"Alice","age":30}';
let obj = parse(jsonString);
log("name:", obj.name); // => 'Alice'

sleep

Pauses the execution of the package for the given number of milliseconds.

sleep(duration_in_ms);

Example:

log("before pause");
sleep(500);
log("after 500ms pause");

stringify

Serializes an object to a JSON-formatted string. Before serializing, the object is cleaned recursively and the following properties are stripped:

  • Properties starting with _
  • Properties starting with tw
  • Properties named content
  • Properties with a Buffer value

Returns the JSON string.

let result = stringify(obj);

See also: parse

Example:

let obj = { name: "Alice", age: 30, _internal: "hidden", twMeta: "removed", content: "ignored" };
let jsonString = stringify(obj);
log("json:", jsonString); // => '{"name":"Alice","age":30}'

uuid

Generates a universally unique identifier (UUID v4).

Returns a UUID string.

let result = uuid();

Example:

let uniqueIdentifier = uuid();
log("id:", uniqueIdentifier); // => e.g. '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

Deprecated functions

The functions in this section are deprecated. They still work, but you are encouraged to use the indicated alternatives.

date

warning

The date function is deprecated. Use the Date() constructor instead.

Creates a new Date object.

Returns a JavaScript Date object.

let result = date([params]);

See also: moment

Example 1:

let today = date();
log("today:", today); // => current date and time

Example 2:

let date_1 = date("2015-12-20");
let year = date_1.getFullYear();
let month = date_1.getMonth() + 1; // getMonth delivers 0 for January
let day = date_1.getDate();

let dateText = day + "." + month + "." + year;

let current = date(); // current date and time
let text_default = current.format(dateformat, timezone);
let text_long = current.format("MMMM DD YYYY, h:mm:ss a", timezone);
let text_other_tz = current.format("YYYY-MM-DD, HH:mm:ss", "Europe/Berlin");

newMap

warning

The newMap function is deprecated. Use the Map() constructor instead.

Creates a new map. The map can optionally be initialized with an iterable, such as an array with key-value pairs.

Returns a Map instance.

let map = newMap();
let map = newMap(iterable);

Example:

let map = newMap([["key_1", "value_1"]]);

map.set("key_1", "value_1_updated"); // update value of key_1
map.set("key_2", "value_2");
map.set("key_3", "value_3");

log("value of key_1:", map.get("key_1")); // "value_1_updated"
log("is there a key_5?", map.has("key_5") ? "YES" : "NO"); // NO

newSet

warning

The newSet function is deprecated. Use the Set() constructor instead.

Creates a new set. The set can optionally be initialized with an iterable, such as an array.

Returns a Set instance.

let set = newSet();
let set = newSet(iterable);

Example:

let set = newSet(["value_1"]);

set.add("value_1"); // duplicate value is ignored
set.add("value_2");
set.add("value_3");

log("has value_1:", set.has("value_1")); // => true
log("has value_2:", set.has("value_2")); // => true
log("has value_5:", set.has("value_5")); // => false

RegExp

warning

The RegExp function is deprecated. Use the RegExp() constructor instead.

Creates a regular expression from a pattern string.

Returns a RegExp object.

See MDN RegExp reference for more information.

const pattern = "https?://.*\.(com|org|net)/";
const flags = "i"; // optional flags.
const regex = Regexp(pattern, flags);
// You can also create a regular expression using the literal syntax:
const regex2 = /https?:\/\/.*\.(com|org|net)\//i;

assign

warning

The assign function is deprecated. Use Object.assign or spread syntax instead.

Assigns source objects to a target object using shallow property merging.

Returns the modified target object.

const result = assign(target_object, source_object_1, ...)

assign can take up to 4 source objects.

See also: extend

Example 1:

let ob_a = {
p1: "a",
};

let ob_b = {
p2: "b",
};

assign(ob_a, ob_b); // ob_a is now { p1: 'a', p2: 'b' }

ob_b is integrated into ob_a, ob_a is modified.

Example 2:

let ob_a = {
p1: "a",
};

let ob_b = {
p2: "b",
};

const result = assign({}, ob_a, ob_b); // ob_a and result are now { p1: 'a', p2: 'b' }

getProperty

warning

The getProperty function is deprecated. Use property accessors via the dot notation or the bracket notation instead.

Accesses an object property using a string property name.

Returns the value of the specified property.

let value = getProperty(data_object, property_name);

See also: setProperty

Example:

let custom_data = request.custom_data;
let propName = "new" + "Property"; // calculated name
let result = getProperty(custom_data, propName);
log("value:", result); // => value of custom_data.newProperty

setProperty

warning

The setProperty function is deprecated. Use property accessors via the dot notation or the bracket notation instead.

Sets an object property using a string property name.

setProperty(data_object, property_name, value);

See also: getProperty

Example:

let custom_data = request.custom_data;
let propName = "new" + "Property"; // calculated name
setProperty(custom_data, propName, "42");
log("updated:", custom_data.newProperty); // => '42'

isArray

warning

The isArray function is deprecated. Use Array.isArray instead.

Checks if a variable is an array.

Returns true or false.

let result = isArray(variable);

Example 1:

let list = [];
log("is array:", isArray(list));

Example 2:

let list = [1, 2, 3];
if (isArray(list)) {
for (let el of list) {
log("element:", el);
}
}

checkNull

warning

The checkNull function is deprecated.

In most cases, you will want to use the nullish coalescing operator instead.

For example, write value ?? replacement instead of checkNull(value, replacement).

If you are specifically checking for a falsy value (which includes 0 and ""), you can write value || replacement instead.

Checks if a value is falsy and returns a replacement value if so.

Returns the original value if truthy, or the replacement value otherwise.

let result = checkNull(value, replacement);

Example:

let value = null;
let result = checkNull(value, "replacement");
log("result", result); // => 'replacement'

let value2 = ""; // falsy value
let result2 = checkNull(value2, "replacement");
log("result 2", result2); // => 'replacement'

isNull

warning

The isNull function is deprecated. Write variable == null instead of isNull(variable).

Checks if a variable is null or undefined.

Returns true or false.

let result = isNull(variable);

Example:

if (isNull(object.property)) {
log("property is not available");
}

join

warning

The join function is deprecated. Use Array.join instead.

For example, write elements.join("/") instead of join(element1, element2, ...).

Joins all parameters into a single string separated by /.

Returns the joined string.

let result = join(param1, param2, [param3], [...paramX]);

Example:

let result = join("this", "is", "a", "test");
log("result", result); // => 'this/is/a/test'

pathJoin

warning

The pathJoin function is deprecated. Use Array.map and Array.join instead.

For example, write elements.map(e => e.path_value).join(" ")
instead of pathJoin(elements, "path_value", " ").

Joins the values of a specified property from an array of objects into a single string.

Returns the joined string. Default separator is ,.

let result = pathJoin(list, "property name", [separator]);

Example:

let list = [{ path_value: "This" }, { path_value: "is" }, { path_value: "a" }, { path_value: "test" }];

let result = pathJoin(list, "path_value", " ");
log("result :", result); // => 'This is a test'