Miscellaneous
A collection of miscellaneous functions.
Miscellaneous Types
{
method: string,
headers: Record<string, string>,
body: string
}
{
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
}
{
delimiter: string,
hasHeaders: boolean
}
console
Write a log line with different levels of verbosity to the execution output.
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")
Name | Type | Description |
---|---|---|
message | string | The 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
Name | Type | Description |
---|---|---|
text | string | The 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
Name | Type | Description |
---|---|---|
value | object | The 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
Name | Type | Description | Optional |
---|---|---|---|
text | string | The CSV text to parse. | ❌ |
options | CsvOptions | Options 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
Name | Type | Description | Optional |
---|---|---|---|
records | object[] | The array of records to stringify | ❌ |
options | CsvOptions | Options 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
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
Name | Type | Description |
---|---|---|
url | string | The resource that you wish to fetch. |
options | FetchOptions | An 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
Name | Type | Description |
---|---|---|
encodedData | string | A 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
Name | Type | Description |
---|---|---|
stringToEncode | string | The 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
Name | Type | Description |
---|---|---|
input | string | The input UTF8 string to compute the hash code for. |
Example
var hashedData = sha256('Hello World!');