Global helper functions

You can use any of the global helper functions listed below when developing your templates. These functions can be used with any of our platform connectors.

$parseJSON

Receives a string as input (in stringified JSON format) and returns a JSON object.

Template compatibility: All

Input

Any string

Examples

$parseJson('{"foo": "bar"}')
=> { foo: 'bar' }

$or

Coalesces any number of values. Equivalent to the || (logical or) operator. It returns the first value that is not falsy. If all values are falsy, it returns the last one.

Template compatibility: All

Input

Any number of any arguments

Examples

$or(1, 2)
=> 1

$or(null, 3)
=> 3

$or(null, '')
=> ''

$includes

Returns a boolean indicating if a value is contained within an array. Equivalent of [].includes(payload).

Template compatibility: All

Input

[an array, any value]

Examples

$includes(['foo', 'bar'], 'foo')
=> true

$includes(['foo', 'bar'], 'baz')
=> false

$deepMutate

Deeply mutates an object with the payload. Instead of creating new memory, this directly updates a reference that already exists, thus being more performant and useful for heavy computational cases.

Template compatibility: All

Input

[an object, an object]

Examples

$person := {
  "name": "Bob",
  "age": 25,
  "metrics": {
    "height": 170,
    "weight": 50
  }
};

$deepMutate($person, {
  "name": "Alice",
  "metrics": {
    "weight": 40
  }
});

=> {
  "name": "Alice",
  "age": 25,
  "metrics": {
    "height": 170,
    "weight": 40
  }
}

$forceReturnArray

Forces any return type to be an array. If an array is provided, it won't be changed.

Template compatibility: All

Input

Anything

Examples

$forceReturnArray(42)
=> [42]

$forceReturnArray([42])
=> [42]

$kebabCase

Converts a string into the kebab-case format. Note that words and numbers will be separated based on casing.

Template compatibility: All

Input

A string

Examples

$kebabCase('The quick brown fox jumps over the lazy dog')
=> 'the-quick-brown-fox-jumps-over-the-lazy-dog'

$kebabCase('TheAnswerIs42')
=> 'the-answer-is-42'

$flattenArray

Recursively flattens an array. If the payload is not an array, it'll be converted to one.

Template compatibility: All

Input

An array

Examples

$flattenArray([['foo'], ['bar', ['baz']]])
=> ['foo', 'bar', 'baz']

$flattenArray(42)
=> [42]

$objectValues

Returns the values of an object as an array. Returns an empty array if the payload is falsy.

Template compatibility: All

Input

An object

Examples

$objectValues({"foo": "bar", "baz": "qux"})
=> ['bar', 'qux']

$objectValues(null)
=> []

$getHTMLText

Extracts the text from an HTML string, such as a product description.

Note that this will limit the string to 1000 characters.

Template compatibility: All

Input

A string

Examples

$getHTMLText('<p>This is a <bold>text</bold></p>')
=> 'This is a text'

$truncateFacet

Truncates the value of a facet to its limit of 200 characters. The facet value type will be kept, either as a string or an array of strings.

If the value is an array, each entry will be truncated separately.

Template compatibility: All

Input

A string or an array of strings

Examples

$truncateFacet('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam imperdiet ac magna non venenatis. Nunc faucibus maximus accumsan. Donec consequat nulla eu elit sagittis sodales. Aliquam dignissim magna id convallis placerat. Cras vitae scelerisque nibh.')
=> 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam imperdiet ac magna non venenatis. Nunc faucibus maximus accumsan. Donec consequat nulla eu elit sagittis sodales. Aliquam dignissim magna'

$truncateFacet(['foo', 'bar'])
=> ['foo', 'bar']