Syntax and Operators
Details on the syntax and operators available in conditional strings
Critical Moments uses the excellent expr library for conditional evaluation. Portions of this document are from their documentation, and those portions are under a MIT licence. Their original documentation is available here, and licensed here.
Portions of this document are extensions specific to Critical Moments, all rights reserved.
Literals
Comment |
|
Boolean |
|
Integer |
|
Float |
|
String |
|
Array |
|
Map |
|
Nil |
|
Operators
Arithmetic |
|
Comparison |
|
Logical |
|
Conditional |
|
Membership |
|
String |
|
Regex |
|
Range |
|
Slice |
|
Pipe |
|
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.
Optional chaining
The ?.
operator can be used to access a field of a struct or an item of a map without checking if the struct or the map is nil
. If the struct or the map is nil
, the result of the expression is nil
.
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, if 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. See more examples in Working with Dates.
now()
Returns the current date and time.
duration(str)
The duration function takes a string such as 1.5h
, 24h
, or -30s
. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
Durations can be added to datetimes like so: now() - duration('12h')
date(str[, format[, timezone]])
Converts the given string str
into a date.
str [required, string]: required, the date string to parse
format [optional, string]: the format of the date string. If omitted we'll try a variety of formats to try to parse the given string. Example:
date('2024-01-15T22:00:00-08:00', RFC3339)
. The following variables can be provided to parse for common standards.date_format:
YYYY-MM-DD
formatdate_and_time_format:
YYYY-MM-DD HH:MM:SS.SSSSSS
formatdate_with_tz_format: A
YYYY-MM-DDZ
date with a timezone. Example2021-01-01Z
for UTC or2021-01-01+05:00
for a 5 hour offsetRFC3339 (allowing fractional seconds)
RFC822
RFC850
RFC1123
RFC822Z: RFC822 with numeric zone like
-700
RFC1123Z: RFC1123 with numeric zone like
-700
Custom strings: supported using the golang time package time format described here
timezone [optional, string]: The timezone to parse in. Possible values include:
"UTC", empty string, or not specified - UTC
"Local": the user's local timezone
A location name corresponding to a file in the IANA Time Zone database, such as "America/New_York"
See examples in Working with Dates.
formatTime(timestamp, format, [timezone])
This function can be used to format timestamps, and extract components of a date. It returns the provided timestamp, formatted in the requested format. It returns an integer when possible (hour of day, month of year, etc) and a string when not possible (name of the month of the year).
Example: formatTime(now(), 'year') == 2024
Timestamp: a timestamp such as
now()
, the result of a call todate(...)
, or the result of a call to aunixTime*(...)
callFormat: a string format. One of the following strings:
"dow": the int day of week. Sunday is 0, Saturday is 6.
"dow_short": the string short form day of week. Mon, Tue, etc
"dow_long": the string full form day of week. Monday, Tuesday, etc
"dom": the int day of month (1-31)
"hod": the int hour of the day (0-23)
"moh": the int minut of the hour (0-59)
"ampm": "AM" or "PM"
"month": the int month. Jan is 1, Dec is 12.
"month_short": the short form name of month. Example: Jan
"month_long": the full form name of month. Example: January.
"year": the int year. Example, 1999
Timezone (optional): the timezone to apply to the timestamp before formatting. If not provided, defaults to the user's local timezone. Can be "Local" (user local), "UTC" or a location name corresponding to a file in the IANA Time Zone database, such as "America/Toronto".
unixTimeSeconds(int), unixTimeMilliseconds(int), unixTimeNanoseconds(int) Functions
These three functions take in a unix timestamp (time since January 1, 1970 UTC) and return a datetime for that point in time. Negative values are supported for times before 1970.
Be sure you've call the appropriate function. Different libraries provide unix time in seconds, milliseconds or nanoseconds.
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.
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:
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.
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)
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.
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.
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
.
Random Number Generation
Critical moments provides 4 methods for random number generation.
All 4 return a 63-bit non-negative integer. Typically you want to use modulo operator (%) on the result of these functions. Example condition which returns true 5% of the time rand() % 100 < 5
rand()
rand() returns a random number. It returns a new random number every time it is invoked.
sessionRand()
sessionRand() returns a random number which is stable for the duration of this session (held in memory until app restarted).
stableRand()
stableRand() returns a random number that is stable for the lifetime of this app install. It is generated on first use, and stored to disk. It's useful if you want to have a random number consistently return the same value for a user (example: opting into an AB test).
randForKey(stringSeed, intSeed)
randForKey returns pseudo random number given a key seed, and int seed. It returns the same number if provided the same inputs. It's useful in a number of cases:
For generating a stable random number across many devices using an input string or int:
randForKey('', user_id)
orrandForKey(user_email, 0)
For opting into AB tests, but shuffling for different experiments. For example,
randForKey('experiment1', stableRand()) % 100 == 0
andrandForKey('experiment2', stableRand()) % 100 == 0
will each return a consistent random int over time, but a different one than the other experiment.A combination of use cases (stable across device, and shuffling per use case):
randForKey(user_email + 'experiment1', 1)
Version Number Operators
Critical moments has a built in condition functions to help you compare version numbers.
Typically you don't want to use string comparisons on version numbers except for exact match checks. The check "11.0" > "2.0"
will evaluate to false with string comparisons since "1" < "2".
Version Number Format
Version numbers are represented as strings of semantic version numbers:
The simpliest format is
1.2.3
. This is our prefered style, and is used for built-in properties. There's no limit to the number of sub-versions you include.You can optionally prefix them with a v:
v1.2.3
You can optionally postfix them with a dash followed by text
v1.2.3-beta
versionGreaterThan , versionLessThan , versionEqual
These three functions can be used to compare 2 version number strings, and return a boolean result.
Version numbers with a postfix are considered less than otherwise equal version numbers without a postfix (v2.1-beta < v2.1), but we don't compare the values of the postfix strings (v2.1-alpha == v2.1-beta == v2.1-helloworld).
All three functions return false if passed an invalid version number. Be sure to pick the right operator to fail as intended if your version number strings might be nil or invalid.
function versionNumberComponent(versionString, componentInt)
This function can be used in condition strings to extract individual components of a version number as integers. For example versionNumberComponent("1.2.3", 1)
returns 2
.
Exact version checks
In some cases, like checking for a known buggy version number, exact string comparisons may be more succinct than versionEqual(a,b)
. Examples:
os_version == "16.4.1"
returns true if the os version is exactly 16.4.1 (string comparison)app_version in ["1.2.3", "2.3.1"]
returns true if the app version is in a set of exact strings
Query Event History
eventCount(eventName)
Fetches the number of times an event has occurred from the events database.
eventCountWithLimit(eventName, limitInt)
Fetches the number of times an event has occurred from the events database, but the max count retuned will be limitInt. This provides a small performance improvement if checking a minimum count on a high volume event.
latestEventTime(eventName)
Returns the latest datetime when this event occurred.
Function will return nil if that event was never fired. Example nil handling:
Nil coalescing, using now as default value
(lastEventTime('event_name') ?? now())
Ternary operator:
lastEventTime('event_name') == nil ? false : lastEventTime('event_name') < now() - duration('1h')
propertyEver(propNameString, value)
Returns a boolean indicating if a property has ever had a given value, using a local database tracking prior observed values.
The value can be a string, int, float, bool or date. The value must match the named property's type or it will return false.
For performance reasons, not all built in properties are tracked in the history, and they may sample at different times (app startup for some, lazily on access for others). Check this works for your use case locally before deploying.
Checking Installed Apps
canOpenUrl(urlString)
This function returns a boolean indicating if this app can open the provided url, which is useful for checking if other apps are installed. For example canOpenUrl('spotify:')
returns true if Spotify is installed.
Non-standard URL schemes need to be included in your app's info.plist. For URLs schemes not included, this function will return false.
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:
Last updated