Helper Functions
In this section, we will explore how to use Helper Functions effectively.
Helper Functions are referenced through template syntax. If you're not familiar with this, please first refer to the Template Syntax section.
Helper arguments are evaluated as template expressions. When a Duck value is passed to a helper, the helper receives its payload value; use .value or .target when you need to access the wrapper explicitly.
Available Functions
Tapicker provides built-in helper functions that can be called directly by name. The currently available functions are:
- getPageURL()
- getPageTitle()
- getPageVar(path)
- getValueType(value)
- getTimestamp(value?, unit?)
- getDate(value?)
- getRange(start, end?, step?)
- getUUID(separator?)
- getRandomId(length?)
- getRandomNum(min, max, floating?)
- getSimilarity(str1, str2)
Note that arguments followed by a ? mark, such as value?, are optional.
Next, we'll go through each function, explaining their purposes and how to use them.
getPageURL()
Retrieves the URL of the current page.
Arguments
- None
Returns
- String: The URL of the current page
Examples
-
If you are visiting Google, this will return:
{{getPageURL()}} π https://www.google.com/
getPageTitle()
Retrieves the title of the current page. This is a shortcut for getPageVar('document.title').
Arguments
- None
Returns
- String: The title of the current page
Example
{{getPageTitle()}} π "Google"
getPageVar(path)
Retrieves a variable from the current page.
Arguments
path: String, required. It is the access path of the variable.
Returns
- Any: The value of the variable. If the variable cannot be accessed, it returns
undefined.
Examples
-
Retrieve the page title:
{{getPageVar("document.title")}} π βGoogleβ -
Retrieve the host of the page URL:
{{getPageVar("location.host")}} π βwww.google.comβ -
Retrieve the language of the page:
{{getPageVar("navigator.language")}} π "en-US"
getValueType(value)
Returns the normalized type of a value.
Arguments
value: Any, the value to inspect.
Returns
- String: One of
undefined,null,string,number,boolean,array,object,date, orregexp.
Examples
{{getValueType($vars.value)}} π "number"
{{getValueType(getDate())}} π "date"
getTimestamp(value?, unit?)
Gets the timestamp of the current time or a specific time, representing the number of milliseconds since 1970-01-01 00:00:00 to the target time.
Arguments
value: String | Number | Date, optional. The date or timestamp to convert.unit:ms|s, optional. The output unit. Defaults toms.
Returns
- Number: The timestamp in the selected unit.
Examples
-
Get the timestamp of the current time:
{{getTimestamp()}} π 1725031203658 -
Get the timestamp of a specific date:
{{getTimestamp('2024-08-28T18:18:28')}} π 1724840308000 -
Get a timestamp in seconds:
{{getTimestamp('2024-08-28T18:18:28', 's')}} π 1724840308
getDate(value?)
Retrieves the date object for the current time or a specific time.
Arguments
value: String | Number | Date, optional. A date string, timestamp, orDatevalue.
Returns
- Date: The date object for the specified time.
Examples
-
Get the date object for the current time:
{{getDate()}} π 2024-08-30T15:20:03.658Z -
Get the date object for a specific date:
{{getDate('2024-08-28T18:18:28')}} π 2024-08-28T10:18:28.000Z -
Get a date object from a timestamp:
{{getDate(1724840308000)}} π 2024-08-28T10:18:28.000Z -
Format a date object into a specific format:
{{getDate(1724840308000)|format("YYYY/MM/DD")}} π 2024/08/28
getRange(start, end?, step?)
Generates a number sequence. The end value is exclusive. If end is omitted, the sequence starts at 0.
Arguments
start: Number, required. The start value, or the end value whenendis omitted.end: Number, optional. The exclusive end value.step: Number, optional. The increment. Defaults to1, or-1for a descending range.
Examples
{{getRange(1, 5)}} π [1, 2, 3, 4]
{{getRange(5, 1, -1)}} π [5, 4, 3, 2]
getUUID(separator?)
Generates a standard UUID or a UUID with a custom separator.
Arguments
separator: String, optional. The separator character.
Returns
- String: The generated UUID.
Examples
-
Generate a standard UUID:
{{getUUID()}} π "d58c8f4b-0375-41d0-adc2-f33f56c0e16c" -
Generate a UUID with a custom separator:
{{getUUID("#")}} π "d58c8f4b#0375#41d0#adc2#f33f56c0e16c" -
Generate a UUID with no separator:
{{getUUID("")}} π "d58c8f4b037541d0adc2f33f56c0e16c"
getRandomId(length?)
Generates a random ID of a specified length.
Arguments
length: Number, optional. The length of the ID.
Returns
- String: A random string composed of digits and characters from
atof.
Examples
-
Generate an ID with the default length of 8:
{{getRandomId()}} π "dd9a7d68" -
Generate an ID with a length of 24:
{{getRandomId(24)}} π "45082a6f856340d3bfd342b9"
getRandomNum(min, max, floating?)
Generates a random number within a specified range, inclusive of min and max.
Arguments
min: Number, required. The minimum value.max: Number, required. The maximum value.floating: Boolean, optional. Whether to return a floating-point number.
Returns
- Number: A random number within the specified range.
Examples
-
Generate a random number between 1 and 10:
{{getRandomNum(1, 10)}} π 6
getSimilarity(str1, str2)
Get the similarity between two strings.
Arguments
str1: String, required. The string 1.str2: String, required. The string 2.
Returns
- Number: Returns a number between 0 and 1, the closer to 1 the more similar they are.
Examples
-
Get the similarity between the strings "hello" and "heylo":
{{getSimilarity("hello", "heylo")}} π 0.5