Expressions can be used in Snaps such as the Mapper (Data) Snap to manipulate data. The expressions are based off a subset of JavaScript and act accordingly unless otherwise noted.
See Expression Language Usage for specifics on how to enter expressions.
Operators
Comparison Operators
Comparison operators (>, >=, <, <=, ==, !=) behave the same as JavaScript comparison operators except that strict equals (===) and strict not equals (!===) are not supported.
Arithmetic Operators
Arithmetic operators (+, -, *, /) operators behave the same as JavaScript arithmetic operators except that increment (++) and decrement (--) are not supported.
Example:
1/2 = .5
parseInt(1/2) = 0
Logical Operators
Logical operators (&&, ||, !) behave the same as JavaScript logical operators.
String Operators
String operators (+) behave the same as JavaScript string operators except that the shorthand assignment operator (+=) is not supported.
Special Operators
Conditional (Ternary)
The conditional operator (?) allows you to specify a value to use based on the result of an expression. The syntax is as follows:
condition ? trueValue : falseValue
The condition
expression will be evaluated and the trueValue
will be the result of the expression if the condition evaluates to true, otherwise the falseValue
will be the result.
Example:
$text == "NFL" ? "foo" : "bar"
Example:
$counter > 1 ? ($counter < 3 ? 50 : 100) : -1
instanceof
The instanceof operator returns True if the given object is an instance of the given type. The syntax is as follows:
The possible values for the type are: Null, Boolean, String, Number, Object, Array, Date, LocalDate, DateTime, and LocalDateTime.
Example:
$my_array instanceof Array
typeof
The typeof operator returns the type of a value as a string. The syntax is as follows:
The possible return values are: "boolean", "number", "string", "object", and "array".
You can add notes to your expressions using comments. A comment starts with '/*' and ends with '*/', for example:
/* say hello */ "Hello, World!"
The comment will be ignored when evaluating the expression, it is only for the reader's benefit.
Operator Precedence
Operator type | Individual operators |
---|
member | . [] |
call | () |
negation | ! - |
multiply/divide | * / % |
addition/subtraction | + - |
relational | < <= > >= |
equality | == != |
logical-and | && |
logical-or | || |
comma | , |
Unsupported Operations
- Assignment
- Creating variables and assigning values is not supported
- Example: var temp = 10
- Short hand assignment
- Strict equals
- Strict not equals
- Increment
- Decrement
Accessing Document Values
To access values in a document, JavaScript object accessors can be used.
For a given document data:
{
first_name: "James",
last_name: "Smith"
}
The expression $first_name would return the 'first_name' property which is James.
The 'first_name' property can also be accessed by using array notation $['first_name'].
JavaScript array accessors can be used also if the object is an array/list.
For a given document data:
[ 1, 2, 3]
$[1] would return the value 2.
More complex example:
{
names: ["Joe", "Bob", "Fred"]
}
$names[2] would return the value Fred.
Arrow Functions
Custom expression language functions can be created using the arrow function syntax:
// A function that takes multiple parameters:
(param1, param2, ..., paramN) => expression
// A function that takes a single parameter does not need parentheses:
(param) => expression
param => expression
// A function with no parameters requires parentheses:
() => expression
// Function parameters can also have default values
(param1 = defaultValue1, param2 = defaultValue2, ..., paramN = defaultValueN) => expression
These functions can be passed to other functions that accept callbacks, like Array.map() or Array.filter(), or they can be put into an expression library for use in any expression property in your pipelines.
Example
To multiply all numbers in an array by ten:
[1, 2, 3].map(x => x * 10)
Result:
[10, 20, 30]
Pages in this Section