General Functions in Rule Expressions

In additional to traditional math expressions like addition, multiplication, division, and averaging, rule expressions provide a variety of general functions that are helpful for boolean logic, comparisons, and more. For more details on how to use expressions, check out the Introduction to Expressions article.

Math Features

abs(input)

Returns the absolute value of an input number.

abs(-20) #// returns: 20

ceil(input)

Rounds up and returns the smaller integer greater than or equal to the given number.

ceil(9.1) #// returns: 10

floor(input)

Rounds down and returns the largest integer less than or equal to the given number.

floor(9.9) #// returns: 9

round(input[, precision])

Rounds a given number to the nearest integer, or to the nearest number of decimals when given a precision.

round(9.9)     #// returns: 10
round(9.98)    #// returns: 10
round(9.88, 1) #// returns: 9.9

toFixed(input[, digits])

Takes a numeric input along with a number of digits to display and formats the number as a string with the specified number of digits (rounding if necessary). Note the distinction from round() in that toFixed() outputs a string and retains the exact number of digits specified, zero-padding if necessary.

toFixed(12.345, 2) #// "12.35"
toFixed(12.345, 0) #// "12"
toFixed(120, 2)    #// "120.00"

random([min, max])

Returns a random number between 0 and 1. When provided with a min and max argument, returns a random number between min and max.

random()                #// returns: 0.98
random(0, 100)          #// returns: 78.34
round(random(100, 200)) #// returns: 152
randomInt(100, 200)     #// returns: 152
See the example for randomInt() above which can be used as an alias / shorthand for getting a random integer value

Boolean Logic

and(a, b)

Returns true if a and b are both true

and(1 > 0, true)   #// returns: true
1 > 0 and true     #// returns: true
(true) and (false) #// returns: false

or(a, b)

Returns true if either a or b are true

or(0 >1, true).   #// returns: true
0 > 1 or true     #// returns: true
(true) or (false) #// returns: true

not(input)

Flips a boolean value of a given input

not(true)  #// returns: false
not(false) #// returns: true
not(1 > 0) #// returns: false

Arrays

count(array)

Returns the count of the number of items in an array (especially useful when combined with filter)

count([1,2,3]) #// returns: 3
count(filter([1,2,1,12,9,11], x > 10)) #// returns: 2

filter(array, expression)

Filters a given array based on the boolean expression being applied to each element of the given input array.

filter([1,2,1,12,9,11], x > 10) #// returns: [12, 11]

map(array, expression)

Create a new array with the results of the expression applied to each element of the array

map([1,2,3], x * 2) #// returns: [2,4,6]

pickRandom(array)

Selects a random item from the array

pickRandom([1,2,3])               #// returns: 2
pickRandom(['foo', 'bar', 'baz']) #// returns: 'foo'

join(array, joiner)

Concatenates elements of the array together using the given joiner.

join(['foo', 'bar', 'baz'], ', ') #// returns: 'foo, bar, baz'
join(['foo', 'bar', 'baz'], '-')  #// returns: 'foo-bar-baz'

Objects

objectKeys(object)

Outputs an array which contains the key names of the top-level properties of the input object

objectValues(object)

Outputs an array which contains the values of the top-level properties of the input object

obj = { "first": "value1", "second": 2, "third": {"foo": "bar"}}
objectKeys(obj)   #\\ Output: ["first", "second", "third"]
objectValues(obj) #\\ Output: ["value1", 2, {"foo": "bar"}]

See example usages in the community.

General String Manipulation

parseJson(input)

Parse and input string as a JSON object

parseJson("2")                  #// returns: 2
parseJson("\"test\"")           #// returns "test"
parseJson("[1,2,3]")            #// returns [1,2,3]
parseJson("[\"foo\", \"bar\"]") #// returns: ["foo", "bar"]
parseJson("{\"value\": 2}")     #// returns: {"value": 2}

stringify(input)

Convert a JSON object (or number/string) into a JSON string

stringify(2)                #// returns: "2"
stringify("test")           #// returns '"test"'
stringify([1,2,3])          #// returns "[1,2,3]"
stringify(["foo", "bar"])   #// returns: '["foo", "bar"]'
stringify({"value": 2})     #// returns: '{"value": 2}'

