> ## Documentation Index
> Fetch the complete documentation index at: https://docs.compozy.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Template Functions

> Compozy leverages popular JavaScript libraries to provide a comprehensive set of utility functions for data manipulation within your template expressions.

## Introduction

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

* [Lodash](https://lodash.com/) - For general utility functions (`_`)
* [Voca](https://vocajs.com/) - For string manipulation (`v`)
* [Day.js](https://day.js.org/) - For date operations (`dayjs`)
* [Numeral.js](http://numeraljs.com/) - 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](/core/base-structure/custom-dependencies) to learn more.

## Built-in Libraries

### Lodash Functions

Available through the `_` global variable.

<CodeGroup>
  ```yaml Array Operations theme={"dark"}
  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' }]
  ```

  ```yaml Object Operations theme={"dark"}
  result:
    $exec: |
      return _.merge(defaultConfig, {
        theme: {
          mode: 'dark',
          colors: {
            primary: '#000'
          }
        }
      });
  ```

  ```yaml Collections theme={"dark"}
  grouped:
    $exec: |
      return _.groupBy(items, item =>
        item.price > 100 ? 'expensive' : 'affordable'
      );
  ```
</CodeGroup>

### Voca Functions

Available through the `v` global variable.

<CodeGroup>
  ```yaml String Manipulation theme={"dark"}
  slug: "{{ v.slugify('Hello World!') }}"
  # Output: hello-world

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

  ```yaml String Operations theme={"dark"}
  result:
    $exec: |
      const text = v.trim(description);
      return v.capitalize(v.truncate(text, 100));
  ```
</CodeGroup>

### Day.js Functions

Available through the `dayjs` global variable.

<CodeGroup>
  ```yaml Date Formatting theme={"dark"}
  date: "{{ dayjs().format('YYYY-MM-DD') }}"
  # Output: 2024-03-21

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

  ```yaml Date Manipulation theme={"dark"}
  future:
    $exec: |
      return dayjs()
        .add(1, 'month')
        .startOf('month')
        .format('YYYY-MM-DD');
  ```
</CodeGroup>

### Numeral.js Functions

Available through the `numeral` global variable.

<CodeGroup>
  ```yaml Number Formatting theme={"dark"}
  price: "{{ numeral(1234.5678).format('$0,0.00') }}"
  # Output: $1,234.57

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

  ```yaml Units theme={"dark"}
  filesize: "{{ numeral(1234567).format('0.0b') }}"
  # Output: 1.2MB

  metric: "{{ numeral(1000).format('0.0a') }}"
  # Output: 1.0k
  ```
</CodeGroup>

## Best Practices

<CodeGroup>
  ```yaml Chain Operations theme={"dark"}
  # Good - Using lodash chaining
  result:
    $exec: |
      return _(users)
        .filter('active')
        .map('name')
        .sort()
        .value();
  ```

  ```yaml Library Combination theme={"dark"}
  # Good - Combining libraries
  processed:
    $exec: |
      return _(items)
        .map(item => ({
          ...item,
          slug: v.slugify(item.title),
          date: dayjs(item.date).format('YYYY-MM-DD'),
          price: numeral(item.price).format('$0,0.00')
        }))
        .value();
  ```

  ```yaml Type Safety theme={"dark"}
  # Good - Using library utilities
  value: "{{ _.get(data, 'deeply.nested.path', defaultValue) }}"
  ```
</CodeGroup>

## Library Documentation

Each library provides extensive documentation for their functions:

* [Lodash Documentation](https://lodash.com/docs) - Complete reference of all available Lodash functions
* [Voca Documentation](https://vocajs.com/#documentation) - Comprehensive guide to string manipulation functions
* [Day.js Documentation](https://day.js.org/docs/en/installation/installation) - Date manipulation and formatting guide
* [Numeral.js Documentation](http://numeraljs.com/) - Number formatting guide

## Function Categories

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

* [String Functions](/core/template-engine/template-functions/string) - String manipulation examples using Voca
* [Array Functions](/core/template-engine/template-functions/array) - Array operations using Lodash
* [Object Functions](/core/template-engine/template-functions/object) - Object manipulation with Lodash
* [Date Functions](/core/template-engine/template-functions/date) - Date operations using Day.js

## Next Steps

<CardGroup cols={2}>
  <Card title="Template engine" icon="code" href="/core/template-engine/overview">
    Learn about template expressions
  </Card>

  <Card title="Custom Dependencies" icon="puzzle" href="/core/base-structure/custom-dependencies">
    Add custom libraries
  </Card>
</CardGroup>
