Functions

Updated 1 month ago by Karan

In this step-by-step guide, you will understand how to use functions for optimizing the data in the required format.

Configuring functions

The functions are used to convert data format in various task nodes of a Byteline flow. By default, all data is sent as a string, but the functions can be used to send the data in the appropriate format. For example, if the Airtable column is configured as a number, it should be sent in that format only.

Let's configure functions to convert the data format.

Step 1: Click on the selector tool to select the functions for converting data format.

Step 2: Click on the function button to view all the functions.

Step 3: You can select any of the functions you want to use from the functions pop-up.

Using expressions in functions

You can easily use Byteline expressions in the functions. The expression can be copied using the "Copy expression" button below. This button takes you to the variables tab. Once you click on a variable, it's copied to the clipboard, and then you're navigated back to the functions tab. Now you can paste the variable to any function.

Supported Functions

There are the following functions:

EpochToDate

Select the EpochToDate function to convert the epoch to ISO 8601 format. It takes epoch in both seconds and milliseconds format to convert to the yyyy-MM-dd'T'HH:mm:ss.SSS format.

Examples:

1641645704 -> 2022-01-08T12:41:44.000Z

OR

1641645704123 -> 2022-01-08T12:41:44.123Z

parseDate

This function is used to convert a text to a standard (ISO 8601) date format. Following tokens can be used to define the date format.

MM (month)
MMM (month in short text, e.g. Feb)
MMMM (month in full text, e.g. February)
dd(date)
yyyy (year)
HH (Hour in day)
hh (Hour in am/pm)
mm (minutes)
ss (seconds)
a (am/pm marker)
EEE (Day name in week e.g, Wed)
z (General time zone e.g, Pacific Standard Time or PST orGMT-08:00
S (Milliseconds)

Examples:
01/15/2022 | parseDate('MM/dd/yyyy') -> 2022-01-15T00:00:00.000Z
01/15/2022 1:20 | parseDate('MM/dd/yyyy HH:mm') -> 2022-01-15T01:20:00.000Z
01/15/2022 1:20pm | parseDate('MM/dd/yyyy hh:mma') -> 2022-01-15T13:20:00.000Z
Wed Mar 30 2022 07:00:00 +0000 | parseDate('EEE MMM dd yyyy hh:mm:ss z') -> 2022-03-30T07:00:00.000Z
Mar 30, 2022 | parseDate('MMM dd, yyyy') -> 2022-03-30T00:00:00.000Z
Wed, 03 Jun 2020 04:00:33 UTC | parseDate('EEE, dd MMM yyyy hh:mm:ss z') -> 2020-06-03T04:00:33.000Z

Number

Select the number function to convert the input data to a numerical format. The Numeric values can be Integer, float, decimal, etc.

Bool

Select the bool function to convert the input data to a boolean format. In boolean format, data can have two possible values either true or false.

Today

Select today function to generate today's date in the MM/dd/yyyy format.

Replace

Select replace function to replace any regular expression.

  1. Enter the text to be replaced in the search for field.
  2. Enter the replacement text in the replacement field.
  3. Select either yes or no option from the is regex dropdown menu to confirm whether the replacement text is a regular expression or not.
  4. Click on the Save button to save the function configuration.
Array

Select array function to convert a comma-separated string into an array. The data is represented as type[], where type is the data type of the contained elements and brackets represent the array.

Maxlength

Select maxlenght function is used when the user wants to keep the first n characters from a string. You can also pass an integer value as an argument.

  1. Enter the maxlenght of strings you want in the limit field.
  2. Click on the Save button to save the maxlenght configuration.

Sha1

Select the Sha1 function to convert a string to the hash value. Sha1 is used to calculate an alphanumeric string, serving as a cryptographic representation of data. The SHA-1 function is used to carry out Hex and Base64 encoding.

  1. Select the encryption option from the encoding dropdown menu.

There are the following encoding options:

  • Hex - Hex encoding is performed to convert 8-bit data to 2 hex characters.
  • Base64 - Base64 encoding is performed to convert binary data to an ASCII text format, and vice versa.
  1. If you have performed HMAC SHA1 then you need to enter the decryption key value in the key field.
  2. Click on the Save button to save the SHA1 configuration.
Image

Select image function to convert image URL to the required format e.g. Airtable.

MarkdownToHtml

Select markdownToHtml function to convert Markdown text to HTML.

ElementAt

Select elementAt function to fetch an element from an array. You can specify an index to get the particular element, 0 is used to indicate the first element index.

  1. Enter the index in the index field to fetch the particular element.
  2. Click on the Save button to save the elementAt configuration.
String

Select the string function to convert an input value to a string.

jq

Click on the jq function to execute the JQ expression on the input data. This helps to convert or extract data from the input JSON data.

  1. Enter the JSON data you want to convert in the expression field.
  2. Click on the Save button to save the jq configuration.

Thus you can use functions in your nodes to set them into the right format. Feel free to connect us for any doubt.

map

You can use the map function to extract a field from an array based on the value of another field. This function is applied on an array element and requires the following arguments:

matching value: This value is matched for

matching field: Value of this field is used for matching

lookup field: Value of this field is returned.

For example: if you want to extract the id of the Acme company from the below array, you can apply the following map functions on the provided array.

map('Acme','company','id')

[
{
"company":"Infinite",
"id":"543gdfgdf3465634"
},
{
"company":"Acme"
"id":"dfgdf534534345"
}
]

It will look as below on the Byteline Console Update task when used for the Company1 field.

if

The if function returns a value depending on the value of the conditional expression. If the condition is true, it returns the first argument; otherwise, the second argument. It requires the following arguments:

conditional expression: Either an expression resolving to a boolean or a JavaScript logical expression 

value if true: Value to return if the conditional expression is true

value if false: Value to return if the conditional expression is false

Examples:

Assuming you have the below input JSON.

{
"live":true,
"counter":10
}

if('${input.live}', 'value1', 'value2')

Above if function will return value1.

Using JavaScript expression

if('${input.counter} == 10', 'value1', 'value2')

This function will return the value1. You can use any JavaScript expression. Here is a JavaScript expressions reference documentation.

base64

Converts a text to its Base64


How did we do?