Skip to main content

Miscellaneous

A collection of miscellaneous functions.

Miscellaneous Types

FetchOptions
{
method: string,
headers: Record<string, string>,
body: string
}
FetchResponse
{
status: int,
statusText: string,
ok: boolean,
body: stream,
bodyUsed: boolean,
headers: Record<string, string>,
text(): string, // Parse the body as text
json(): object, // Parse the body and deserialize from JSON
bytes(): byte[] // Parse the body as an array of bytes
}
CsvOptions
{
delimiter: string,
hasHeaders: boolean
}

console

Write a log line with different levels of verbosity to the execution output.

info

If the execution context has a logging verbosity higher than the emitted log, it be ignored. For example; if the function is executing with a verbosity of information, then debug messages will be omitted for the output.

console.log("This is a general informational message")
console.debug("This helps understand the inner workings of the function")
console.warn("Something is wrong, but the function can proceed")
console.error("Something is wrong, and the function must stop")
NameTypeDescription
messagestringThe message to write to the output.

JSON.parse

Parse a JSON string, constructing the JavaScript value or object described by the string.

JSON.parse(text)

Parameters

NameTypeDescription
textstringThe string to parse as JSON.

Returns The object, array, string, number, boolean, or null value corresponding to the given JSON text.

Example

var json = '{ "result": true, "count": 42 }';
var obj = JSON.parse(json);

JSON.stringify

Converts a JavaScript object or value to a JSON string.

JSON.stringify(value)

Parameters

NameTypeDescription
valueobjectThe value to convert to a JSON string.

Returns A JSON string representing the given value, or undefined.

Example

var json = JSON.stringify({ x: 5, y: 6 });
// expected output: "{"x":5,"y":6}"

CSV.parse

Parse a CSV into a collection of records.

CSV.parse(text, options);

Parameters

NameTypeDescriptionOptional
textstringThe CSV text to parse.
optionsCsvOptionsOptions to configure the parsing process.

Returns An array of records which map to the column and row values.

Example

var records = CSV.parse('col1,col2\r\nvalue1,value2\r\n');
// expected output: [{ col1: "value1", col2: "value2" }]

CSV.stringify

Stringify an array of records to a CSV string.

CSV.stringify(records, options);

Parameters

NameTypeDescriptionOptional
recordsobject[]The array of records to stringify
optionsCsvOptionsOptions to configure the parsing process.

Returns The records serialized as a string.

Example

var records = [{ col1: "value1", col2: "value2" }];
var csv = CSV.stringify(records);
// expected output: 'col1,col2\r\nvalue1,value2\r\n'

fetch

caution

This built-in fetch function differs from the traditional fetch function you may be familiar with. For example, the response is not a Promise and is synchronous, as well as not all options are available.

Decode a string of data which has been encoded using Base64 encoding.

fetch(url, options)

Parameters

NameTypeDescription
urlstringThe resource that you wish to fetch.
optionsFetchOptionsAn object containing any custom settings that you want to apply to the request.

Returns A fetch response object.

Example

var response = fetch('http://some.api/user', {
method: 'post',
body: JSON.stringify({
firstName: 'John',
lastName: 'Smith'
})
});

var data = response.json();

atob

Decode a string of data which has been encoded using Base64 encoding.

atob(encodedData)

Parameters

NameTypeDescription
encodedDatastringA binary string containing base64-encoded data.

Returns An ASCII string containing decoded data from encodedData.

Example

var encodedData = btoa('Hello World!'); // encode a string
var decodedData = atob(encodedData); // decode the string

btoa

Creates a Base64-encoded ASCII string from a binary string.

btoa(stringToEncode)

Parameters

NameTypeDescription
stringToEncodestringThe binary string to encode.

Returns An ASCII string containing the Base64 representation of stringToEncode.

Example

var encodedData = btoa('Hello World!'); // encode a string
var decodedData = atob(encodedData); // decode the string

sha256

Computes the hash value for the specified UTF8 string.

sha256('Hello World!')

Parameters

NameTypeDescription
inputstringThe input UTF8 string to compute the hash code for.

Example

var hashedData = sha256('Hello World!');