# Functions

A function changes a piece of text inside a task's field: it cuts a part out of a file name, replaces something, turns a date into another format, does a sum. Functions work together with [variables](Jobs-Variables.md), and in the same fields.

## How a function is written

A function has a start and an end, and works on what is between them:

```
{upper_start}{name}{upper_end}
```

gives the file's name in capitals. Some functions take values after the name, with colons:

```
{replace_start:_:-}{name}{replace_end}
```

replaces every `_` in the name with `-`.

- Functions can be inside each other. The inner one is worked out first.
- A colon inside a value is written `~..~`, because the colon separates the values: `{parse_start:HH~..~mm}` for the format `HH:mm`.
- A field that begins with `{ignore_functions}` is taken as it is: no function in it is run.

## Text

| Function | Does | Example | Result |
|---|---|---|---|
| `upper`, `lower` | capitals, small letters | `{lower_start}Report.PDF{lower_end}` | `report.pdf` |
| `trim` | takes spaces off both ends | | |
| `length` | the number of characters | `{length_start}blah{length_end}` | `4` |
| `replace:a:b` | every `a` becomes `b` | `{replace_start:test:big_test}a_test{replace_end}` | `a_big_test` |
| `substring:from:to` | the characters from one place to another, counted from 0. A negative number counts from the end; `-1` as the second means "to the end". | `{substring_start:2:5}something{substring_end}` | `met` |
| | | `{substring_start:-4:-1}something to find{substring_end}` | `find` |
| `split:by:n` | cuts the text at a separator and gives piece number n, counted from 0 | `{split_start:_:3}this_is_a_test_for_split{split_end}` | `test` |
| `indexof:text:from` | where a text is found, counted from 0; -1 when it is not there. A negative "from" searches backwards from the end. | `{indexof_start:thing:0}something{indexof_end}` | `4` |
| `1char` | takes the last character off | `{1char_start}a,b,c,{1char_end}` | `a,b,c` |
| `verifycontains:a;b` | `true` when the text holds one of the given texts | `{verifycontains_start:.csv;.txt}{name}{verifycontains_end}` | `true` |

## Paths and addresses

| Function | Does | Example | Result |
|---|---|---|---|
| `last` | the last part of a path | `{last_start}/a/b/file.txt{last_end}` | `file.txt` |
| `chop` | the path without its last part | `{chop_start}/a/b/c/{chop_end}` | `/a/b/` |
| `url:part` | one part of an address: `protocol`, `user`, `pass`, `host`, `port`, `path`, `file` or `query` | `{url_start:host}sftp://alex@files.example.com/in/a.txt{url_end}` | `files.example.com` |
| `encode`, `decode` | writes a text the way an address needs it (`%20` for a space), and back | | |
| `vrlencode`, `vrldecode` | the same for a user name or password inside an address (`{at}` for @) | | |

## Numbers

| Function | Does | Example | Result |
|---|---|---|---|
| `increment`, `decrement` | one more, one less | `{increment_start}50{increment_end}` | `51` |
| `add:n` | adds a whole number | `{add_start:500}1000{add_end}` | `1500` |
| `math:type` | works out a sum with `+ - * /`. The type is `i` whole number, `l` long whole number, `f` or `d` number with decimals; `d` if you give none. | `{math_start:l}5+5{math_end}` | `10` |

A counter that survives from run to run is a [UserVariable](Task-UserVariable.md) with a `PERSIST_` name and the value `{increment_start}{PERSIST_count}{increment_end}`.

## Dates

| Function | Does | Example |
|---|---|---|
| `parse:format` | reads a date written in the given format and gives it as milliseconds | `{parse_start:MMddyy HH~..~mm~..~ss}042578 22:43:12{parse_end}` |
| `rparse:format` | the other way: milliseconds as a date in the given format | `{rparse_start:yyyy-MM-dd}{time}{rparse_end}` |

The letters of a format are the ones of the [date variables](Jobs-Variables.md#dates-and-times). A format that begins with `=` is strict: `{parse_start:=MM/dd/yyyy}` refuses `13/02/2026` where the loose form would roll it over into the next year.

Together they turn one date format into another: parse the date in a file's name, then write it with `rparse`.

## Encoding and checks

| Function | Does |
|---|---|
| `base64` | the text as Base64 |
| `md5` | the MD5 checksum of the text, as `MD5:` and the value |
| `xml_encode`, `xml_decode` | writes `<`, `>` and `&` the way XML needs them, and back |
| `htmlclean`, `htmlclean2` | takes the HTML tags out of a text, in two ways. For putting the text of an HTML mail into a plain file. |
| `sql` | makes a value safe to put into an SQL statement: quotes and percent signs become `_`, a semicolon a space |
| `checkemail:domains` | `true` when every address in the text is at one of the given domains: `{checkemail_start:example.com;example.org}{to}{checkemail_end}` |

## Functions that need a right, or a setting

These reach outside the text. A job saved by an admin with limited rights may use one only when the admin's role allows it: see [Who may see, change and run a job](Jobs-Access.md).

| Function | Does | Needs |
|---|---|---|
| `log` | writes the text into the server's log | |
| `encrypt`, `decrypt` | encrypts a text with the server's own key, and back | the right to use the server's keys |
| `geoip` | looks up where an IP address is: `{geoip_start}{user_ip}{geoip_end}` | the lookup service set up in the [Preferences](Prefs.md) |
| `base64url` | fetches what is at an address and gives it as Base64, for a picture in a mail | the server settings `base64url_allowed_url_patterns` (which addresses) and `base64url_fetch_max_size` |
| `group:property:group of users:separator` | one property of every member of a group, as a list: `{group_start:email:MainUsers:,}Sales{group_end}` gives the mail addresses of the group Sales | the right to see those users |

## Only in the UserVariable task

| Function | Does |
|---|---|
| `save_list` | keeps a copy of the list of items under a name |
| `load_list` | replaces the list of items with a kept one |
| `add_list` | adds the item in hand to a kept list |
| `json_parse` | reads a JSON text and makes a variable of each of its values: `{json_parse_start}{body}{json_parse_end}`, then `{status}` for the value named status. One level only. |
| `xml_parse` | the same for XML |
| `properties_parse` | the same for lines of `name=value` |

See [UserVariable](Task-UserVariable.md).

## The order

Functions are worked out in a fixed order, the order of the tables above from top to bottom being close to it: encryption and encoding first, then text, numbers, dates, addresses, checks, and `log` last. With functions of different kinds side by side this does not matter. When the result of one shall go into another, put it **inside** the other.

## What a value cannot do

Variables are filled in before functions run, and what a variable brings in is data. A file whose name is `{math_start}1+1{math_end}.txt` keeps that name: the function is not run. Only functions that you typed into the task run.

## Every function, by the alphabet

[Task functions](Ref-Task-Functions.md) is the index.

## Trying one

`POST /tests/job-variable` of the [API](API-Tests.md) takes a line and answers what it becomes.
