CEL functions in Traffic
This feature (and guide) only applies to static (preprocessed) sites generated by JAMstack frameworks. Web applications use our Traffic Filters feature instead.
Overview
Traffic encompasses extensions to CEL (common expression language), in particular functions for string manipulation. These are extremely useful for constructing rules. All of the available functions are described below. Note that all indices are zero-based.
If you’re unfamiliar with Traffic, please read our how-to guide first.
This guide was adapted from Google’s CEL-Go extension documentation
String functions
CharAt
Returns the character at the given position. If the position is negative, or greater than the length of the string, the function will produce an error:
Examples:
IndexOf
Returns the integer index of the first occurrence of the search string. If the search string is not found the function returns -1.
The function also accepts an optional position from which to begin the substring search. If the substring is the empty string, the index where the search starts is returned (zero or custom).
Examples:
LastIndexOf
Returns the integer index of the last occurrence of the search string. If the search string is not found the function returns -1.
The function also accepts an optional position which represents the last index to be considered as the beginning of the substring match. If the substring is the empty string, the index where the search starts is returned (string length or custom).
Examples:
LowerAscii
Returns a new string where all ASCII characters are lower-cased. This function does not perform Unicode case-mapping for characters outside the ASCII range.
Examples:
Replace
Returns a new string based on the target, which replaces the occurrences of a search string with a replacement string if present. The function accepts an optional limit on the number of substring replacements to be made.
When the replacement limit is 0, the result is the original string. When the limit is a negative number, the function behaves the same as replace all.
Examples:
Split
Returns a list of strings split from the input by the given separator. The function accepts an optional argument specifying a limit on the number of substrings produced by the split.
When the split limit is 0, the result is an empty list. When the limit is 1, the result is the target string to split. When the limit is a negative number, the function behaves the same as split all.
Examples:
Substring
Returns the substring given a numeric range corresponding to character positions. Optionally may omit the trailing range for a substring from a given character position until the end of a string.
Character offsets are 0-based with an inclusive start range and exclusive end range. It is an error to specify an end range that is lower than the start range, or for either the start or end index to be negative or exceed the string length.
Examples:
Trim
Returns a new string which removes the leading and trailing whitespace in the target string. The trim function uses the Unicode definition of whitespace which does not include the zero-width spaces. See: https://en.wikipedia.org/wiki/Whitespace_character#Unicode
Examples:
UpperAscii
Returns a new string where all ASCII characters are upper-cased.
This function does not perform Unicode case-mapping for characters outside the ASCII range.
Examples: