Skip to content

Recipes: Math

Back to Recipes.

sum(items: list)

Returns the sum of all items in the list.

>>> sum([1, 2, 3, 5, 8])
19

avg(items: list)

Returns the average of all items in the list.

>>> avg([1, 2, 3, 5, 8])
3.8

round(value: number, precision: number)

Returns the value rounded to the specified precision.

>>> round(3.1415, 2)
3.14

floor(value: number, precision: number)

Returns the value rounded down to the specified precision.

>>> floor(3.1415, 2)
3.14

ceil(value: number, precision: number)

Returns the value rounded up to the specified precision.

>>> ceil(3.1415, 2)
3.15

max(items: list)

Returns the maximum of all items in the list.

>>> max([1, 2, 3, 5, 8])
8

min(items: list)

Returns the minimum of all items in the list.

>>> min([1, 2, 3, 5, 8])
1

sin(value: number, precision: number)

Returns the sine of the value as a number between -1 and 1.

>>> sin(30, 5)
0.500

cos(value: number, precision: number)

>>> cos(30, 5)
0.866

exp(value: number, precision: number)

>>> exp(1, 5)
2.718

pow(value: number, exponent: number)

>>> pow(2, 3)
8

sqrt(value: number, precision: number)

>>> sqrt(9, 5)
3.0000

abs(value: number)

>>> abs(-3)
3

vector(values: list)

>>> vector([[1, 2, 3]])
Vector {
[[1, 2, 3]]
}

rand(start: number, end: number)

>>> rand(1, 10)
6

len(value: string)

>>> len("hello")
5

substr(value: string, start: number, end: number)

>>> substr("hello", 1, 3)
ell

contains(value: string, search: string)

>>> contains("hello", "ell")
true

toUpperCase(value: string)

>>> toUpperCase("hello")
HELLO

random.password(minLength: number, maxLength: number)

>>> random.password(6, 8)
cR9mVzp6

len(value: object)

>>> len({"hello": "world", "foo": "bar"})
2

contains(value: object, key: string)

>>> contains({"hello": "world", "foo": "bar"}, "hello")
true

list(value: object)

>>> list([{"hello": "world", "foo": "bar"}])
ListWrapper([{"hello": "world", "foo": "bar"}])

Returns a new object with only the required keys for storing relations.

relation(value: object)

>>> relation({"id": 4, "module_id": 12, "hello": "world", "foo": "bar"})
{"id": 4, "module_id": 12}

join(glue: string, pieces: list)

>>> join(', ', ['a', 'b', 'c', 'd'])
'a, b, c, d'

today()

>>> today()
2018-09-12

now()

>>> now()
2018-09-12T13:22:02-05:00

date(value: string, timezone: string)

>>> date('2018-06-19')
Date {
date: 2018-09-12 00:00:00.0 UTC (+00:00)
}

duration(value: string)

>>> duration("1h30m")
90m

Returns the current user from local store. The user gets updated on every refresh.

user()

>>> user()
User {
id: 4,
module_id: 1,
email: "steffen@brezel.io",
name: "Steffen"
}

hasRole(role: string)

>>> hasRole("ROLE_ADMIN")
true

Returns whether the current user has a role ability, either built-in like api_keys or declared by the system. Also available as user().hasAbility(ability) in the API.

hasAbility(ability: string)

>>> hasAbility("export_pdf")
true

json.decode(value: string)

>>> json.decode("[1, 2, 3]")
[1, 2, 3]

json.encode(value: string)

>>> json.encode("[1, 2, 3]")
[1, 2, 3]

regex.quote(value: string, delimiter: string)

>>> regex.quote("-=hello world=-", "/")
\\-=hello\\ world\\=-

regex.matchAll(pattern: string, subject: string)

>>> regex.matchAll("hello (\\w+)", "hello world")
[
{ "0": "hello world", "1": "world" }
]