Pipeline Functions
In this section, we will explore how to effectively use Pipeline Functions.
Usage Cases
Pipeline functions share some similarities with data transformation functions, which we will introduce later. In certain situations, these functions overlap and can be used interchangeably. Pipeline functions are versatile and can be used for data conversion, simple arithmetic operations, date formatting, and more.
You can type |
to invoke the variable panel and add multiple pipeline functions. For example:
{{@vars.Price|toNumber()|add(1)}}
When a pipeline function does not require arguments, you can omit the parentheses ()
. For example:
{{@vars.Price|toNumber|add(1)}}
Next, we will introduce the usage of each function.
add(num)
Performs addition.
Arguments
value
: Number | String, input valuenum
: Number, the number to add
Returns
- Number, the result
- Any, returns the original value if it cannot be converted to a number
Examples
Example 1: When @vars.Price = 1
, this expression is equivalent to 1 + 2
, resulting in 3
.
{{@vars.Price|add(2)}} // 3
sub(num)
Performs subtraction.
Arguments
value
: Number | String, input valuenum
: Number, the number to subtract
Returns
- Number, the result
- Any, returns the original value if it cannot be converted to a number
Examples
Example 1: When @vars.Price = 1
, this expression is equivalent to 1 - 1
, resulting in 0
.
{{@vars.Price|sub(1)}} // 0
mul(num)
Performs multiplication.
Arguments
value
: Number | String, input valuenum
: Number, the multiplier
Returns
- Number, the result
- Any, returns the original value if it cannot be converted to a number
Examples
Example 1: When @vars.Price = 2
, this expression is equivalent to 2 * 2
, resulting in 4
.
{{@vars.Price|mul(2)}} // 4
div(num)
Performs division.
Arguments
value
: Number | String, input valuenum
: Number, the divisor
Returns
- Number, the result
- Any, returns the original value if it cannot be converted to a number
Examples
Example 1: When @vars.Price = 2
, this expression is equivalent to 2 / 2
, resulting in 1
.
{{@vars.Price|div(2)}} // 1
odd()
Transforms the value into an odd number.
Examples
Example 1: When @vars.Price = 2
, this expression is equivalent to 2 * 2 + 1
, resulting in 5
.
{{@vars.Price|odd()}} // 5
even()
Transforms the value into an even number.
Examples
Example 1: When @vars.Price = 2
, this expression is equivalent to 2 * 2 + 2
, resulting in 6
.
{{@vars.Price|even()}} // 6
abs()
Returns the absolute value.
Examples
Example 1: When @vars.Price = 2
, this expression is equivalent to |2|
, resulting in 2
.
{{@vars.Price|abs()}} // 2
Example 2: When @vars.Price = -2
, this expression is equivalent to |-2|
, resulting in 2
.
{{@vars.Price|abs()}} // 2
ceil()
Rounds the value up to the nearest integer.
Examples
Example 1: When @vars.Price = 2.1
, the result is 3
.
{{@vars.Price|ceil()}} // 3
floor()
Rounds the value down to the nearest integer.
Examples
Example 1: When @vars.Price = 2.9
, the result is 2
.
{{@vars.Price|floor()}} // 2
round()
Rounds the value to the nearest integer.
Examples
Example 1: When @vars.Price = 2.5
, the result is 3
.
{{@vars.Price|round()}} // 3
Example 2: When @vars.Price = 2.3
, the result is 2
.
{{@vars.Price|round()}} // 2
trim()
Trims whitespace from the beginning and end of a string.
Examples
Example 1: When @vars.Name = " abc "
, the result is "abc"
.
{{@vars.Name|trim()}} // "abc"
toUpper()
Converts the string to uppercase.
Examples
Example 1: When @vars.Name = "abc"
, the result is "ABC"
.
{{@vars.Name|toUpper()}} // "ABC"
toLower()
Converts the string to lowercase.
Examples
Example 1: When @vars.Name = "ABC"
, the result is "abc"
.
{{@vars.Name|toLower()}} // "abc"
urlencode()
Encodes the string for use in a URL, typically for query parameters.
Examples
Example 1: When @vars.Name = "中文"
, the result is "%E4%B8%AD%E6%96%87"
.
{{@vars.Name|urlencode()}} // "%E4%B8%AD%E6%96%87"
capitalize()
Capitalizes the first letter of the string.
Examples
Example 1: When @vars.Name = "abc"
, the result is "Abc"
.
{{@vars.Name|capitalize()}} // "Abc"
split(separator)
Splits a string into an array.
Examples
Example 1: When @vars.Name = "a,b,c"
, the result is [ "a", "b", "c" ]
.
{{@vars.Name|split(',')}} // [ "a", "b", "c" ]
extract(regexp)
Extracts a substring from the string using a Regular Expression.
Examples
Example 1: When @vars.Name = "abc"
, the result is "ab"
.
{{@vars.Name|extract(/ab/g)}} // "ab"
replace(regexp, text)
Replaces matched characters using a Regular Expression.
Examples
Example 1: When @vars.Name = "abc"
, the result is "ccc"
.
{{@vars.Name|replace(/ab/g, 'cc')}} // "ccc"
size()
Returns the number of elements in an array or the length of a string.
Examples
Example 1: When @vars.Name = "abc"
, the result is 3
.
{{@vars.Name|size()}} // 3
Example 2: When @vars.Names = [ "abc", "def" ]
, the result is 2
.
{{@vars.Names|size()}} // 2
at(index?)
Retrieves a specific element from an array or a specific character from a string.
Examples
Example 1: When @vars.Name = "abc"
, the result is "b"
.
{{@vars.Name|at(1)}} // "b"
Example 2: When @vars.Names = [ "abc", "def" ]
, the result is "abc"
.
{{@vars.Names|at(0)}} // "abc"
Example 3: When @vars.Names = [ "abc", "def" ]
, if the argument is omitted, a random element is selected.
{{@vars.Names|at()}} // "abc" or "def"
slice(start, end)
Extracts a portion of an array or a substring.
Examples
Example 1: When @vars.Names = [ "a", "b", "c", "d", "e" ]
, the result is [ "a", "b", "c" ]
.
{{@vars.Names|slice(0, 3)}} // [ "a", "b", "c" ]
join(separator)
Joins an array into a string.
Examples
Example 1: When @vars.Names = [ "a", "b", "c", "d", "e" ]
, the result is "abcde"
.
{{@vars.Names|join('')}} // "abcde"
concat(array)
Merges arrays.
Examples
Example 1: When @vars.Names01 = [ "a", "b" ]
and @vars.Names02 = [ "c", "d" ]
, the result is [ "a", "b", "c", "d" ]
.
{{@vars.Names01|concat(@vars.Names02)}} // [ "a", "b", "c", "d" ]
sort()
Sorts an array.
Examples
Example 1: When @vars.Names = [ "a", "c", "b" ]
, the result is [ "a", "b", "c" ]
.
{{@vars.Names|sort()}} // [ "a", "b", "c" ]
unique()
Removes duplicate values from an array.
Examples
Example 1: When @vars.Names = [ "a", "b", "b" ]
, the result is [ "a", "b" ]
.
{{@vars.Names|unique()}} // [ "a", "b" ]
compact()
Removes falsy values from an array. Values such as ""
, 0
, false
, undefined
, null
, and NaN
are considered falsy.
Examples
Example 1: When @vars.Names = [ "a", "", "b" ]
, the result is [ "a", "b" ]
.
{{@vars.Names|compact()}} // [ "a", "b" ]
random(min?, max?)
Randomizes an array.
Examples
Example 1: When @vars.Names = [ "a", "b", "c" ]
, the result is [ "c", "a", "b" ]
.
{{@vars.Names|random()}} // [ "c", "a", "b" ]
Example 2: When @vars.Names = [ "a", "b", "c" ]
, the result is [ "a", "c" ]
.
{{@vars.Names|random(0, 2)}} // [ "a", "c" ]
format(template)
Formats a date. For more details, refer to Day.js.
Examples
Example 1: When @vars.Date = "2024-10-12T07:37:55.568Z"
, the result is "2024-10-12 15:37:55"
.
{{@vars.Date|format("YYYY-MM-DD HH:mm:ss")}} // "2024-10-12 15:37:55"
toString()
Converts a value to a string.
Examples
Example 1: When @vars.Age = 18
, the result is "18"
.
{{@vars.Age|toString()}} // "18"
toNumber()
Converts a value to a number.
Examples
Example 1: When @vars.Age = "24"
, the result is 24
.
{{@vars.Age|toNumber()}} // 24
toBoolean()
Converts a value to a boolean. Values such as ""
, 0
, false
, undefined
, null
, and NaN
are considered falsy.
Examples
Example 1: When @vars.Age = "24"
, the result is true
.
{{@vars.Age|toBoolean()}} // true
Example 2: When @vars.Age = ""
, the result is false
.
{{@vars.Age|toBoolean()}} // false
toArray()
Converts a value to an array.
Examples
Example 1: When @vars.Names = '[ "a", "b" ]'
, the result is [ "a", "b" ]
.
{{@vars.Names|toArray()}} // [ "a", "b" ]
toJSON()
Converts a string to a JSON object.
Examples
Example 1: When @vars.str = '{ "foo": "bar" }'
, the result is { "foo": "bar" }
.
{{@vars.str|toJSON()}} // { "foo": "bar" }