Introduction

Instead of reinventing the wheel, Compozy provides direct access to well-established JavaScript utility libraries within your template expressions:

  • Lodash - For general utility functions (_)
  • Voca - For string manipulation (v)
  • Day.js - For date operations (dayjs)
  • Numeral.js - For number formatting (numeral)

These libraries are available globally in your template expressions, providing battle-tested and well-documented functionality. Need additional libraries? You can add custom dependencies through the $ namespace (e.g., $.math, $.decimal). Check out Custom Dependencies to learn more.

Built-in Libraries

Lodash Functions

Available through the _ global variable.

items: "{{ _.chunk(['a', 'b', 'c', 'd'], 2) }}"
# Output: [['a', 'b'], ['c', 'd']]

unique: "{{ _.uniqBy(users, 'id') }}"
# Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]

Voca Functions

Available through the v global variable.

slug: "{{ v.slugify('Hello World!') }}"
# Output: hello-world

camel: "{{ v.camelCase('hello-world') }}"
# Output: helloWorld

Day.js Functions

Available through the dayjs global variable.

date: "{{ dayjs().format('YYYY-MM-DD') }}"
# Output: 2024-03-21

relative: "{{ dayjs(date).fromNow() }}"
# Output: 2 days ago

Numeral.js Functions

Available through the numeral global variable.

price: "{{ numeral(1234.5678).format('$0,0.00') }}"
# Output: $1,234.57

percentage: "{{ numeral(0.123).format('0.0%') }}"
# Output: 12.3%

Best Practices

# Good - Using lodash chaining
result:
  $exec: |
    return _(users)
      .filter('active')
      .map('name')
      .sort()
      .value();

Library Documentation

Each library provides extensive documentation for their functions:

Function Categories

For convenience, we’ve organized common operations into categories:

Next Steps