JavaScript in Make

IML is a mustache-like markup language with support for evaluating complex expressions in javascript-like syntax. Expressions are written between mustaches: {{body.data}}.

Available Operators

Algebraic Operators

+, -, *, /, %

Equality Operators

== (same as = or ===), != (same as !==)

All equality operations are strict.

Relational Operators

<, <=, >, >=

Logical Operators

&&, ||, !

Strings Inside Expressions

you can use ' or " to put strings inside IML expressions:

{{'Hello, ' + item.name}}
{{"Hello, " + item.name}}

Conditionals

You can create conditionals with if and ifempty functions.

{{if(body.id > 100, body.user, false)}}
{{ifempty(body.username, body.user)}}

ifempty function returns second argument when first argument is either not defined, null or empty string. Otherwise it returns first argument.

Retrieving Collection Properties

You can use normal JavaScript . (dot) notation to retrieve properties of collections, such as parameters: {{parameters.input.data}}.

If the property name contains spaces or starts with a non-alphabetic character or contains non-alphanumeric characters (except underscore _), you can use ` (back ticks) to retrieve such properties: {{headers.`X-HOOK-TYPE`}} or {{parameters.`First Name`}}.

Retrieving Array Items

You can use normal JavaScript [] brackets notation to retrieve a specific element from an array: {{body.data[2].prop}}. Unlike JavaScript, IML uses 1 to access the first item in an array.

You can also use -1 to access the last element of an array, like so: body.data[-1].prop.

Tip: If you need to retrieve a property of an object, and the property name is contained in a variable, or computed, the dot . notation will not work. For this, you can use the get function, where the first argument of the function is the object you wish to retrieve the property from, and the second argument is the property name, or a variable containing a property name, or an expression that resolves to a property name: get(body.data, 'some property'), get(body, parameters.fieldName), get(body.data, if(body.xml == true, 'xml', 'json'))

Built-in IML Functions

There are plenty of Built-in IML functions, like:

  • String functions:

FunctionSpecification

ascii()

Removes all non-ascii characters from a text string.

base64()

Transforms text to base64.

capitalize()

Converts the first character in a text string to uppercase.

contains()

Verifies if text contains the search string.

decodeURL()

Decodes special characters in URL to text.

encodeURL()

Encodes special characters in a text to a valid URL address.

escapeHTML()

Escapes all HTML tags in text.

indexOf()

Returns the position of the first occurrence of a specified value in a string. This method returns '-1' if the value searched for never occurs.

length()

Returns the length of a text string (number of characters) or binary buffer (buffer size in bytes).

lower()

Converts all alphabetical characters in a text string to lowercase.

md5()

Calculates the md5 hash of a string.

mime()

Gets MIME Type of the file from its filename with extension. E.g. calling mime(picture.png) will return image/png MIME Type.

replace()

Replaces the search string with the new string.

sha1()

Calculates the sha1 hash of a string. If the key argument is specified, sha1 HMAC hash is returned instead. Supported encodings: hex (default), base64, or latin1.

sha256()

Calculates the sha256 hash of a string. If the key argument is specified, sha256 HMAC hash is returned instead. Supported encodings: hex (default), base64, or latin1.

sha512()

Calculates the sha512 hash of a string. If the key argument is specified, sha512 HMAC hash is returned instead. Supported encodings: hex (default), base64, or latin1. Supported key encodings: text (default), hex, base64, or binary. When using binary key encoding, a key must be a buffer, not a string.

split()

Splits a string into an array of strings by separating the string into substrings.

startcase()

Capitalizes the first letter of every word and lower cases of all other letters.

stripHTML()

Removes all HTML tags from text.

substring()

Returns a portion of a text string between the "start" position and "the end" position.

toBinary()

Converts any value to binary data. You can also specify encoding as a second argument to apply binary conversions from hex or base64 to binary data.

toString()

Converts any value to a string.

trim()

Removes space characters at the start or end of the text.

upper()

Converts all alphabetical characters in a text string to uppercase.

  • Array functions:

FunctionSpecification

add()

Adds values specified in parameters to an array and returns that array.

contains()

Verifies if an array contains the value.

deduplicate()

Removes duplicates from an array.

distinct()

Removes duplicates inside an array. Use the key argument to access properties inside complex objects. To access nested properties, use dot notation. The first item in an array is index 1.

first()

Returns the first element of an array.

flatten()

Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

join()

Concatenates all the items of an array into a string, using a specified separator between each item.

keys()

Returns an array of a given object's or array's properties.

last()

Returns the last element of an array.

length()

Returns the number of items in an array.

map()

Returns a primitive array containing values of a complex array. Allows filtering values. Use raw variable names for keys.

merge()

Merges two or more arrays into one array.

remove()

Removes values specified in the parameters of an array. Effective only in the case of primitive arrays of text or numbers.

reverse()

The first element of the array becomes the last element and vice versa.

shuffle()

Shuffles (randomly reorders) elements of an array.

slice()

Returns a new array containing only selected items.

sort()

Sorts values of an array.

toArray()

Converts a collection into an array of key-value collections.

toCollection()

Converts an array containing objects with key-value pairs into a collection.

  • Date & Time functions:

FunctionSpecification

formatDate()

Formats a date into a specific format, e.g. 'YYYY-MM-DD'.

parseDate()

Parses a date in a specific format, e.g. 'YYYY-MM-DD HH:mm'.

addDays()

Returns a new date as a result of adding a given number of days to a date. To subtract days, enter a negative number.

addHours()

Returns a new date as a result of adding a given number of hours to date. To subtract hours, enter a negative number.

addMinutes()

Returns a new date as a result of adding a given number of minutes to date. To subtract minutes, enter a negative number.

addMonths()

Returns a new date as a result of adding a given number of months to date. To subtract months, enter a negative number.

addSeconds()

Returns a new date as a result of adding a given number of seconds to date. To subtract seconds, enter a negative number.

addYears()

Returns a new date as a result of adding a given number of years to date. To subtract years, enter a negative number.

setSecond()

Returns a new date with the seconds specified in parameters. Accepts numbers from 0 to 59. If a number is given outside of this range, it will return the date with the seconds from the previous or subsequent minute(s), accordingly.

setMinute()

Returns a new date with the minutes specified in parameters. Accepts numbers from 0 to 59. If a number is given outside of this range, it will return the date with the minutes from the previous or subsequent hour(s), accordingly.

setHour()

Returns a new date with the hour specified in parameters. Accepts numbers from 0 to 59. If a number is given outside of this range, it will return the date with the hour from the previous or subsequent day(s), accordingly.

setDay()

Returns a new date with the day specified in parameters. It can be used to set the day of the week, with Sunday as 1 and Saturday as 7. If the given value is from 1 to 7, the resulting date will be within the current (Sunday-to-Saturday) week. If a number is given outside of the range, it will return the day from the previous or subsequent week(s), accordingly.

setDate()

Returns a new date with the day of the month specified in parameters. Accepts numbers from 1 to 31. If a number is given outside of the range, it will return the day from the previous or subsequent month(s), accordingly.

setMonth()

Returns a new date with the month specified in parameters. Accepts numbers from 1 to 12. If a number is given outside of this range, it will return the month in the previous or subsequent year(s), accordingly.

setYear()

Returns a new date with the year specified in parameters.

  • Math functions:

FunctionSpecification

average()

Returns the average value of the numeric values in a specific array, or the average value of numerical values entered individually.

ceil()

Returns the smallest integer greater than or equal to a specified number.

floor()

Returns the largest integer less than or equal to a specified number.

formatNumber()

Returns a number in the requested format. A decimal point is, by default, Thousands separator is, by default.

max()

Returns the largest number in a specified array, or the largest number among numbers entered individually.

min()

Returns the smallest number in a specified array, or the smallest number among numbers entered individually.

parseNumber()

Parses a string with a number and returns the number.

round()

Rounds a numeric value to the nearest integer.

sum()

Returns the sum of the values in a specified array, or the sum of numbers entered individually.

  • JSON functions:

FunctionSpecification

createJSON()

Creates a JSON string inside a JSON object.

parseJSON()

Parses a JSON string inside a JSON object.

In the custom functions, the JSON.stringify() or JSON.parse() functions should be used instead of createJSON() or parseJSON().

Last updated