concat(a, b, [c, d, ...z])

Joins two or more strings together

concat("foo", "bar")          #// returns "foobar"
concat("Hello", " ", "World") #// returns "Hello World"

urlEncode(input)

Converts a given input string to its URL encoded equivalent

urlEncode("Mañana") #// returns: "Ma%C3%B1ana"

Content Checks

isEmpty(input)

Checks if a given input  is ‘empty’. Returns true  for an input string that is null or blank "" , an array with no items, or a number that is 0. Otherwise returns false

equalText(left, right)

Checks if a given input string on the left exactly matches the input string on the right . Returns true  for a match. Otherwise returns false

contains(input, searchTerm)

Returns true  if an input  contains a given searchTerm

indexOf(input, searchTerm)

Returns the zero-based ‘index’ (position) of the first occurrence of a given searchTerm  within an input  string.

indexOf("Hello World", "l") #// returns: 2

lastIndexOf(input, searchTerm)

Returns the zero-based index (position) of the last occurrence of a given searchTerm  within an input  string.

lastIndexOf("Hello World", "l") #// returns: 9

startsWith(input, searchTerm)

Returns true  if the input  string begins with the given searchTerm

endsWith(input, searchTerm)

Returns true  if the input  string ends with the given searchTerm

Substrings

replace(input, searchTerm, replacement)

Replaces the searchTerm  with the replacement  value if found in the input

replace("Hello World", "World", "Universe")  #// result: "Hello Universe" 
replace("Hello World", "Hello ", "")         #// result: "World" 
replace("Hello, World", ",", ":")            #// result: "Hello: World"

split(string, separator)

Splits a given string  using the separator  into an array of substrings.

split("Hello - World", " - ") #// returns: ["Hello", "World"]

mid(input, start, length)

Given a zero-based start  position, returns the remaining length  of characters. If length  is omitted, returns all remaining characters.

mid("Hello World", 6)    #// returns: "World" 
mid("Hello World", 1, 4) #// returns: "ello" 
mid("Hello World", -5)   #// returns: "World"

Note that you can use a negative start index which counts from the end of the string instead of the beginning

substring(input, start, end)

Returns a substring from a given input  string starting at the zero-based start  position and ending at the zero-based end  position. If end  is omitted, returns all remaining characters.

Note that this is different from mid() as it requires an  end  index rather than  length

substring("Hello World", 0, 5) #// returns: "Hello" 
substring("Hello World", 6)    #// returns "World"

left(input, length)

Returns the first length  characters from an input  stringright(input, length)

Returns the last length  characters from an input  string

Remove Whitespace

trim(input)

Removes all whitespace from before and after a given input  string

trimLeft(input)

Removes all whitespace from before a given input  string 

ltrim(input)

Alias for trimLeft()  

trimRight(input)

Removes all whitespace from after a given input  string 

rtrim(input)

Alias for trimRight()

Change Case

upper(input)

Returns an input  string formatted as all 'UPPER CASE'  

lower(input)

Returns an input  string formatted as all 'lower case'  

title(input)

Returns an input  string formatted as 'Title Case'

Conditions / Ternary Expression

condition ? trueExpression : falseExpression

Given a condition, returns the result of either the 'true expression' or 'false expression' based on the result of the condition evaluation. This could be read as 'If condition is true, then return the true expression, else return the false expression'

equalText($switchState, "on") ? "active" : "inactive" #// conditionally convert a state
factor = (X > 0) ? 1 : -1    #// determine if you want a positive or negative factor
isEmpty(X) ? "default" : X   #// default a value if source value is not set

Ternary expressions can also be chained together

condition1 ? value1 :
 condition2 ? value2 :
 condition3 ? value3 :
 value4

This is roughly equivalent to the following logical statement (the example below is not valid expression syntax, but is how you might describe the above chained ternary expression in words):

if (condition1)
  then value1
else if (condition2)
  then value2
else if (condition3)
  then value3
else
  value4
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.