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, or regexp.

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 to ms.

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, or Date value.

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 when end is omitted.
  • end: Number, optional. The exclusive end value.
  • step: Number, optional. The increment. Defaults to 1, or -1 for 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 a to f.

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