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

# Built-in functions

> Complete reference for ZEN expression language built-in functions.

## Math functions

### abs

Returns the absolute value.

```
abs(-5)        // 5
abs(5)         // 5
```

### floor

Rounds down to the nearest integer.

```
floor(4.9)     // 4
floor(-4.1)    // -5
```

### ceil

Rounds up to the nearest integer.

```
ceil(4.1)      // 5
ceil(-4.9)     // -4
```

### round

Rounds to the nearest integer or decimal places.

```
round(4.5)         // 5
round(4.4)         // 4
round(4.567, 2)    // 4.57
```

### trunc

Truncates toward zero.

```
trunc(4.9)     // 4
trunc(-4.9)    // -4
```

### min

Returns the minimum value from an array.

```
min([5, 2, 8, 1])    // 1
```

### max

Returns the maximum value from an array.

```
max([5, 2, 8, 1])    // 8
```

### sum

Returns the sum of array values.

```
sum([1, 2, 3, 4, 5])    // 15
```

### avg

Returns the average of array values.

```
avg([10, 20, 30])    // 20
```

### median

Returns the median of array values.

```
median([1, 2, 3, 4, 5])    // 3
```

### mode

Returns the most frequent value.

```
mode([1, 2, 2, 3, 3, 3])    // 3
```

### rand

Returns a random number between 0 and the specified maximum.

```
rand(100)    // Random number 0-100
```

## String functions

### len

Returns the length of a string or array.

```
len("hello")       // 5
len([1, 2, 3])     // 3
```

### upper

Converts to uppercase.

```
upper("hello")    // "HELLO"
```

### lower

Converts to lowercase.

```
lower("HELLO")    // "hello"
```

### trim

Removes leading and trailing whitespace.

```
trim("  hello  ")    // "hello"
```

### contains

Checks if a string contains a substring, or array contains a value.

```
contains("hello world", "world")    // true
contains([1, 2, 3], 2)              // true
```

### startsWith

Checks if a string starts with a prefix.

```
startsWith("hello", "he")    // true
startsWith("hello", "lo")    // false
```

### endsWith

Checks if a string ends with a suffix.

```
endsWith("hello", "lo")     // true
endsWith("hello", "he")     // false
```

### matches

Tests a string against a regular expression.

```
matches("hello123", "[a-z]+[0-9]+")           // true
matches("123-456-7890", "[0-9]{3}-[0-9]{3}-[0-9]{4}")    // true
```

### extract

Extracts groups from a regular expression match.

```
extract("2024-01-15", "(\d{4})-(\d{2})-(\d{2})")
// ["2024-01-15", "2024", "01", "15"]
```

### split

Splits a string by delimiter.

```
split("a,b,c", ",")    // ["a", "b", "c"]
```

### fuzzyMatch

Returns a similarity score (0-1) between strings.

```
fuzzyMatch("hello", "hello")    // 1
fuzzyMatch("hello", "helo")     // 0.8
```

## Array functions

These functions iterate over arrays. Use `#` for the current element, or `as` for a named alias:

```
map(items, #.price)                              // using #
map(items as item, item.price)                   // using alias
filter(users as user, user.isActive)             // more readable
```

### map

Transforms each element.

```
map([1, 2, 3], # * 2)                       // [2, 4, 6]
map(items, #.price)                         // [prices...]
map(users as u, { name: u.name, age: u.age })
```

### filter

Keeps elements matching a condition.

```
filter([1, 2, 3, 4, 5], # > 3)      // [4, 5]
filter(items as item, item.price < 100)
```

### some

Returns true if any element matches.

```
some([1, 2, 3], # > 2)             // true
some(items, #.outOfStock)          // true if any out of stock
```

### all

Returns true if all elements match.

```
all([1, 2, 3], # > 0)              // true
all(items, #.verified)             // true if all verified
```

### one

Returns true if exactly one element matches.

```
one([1, 2, 3], # == 2)             // true
one([1, 2, 2], # == 2)             // false
```

### none

Returns true if no elements match.

```
none([1, 2, 3], # > 10)            // true
```

### count

Counts elements matching a condition.

```
count([1, 2, 2, 3, 3, 3], # == 3)    // 3
```

### flatMap

Maps and flattens results.

```
flatMap([[1, 2], [3, 4]], #)        // [1, 2, 3, 4]
```

### keys

Returns object keys or array indices.

```
keys({ a: 1, b: 2 })    // ["a", "b"]
keys([10, 20, 30])      // [0, 1, 2]
```

### values

Returns object values.

```
values({ a: 1, b: 2 })    // [1, 2]
```

### merge

Combines an array of arrays or an array of objects into a single result.

Arrays — concatenates all arrays into one:

```
merge([[1, 2], [3, 4], [5]])    // [1, 2, 3, 4, 5]
```

Objects — combines all objects, last value wins for duplicate keys:

```
merge([{a: 1}, {b: 2}, {c: 3}])        // {a: 1, b: 2, c: 3}
merge([{a: 1, b: 2}, {b: 3, c: 4}])    // {a: 1, b: 3, c: 4}
```

### mergeDeep

Recursively merges an array of objects. Unlike `merge`, nested objects are combined rather than replaced, and nested arrays are concatenated.

Nested objects — recursively merged:

```
mergeDeep([{a: {x: 1}}, {a: {y: 2}}])              // {a: {x: 1, y: 2}}
mergeDeep([{a: {b: {c: 1}}}, {a: {b: {d: 2}}}])    // {a: {b: {c: 1, d: 2}}}
```

Nested arrays — concatenated:

```
mergeDeep([{tags: [1, 2]}, {tags: [3, 4]}])    // {tags: [1, 2, 3, 4]}
```

Scalar values — last value wins:

```
mergeDeep([{a: 1}, {a: 2}])    // {a: 2}
```

## Date functions

### d

Creates a date object. See [Date operations](/learn/zen-language/dates) for full documentation.

```
d("2024-01-15")                         // Date object
d("2024-01-15", "America/New_York")     // With timezone
d()                                     // Current date/time
```

### duration

Parses a duration string to seconds.

```
duration("1h 30m")    // 5400
duration("7d")        // 604800
```

## Type functions

### string

Converts to string.

```
string(123)       // "123"
string(true)      // "true"
```

### number

Converts to number.

```
number("123")     // 123
number("12.5")    // 12.5
number(true)      // 1
number(false)     // 0
```

### bool

Converts to boolean.

```
bool(1)           // true
bool(0)           // false
bool("true")      // true
```

### type

Returns the type as a string.

```
type("hello")     // "string"
type(123)         // "number"
type(true)        // "bool"
type([1, 2])      // "array"
type({a: 1})      // "object"
type(null)        // "null"
```

### isNumeric

Checks if a value is numeric or can be converted to a number.

```
isNumeric(123)        // true
isNumeric("123")      // true
isNumeric("hello")    // false
```
