Functions
Examples:
Membership Operator
Fields of structs and items of maps can be accessed with .
operator
or []
operator. Elements of arrays and slices can be accessed with
[]
operator. Negative indices are supported with -1
being
the last element.
The in
operator can be used to check if an item is in an array or a map.
Nil coalescing
The ??
operator can be used to return the left-hand side if it is not nil
,
otherwise the right-hand side is returned.
Slice Operator
The slice operator [:]
can be used to access a slice of an array.
For example, variable array
is [1, 2, 3, 4, 5]
:
Pipe Operator
The pipe operator |
can be used to pass the result of the left-hand side
expression as the first argument of the right-hand side expression.
For example, expression split(lower(user.Name), " ")
can be written as:
String Functions
trim(str[, chars])
Removes white spaces from both ends of a string str
.
If the optional chars
argument is given, it is a string specifying the set of characters to be removed.
trimPrefix(str, prefix)
Removes the specified prefix from the string str
if it starts with that prefix.
trimSuffix(str, suffix)
Removes the specified suffix from the string str
if it ends with that suffix.
upper(str)
Converts all the characters in string str
to uppercase.
lower(str)
Converts all the characters in string str
to lowercase.
split(str, delimiter[, n])
Splits the string str
at each instance of the delimiter and returns an array of substrings.
splitAfter(str, delimiter[, n])
Splits the string str
after each instance of the delimiter.
replace(str, old, new)
Replaces all occurrences of old
in string str
with new
.
repeat(str, n)
Repeats the string str
n
times.
indexOf(str, substring)
Returns the index of the first occurrence of the substring in string str
or -1 if not found.
lastIndexOf(str, substring)
Returns the index of the last occurrence of the substring in string str
or -1 if not found.
hasPrefix(str, prefix)
Returns true
if string str
starts with the given prefix.
hasSuffix(str, suffix)
Returns true
if string str
ends with the given suffix.
Date Functions
The following operators can be used to manipulate dates:
now()
Returns the current date and time.
duration(str)
Returns the duration value of the given string str
as nanoseconds. Use the additional functions to return it in a different unit.
Valid time units are “ns”, “us” (or “µs”), “ms”, “s”, “m”, “h”.
See also the following utility functions.
date(str[, format[, timezone]])
Converts the given string str
into a date representation.
If the optional format
argument is given, it is a string specifying the format of the date.
The format string uses the same formatting rules as the standard
Go time package.
If the optional timezone
argument is given, it is a string specifying the timezone of the date.
If the format
argument is not given, the v
argument must be in one of the following formats:
- 2006-01-02
- 15:04:05
- 2006-01-02 15:04:05
- RFC3339
- RFC822,
- RFC850,
- RFC1123,
timezone(str)
Returns the timezone of the given string str. List of available timezones can be found here.
To convert a date to a different timezone, use the In() method:
Number Functions
max(n1, n2)
Returns the maximum of the two numbers n1
and n2
.
min(n1, n2)
Returns the minimum of the two numbers n1
and n2
.
abs(n)
Returns the absolute value of a number.
ceil(n)
Returns the least integer value greater than or equal to x.
floor(n)
Returns the greatest integer value less than or equal to x.
round(n)
Returns the nearest integer, rounding half away from zero.
sum(array)
Returns the sum of all numbers in the array.
mean(array)
Returns the average of all numbers in the array.
median(array)
Returns the median of all numbers in the array.
Array Functions
all(array, predicate)
Returns true if all elements satisfies the predicate. If the array is empty, returns true.
any(array, predicate)
Returns true if any elements satisfies the predicate. If the array is empty, returns false.
one(array, predicate)
Returns true if exactly one element satisfies the predicate. If the array is empty, returns false.
none(array, predicate)
Returns true if all elements does not satisfy the predicate. If the array is empty, returns true.
map(array, predicate)
Returns new array by applying the predicate to each element of the array.
filter(array, predicate)
Returns new array by filtering elements of the array by predicate.
find(array, predicate)
Finds the first element in an array that satisfies the predicate.
findIndex(array, predicate)
Finds the index of the first element in an array that satisfies the predicate.
findLast(array, predicate)
Finds the last element in an array that satisfies the predicate.
findLastIndex(array, predicate)
Finds the index of the last element in an array that satisfies the predicate.
groupBy(array, predicate)
Groups the elements of an array by the result of the predicate.
count(array, predicate)
Returns the number of elements what satisfies the predicate.
Equivalent to:
concat(array1, array2[, …])
Concatenates two or more arrays.
join(array[, delimiter])
Joins an array of strings into a single string with the given delimiter. If no delimiter is given, an empty string is used.
concat(array1, array2[, …])
Concatenates two or more arrays.
reduce(array, predicate[, initialValue])
Applies a predicate to each element in the array, reducing the array to a single value.
Optional initialValue
argument can be used to specify the initial value of the accumulator.
If initialValue
is not given, the first element of the array is used as the initial value.
Following variables are available in the predicate:
#
- the current element#acc
- the accumulator#index
- the index of the current element
sum(array)
mean(array)
median(array)
first(array)
Returns the first element from an array. If the array is empty, returns nil
.
last(array)
Returns the last element from an array. If the array is empty, returns nil
.
take(array, n)
Returns the first n
elements from an array. If the array has fewer than n
elements, returns the whole array.
reverse(array)
Return new reversed copy of the array.
sort(array[, order])
Sorts an array in ascending order. Optional order
argument can be used to specify the order of sorting: asc
or desc
.
sortBy(array, key[, order])
Sorts an array of maps by a specific key in ascending order. Optional order
argument can be used to specify the order
of sorting: asc
or desc
.
Map Functions
keys(map)
Returns an array containing the keys of the map.
values(map)
Returns an array containing the values of the map.
Type Conversion Functions
type(v)
Returns the type of the given value v
.
Returns on of the following types: nil
, bool
, int
, uint
, float
, string
, array
, map
.
For named types and structs, the type name is returned.
int(v)
Returns the integer value of a number or a string.
float(v)
Returns the float value of a number or a string.
string(v)
Converts the given value v
into a string representation.
toJSON(v)
Converts the given value v
to its JSON string representation.
fromJSON(v)
Parses the given JSON string v
and returns the corresponding value.
toBase64(v)
Encodes the string v
into Base64 format.
fromBase64(v)
Decodes the Base64 encoded string v
back to its original form.
toPairs(map)
Converts a map to an array of key-value pairs.
fromPairs(array)
Converts an array of key-value pairs to a map.
Miscellaneous Functions
len(v)
Returns the length of an array, a map or a string.
get(v, index)
Retrieves the element at the specified index from an array or map v
. If the index is out of range, returns nil
.
Or the key does not exist, returns nil
.
fallback(v1, v2[, …])
Returns the first non-nil value from the arguments given.
Bitwise Functions
bitand(int, int)
Returns the values resulting from the bitwise AND operation.
bitor(int, int)
Returns the values resulting from the bitwise OR operation.
bitxor(int, int)
Returns the values resulting from the bitwise XOR operation.
bitnand(int, int)
Returns the values resulting from the bitwise AND NOT operation.
bitnot(int)
Returns the values resulting from the bitwise NOT operation.
bitshl(int, int)
Returns the values resulting from the Left Shift operation.
bitshr(int, int)
Returns the values resulting from the Right Shift operation.
bitushr(int, int)
Returns the values resulting from the unsigned Right Shift operation.
Predicate
The predicate is an expression. It takes one or more arguments and returns a boolean value.
To access the arguments, the #
symbol is used.
If items of the array is a struct or a map, it is possible to access fields with
omitted #
symbol (#.Value
becomes .Value
).
Braces {
}
can be omitted:
$env
variable
The $env
variable is a map of all variables passed to the expression.
Literals
Comment | /* */ or // |
Boolean | true , false |
Integer | 42 , 0x2A |
Float | 0.5 , .5 |
String | ”foo” , ‘bar’ |
Array | [1, 2, 3] |
Map | {a: 1, b: 2, c: 3} |
Nil | nil |
Operators
Arithmetic | + , - , * , / , % (modulus), ^ or ** (exponent) |
Comparison | == , != , < , > , <= , >= |
Logical | not or ! , and or && , or or || |
Conditional | ?: (ternary), ?? (nil coalescing) |
Membership | [] , . , ?. , in |
String | + (concatenation), contains , startsWith , endsWith |
Regex | matches |
Range | .. |
Slice | [:] |
Pipe | | |