Advanced jexl
+++ title: “How to implement advanced JEXL functions in Peaka” description: “Click here for an overview of the advanced functions in JEXL” +++
Unary Operators
Operation | Symbol |
---|---|
Negate | ! |
Example
The exclamation mark negates the boolean that appears next to it.
Binary Operators
Operation | Symbol |
---|---|
Add, Concat | + |
Subtract | - |
Multiply | * |
Divide | / |
Divide and floor | // |
Modulus | % |
Power of | ^ |
Logical AND | && |
Logical OR | || |
Comparisons
Comparison | Symbol |
---|---|
Equal | == |
Not equal | != |
Greater than | > |
Greater than or equal | >= |
Less than | < |
Less than or equal | <= |
Element in array or string | in |
A note about in
The in
operator can be used to check for a substring:
"Cad" in "Ron Cadillac"
, and it can be used to check for an array element:
"coarse" in ['fine', 'medium', 'coarse']
. However, the ==
operator is used
behind-the-scenes to search arrays, so it should not be used with arrays of
objects. The following expression returns false: {a: 'b'} in [{a: 'b'}]
.
Ternary operator
Conditional expressions are used to check if the first segment returns a correct value. If that’s the case, the value in the next segment is applied. Otherwise, the other value is applied. If the next segmeent is missing, the test result itself will be used instead.
Expression | Result |
---|---|
"" ? “Full” : “Empty” | Empty |
”foo” in “foobar” ? “Yes” : “No” | Yes |
{agent: "Archer"}.agent ?: "Kane" | Archer |
Native Types
Type | Examples |
---|---|
Booleans | true , false |
Strings | ”Hello “user"", ‘Hey there!’ |
Numerics | 6, -7.2, 5, -3.14159 |
Objects | {hello: "world!"} |
Arrays | [‘hello’, ‘world!’] |
Groups
Parentheses work just how you’d expect them to:
Expression | Result |
---|---|
(83 + 1) / 2 | 42 |
1 < 3 && (4 > 2 || 2 > 4) | true |
Identifiers
Access variables in the context object by just typing their name. Objects can be accessed with dot notation, or by using brackets to access a dynamic property name.
Example context:
Expression | Result |
---|---|
name.first | Malory |
name[‘la’ + ‘st’] | Archer |
exes[2] | Burt Reynolds |
exes[lastEx - 1] | Len Trexler |
Functions
In JEXL, common built-in functions are readily available, but also functions library is extended with lodash functions.
You can use lodash functions from this link.
Additionally, you can check our functions from this article.
Example
Check out the JEXL codes below:
Find Min
Reverse Array
Check Equal
Add Elements To Array
Get Last Element of Array