Function Tests

These tests test whether Mavo functions produce correct results. Remember, MavoScript doesn't support {} for object literals, either use group() or $fn. in front of every function you call.

Basic Operators

addition("1", 1) 2
addition("", "") 0
addition("3", "5") 8
addition([1, 2, 3]) 6
addition([1, 2, 3], [4, 5]) [5, 7, 3]
subtract(1, 2) -1
subtract(1, -2) 3
subtract(1, 0) 1
minus(1) -1
divide(4, 2) 2
divide([4,2], [2]) [2,2]
divide([4], [2,2]) [2,2]
and(0, true) 0
and([0, 1, 2, 3], [true, true, false]) [0, true, false, false]
and([true, true, false], [0, 1, 2, 3]) [0, 1, false, false]
or(true, false, false) true
or(false, false, false) false
eq(3, 3, 3, 3) true
eq("l","l") true
gt(3, 2, 1) true
lt(1, 2, 3) true
lt(1, 2, 1) false
gt(3, 2, 3) false
gt("2090-06-13", $now) true
gt("1990-06-13", $now) false
neq(2, 2) false
neq(foo, bar) true
neq(2, 2, 3) false

Multi-valued Arguments

Unary Functions

ordinal([1,2,3,4]) ["st","nd","rd","th"]
len(["foobar", 314]) [6,3]
idify(["Foo bar", "BarBaz"]) ["foo-bar","barbaz"]
readable(["foo-bar", "BarBaz"]) ["Foo bar","Bar baz"]
uppercase(["foobar", "bar baz"]) ["FOOBAR","BAR BAZ"]
lowercase(["FOOBAR", "BAR BAZ"]) ["foobar","bar baz"]
filename(["mavo.io/foo.html", "mavo.io/bar?q=baz"]) ["foo.html","bar"]

Unary Math Functions

abs([-2,3]) [2,3]
floor([3.5,4.9]) [3,4]
sqrt([16,25]) [4,5]

Binary Functions

atan2([0,5], [2,5]) * 180 / PI [0,45]
atan2([0,5], [2,5,0]) * 180 / PI [0,45,0]
atan2([0,5,0], [2,5]) * 180 / PI [0,45,0]
pow([2,3], [3,2]) [8,9]
pow([3], [2,2]) [9,1]
pow([3,4], [2]) [9,4]
imul([2,3], [4,5]) [8,15]
imul([2], [4,5]) [8,5]
imul([2,3], [4]) [8,3]
round([5.85, -5.05], 0) [6,-5]
round([5.85, -5.05, 6.5], [1, 0]) [5.9,-5,7]
round([5.85, -5.05], [1, 0, 3]) [5.9,-5,3]
search([34, "foobar"], [3, bar]) [0,3]
search([34, "foobar", "baz"], [3, "bar"]) [0,3,-1]
search([34, "foobar"], [3, bar, "baz"]) [0,3,-1]
starts([34, "foobar"], [3, bar]) [true,false]
starts([34, "foobar", "baz"], [3, "bar"]) [true,false,false]
starts([34, "foobar"], [3, bar, "baz"]) [true,false,false]
ends([34, "foobar"], [3, bar]) [false,true]
ends([34, "foobar", "baz"], [3, "bar"]) [false,true,false]
ends([34, "foobar"], [3, bar, "baz"]) [false,true,false]
from(["foo.bar", "barbaz"], [".", "b"]) ["bar","arbaz"]
from(["foo.bar", "barbaz", "foobar"], [".", "b"]) ["bar","arbaz","foobar"]
from(["foo.bar", "barbaz"], [".", "b", "foobar"]) ["bar","arbaz",""]
fromlast(["foo.bar", "barbaz"], [".", "b"]) ["bar","az"]
fromlast(["foo.bar", "barbaz", "foobar"], [".", "b"]) ["bar","az","foobar"]
fromlast(["foo.bar", "barbaz"], [".", "b", "foobar"]) ["bar","az",""]
to(["foo.bar", "barbaz"], [".", "a"]) ["foo","barb"]
to(["foo.bar", "barbaz", "foobar"], [".", "a"]) ["foo","barb","foobar"]
to(["foo.bar", "barbaz"], [".", "a", "foobar"]) ["foo","barb",""]
tofirst(["foo.bar", "barbaz"], [".", "a"]) ["foo","b"]
tofirst(["foo.bar", "barbaz", "foobar"], [".", "a"]) ["foo","b","foobar"]
tofirst(["foo.bar", "barbaz"], [".", "a", "foobar"]) ["foo","b",""]
split(["foo.bar", "bar,baz"], [".", ","]) [["foo", "bar"], ["bar", "baz"]]
split(["foo.bar", "bar.baz"], ".") [["foo", "bar"], ["bar", "baz"]]
split(["foo bar", "bar baz"]) [["foo", "bar"], ["bar", "baz"]]

Polynary Functions

random(1, 1, 1) 1
random([1,2,3], [1,2,3], 1) [1,2,3]
random([1,2], [1,2,0], 1) [1,2,0]
between(["foo.bar!baz", "foo!bar!baz"], [".", "!"], "!") ["bar","bar"]
digits([2, 1], [2, 4], 3.1415) ["03.14", "3.1415"]
replace(["foobar", "barbaz"], ["oo", "bar"], "aa") ["faabar", "aabaz"]

split()

split("afbfc", "f") ["a", "b", "c"]
split("a b", " ") ["a", "b"]

group()

group(name: 'Lea', age: 32) {"name":"Lea","age":32}

list()

list(1, 2, 3) [1, 2, 3]
list() []
list(0) [0]
list([1, 2], [2, 3]) [1, 2, 2, 3]
list([1, 2], 2) [1, 2, 2]
list([1, 2]) [1, 2]

Aggregates

average() 0
average(1) 1
average(null, 1, 2, 3, null, "") 2
average([null, 1, 2, 3, null, ""]) 2
median() 0
median(1) 1
median([1, 2]) 1.5
median([null, 1, 2, 3, null, ""]) 2

iff()

iff(4, 1, 2) 1
iff([1, 0], 1, 2) [1, 2]
iff([1, 0], [1, 2], [3, 4]) [1, 4]
iff([true, false, 0, 1], 1, [2, 3]) [1, 3, 3, 1]

Concatenation

"yolo" & 2 "yolo2"
[1, "hello"] & "#" ["1#", "hello#"]
[1, 2, 3] & "#" & ["a", "b", "c"] ["1#a", "2#b", "3#c"]

get()

$fn.get(bar: 4, "bar") 4
$fn.get(group(), "bar") null
$fn.get(null, "bar") null
$fn.get(group(foo: 1, foO: 2), "FOO") 1
$fn.get(group(foo: 1, foO: 2), "foO") 2
$fn.get(foo: undefined, "foo") undefined
$fn.get(group(foo: undefined, foO: 1), "foo") undefined
$fn.get([group(id: "foo", name: "fooname"), group(id: "bar", name: "barname")], "id=foo").name "fooname"
$fn.get([group(id: "foo", name: "fooname"), group(id: "bar", name: "barname")], "id=bar").name "barname"
$fn.get([group(id: "foo", name: "fooname"), group(id: "bar", name: "barname")], "name=fooname") [{"id":"foo","name":"fooname"}]
$fn.get([group(id: "foo", name: "fooname"), group(id: "bar", name: "barname")], "name") ["fooname", "barname"]
$fn.get($fn.get([group(id: "foo", name: "fooname"), group(id: "bar", name: "barname")], "name=barname"), "id") ["bar"]

String slicing

from("foo", ".") ""
from("foo.bar", ".") "bar"
from("foo.bar.baz", ".") "bar.baz"
fromlast("foo.bar.baz", ".") "baz"
to("foo", ".") ""
to("foo.bar", ".") "foo"
to("foo.bar.baz", ".") "foo.bar"
tofirst("foo.bar.baz", ".") "foo"
between("foo.bar.baz", ".", ".") "bar"
between("foo.bar.baz", "#", "$") ""

contains()

$fn.contains(group(name: "Kenneth", work: "MIT", interests: [group(food: "rice", type: "white"), "coding"]), "rice") true
$fn.contains(["Lea", "Kenneth", "Amy",], "Kenneth") [false, true, false]
$fn.contains(["Kenneth", "Amy", group(name: "Kenneth")], "Kenneth") [true, false, true]
$fn.contains(["Kenneth", [6, "Kenneth"], "MIT"], "Kenneth") [true, true, false]
$fn.contains(group(name: "Kenneth", work: "MIT", interests: [group(food: "rice"), "coding"]), group(food: "rice")) true
$fn.contains(["Kenneth", "MIT", group(food: "rice", type: "white")], group(food: "rice", type: "white")) [false, false, true]
$fn.contains("Kenneth", ["Kenneth", "Lea"]) [true, false]
$fn.contains(["Kenneth", "Verou"], ["Kenneth", "Lea"]) [true, false]
$fn.contains(["Kenneth", "Lea", "Amy"], ["Kenneth", "Lea"]) [true, true, false]
contains(["Kenneth", "Lea"], ["Kenneth", "Lea", "Amy"]) [true, true, false]
$fn.contains([["Kenneth", "Lea"], "Amy"], ["Kenneth", "Lea"]) [true, false]
$fn.contains(group(name: "Kenneth", work: "MIT", interest: ["coding", group(food: "rice", quantity: 6)]), ["Kenneth", "rice"]) [true, true]
$fn.contains(["Kenneth", group(food: "rice", quantity: 6), "MIT"], ["Kenneth", "6"]) [true, true, false]
contains(null, null) false
contains("foo", null) false
contains(null, "foo") false
contains() false
contains("foo") false
contains("0", 0) true
contains([], "foo") []

filter()

filter(42, false) null
filter(42, true) 42
filter(42) 42
filter([1, 2]) [1, 2]
filter([1, 2, 3], [false, true, false]) [null, 2, null]
filter([1, 2, 3], [true, true, false], [false, true, false]) [null, 2, null]
filter([1, 2, 3, 2], 2) [null, 2, null, 2]
filter([1, 2, 3, 4], [true, false, true]) [1, null, 3, null]
filter([1, 2, 3], [true, false, true, true]) [1, null, 3]
filter([1, 2, 3, 4], [true, false, true], 3) [null, null, 3, null]
filter([1, 2, 3, 4], 3, [true, false, true]) [null, null, 3, null]

has()

has(1, [1, 2, 3]) true
$fn.has(foo: 1, [foo: 1, 2, 3]) true
has(1, 1) true
has(1, 2) false
has(1, [numberObject1, 2, 3]) true
has([1, 4], [1, 2, 3]) [true, false]
$fn.has('foo', foo: 1) true
$fn.has(['foo', 'bar'], foo: 1) [true, false]

replace()

replace("foooo", "o", "aa") "faaaaaaaa"
replace(["bar", "baz"], "a", "o") ["bor", "boz"]
replace("foooo", "oo", "o") "foo"
replace("foooo", "oo", "o", 10) "fo"
replace("fo", "o", "oo", 2) "foooo"

idify()

idify(" Foo ^ Bar ") "foo-bar"
idify("property ↔ Collection") "property-collection"
idify("pâté") "pate"
idify("Chicken Liver Pâté! 😋") "chicken-liver-pate"

intersects()

intersects([1], [1]) true
intersects([1, 2], [1, 3]) true
intersects([1, 2], [3, 4]) false

intersection()

intersection() null
intersection(null, null) null
intersection([1], null) null
intersection(null, [1]) null
intersection([1], [1]) [1]
intersection([1, 2], [1, 3]) [1]
intersection([1, 1, 2], [1, 3, 1]) [1, 1]
intersection([1, 2], [3, 4]) []
intersection([], [3, 4]) []
intersection([1, 2], []) []

reverse()

reverse([1, 2, 3, 4, 5]) [5, 4, 3, 2, 1]
reverse([-3, 2, 0, 1, -5, 3]) [3, -5, 1, 0, 2, -3]
reverse(["a", "b", "c", "z"]) ["z", "c", "b", "a"]

range()

range(1, 10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(3) [1, 2, 3]
range(-3) [-1, -2, -3]
range(-5, 1) [-5, -4, -3, -2, -1, 0, 1]
range(1, 3.5) [1, 2, 3]
range(.5, 3) [.5, 1.5, 2.5]
range(3, 1) [3, 2, 1]
range(.5, 3, .5) [.5, 1, 1.5, 2, 2.5, 3]
range(1, 3, 0) [1]
range(1, 3, -1) [1]

digits()

digits(2, 1234.5678) "34.5678"
digits(2, 0, 1234.5678) "34"
digits(2, 5, 1234.5678) "34.56780"
digits(2, 1) "01"
digits(2, 2, 0) "00.00"
digits(0) null

First & Last

first() null
first(3) 3
first([]) null
first(null) null
first([1, 2, 3]) 1
first(["a", "b", "c"]) "a"
$fn.first(["prop": 1, "prop": 2]) {"prop": 1}
first(3, [1, 2, 3, 4, 5]) [1,2,3]
first([null, null, 1, null, 2, 3]) 1
first([null, null, null]) null
first(3, [null, null, 1, null, 2, 3, null, 4, 5, null]) [1,2,3]
first(6, [null, null, 1, null, 2, 3, null, 4, 5, null]) [1,2,3,4,5]
first(5, [null, null, null]) []
first([0, 1, 2, 3, 0]) 0
first(2, [0, 1, 2, 3, 0]) [0,1]
$fn.first(group("prop1": 1, "prop2": 2)) {"prop1": 1, "prop2": 2}
first(0, [1, 2, 3]) []
first(1, [1, 2, 3]) [1]
first(2.5, [1, null, 2, 3]) [1,2]
first(-2.5, [1, 2, 3]) [3,2]
first(-0.1, [1, 2, 3]) []
first(0.5, [1, 2, 3]) []
first(1, 5) [5]
last() null
last(3) 3
last([]) null
last(null) null
last([1, 2, 3]) 3
last(["a", "b", "c"]) "c"
$fn.last(["prop": 1, "prop": 2]) {"prop": 2}
last(3, [1, 2, 3, 4, 5]) [5,4,3]
last([null, null, 1, null, 2, 3, null, null]) 3
last([null, null, null]) null
last(3, [null, null, 1, null, 2, 3, null, 4, 5, null]) [5,4,3]
last(6, [null, null, 1, null, 2, 3, null, 4, 5, null]) [5,4,3,2,1]
last(5, [null, null, null]) []
last([0, 1, 2, 3, 0]) 0
last(2, [0, 1, 2, 3, 0]) [0,3]
$fn.last(group("prop1": 1, "prop2": 2)) {"prop1": 1, "prop2": 2}
last(0, [1, 2, 3]) []
last(1, [1, 2, 3]) [3]
last(2.5, [1, 2, null, 3]) [3,2]
last(-2.5, [1, 2, 3]) [1,2]
last(-0.1, [1, 2, 3]) []
last(0.5, [1, 2, 3]) []
last(1, 5) [5]

groupBy()

$fn.groupBy([group(n: "a", a: 20), group(n: "a", a: 10), group(n: "a", a: 20), group(n: "a", a: 30)], $fn.as([20, 10, 20, 30], age)) [{"$value":20,"age":20,"$items":[{"n":"a","a":20},{"n":"a","a":20}]},{"$value":10,"age":10,"$items":[{"n":"a","a":10}]},{"$value":30,"age":30,"$items":[{"n":"a","a":30}]}]
$fn.groupBy([group(n: "a", a: 20), group(n: "a", a: 10), group(n: "a", a: 20), group(n: "a", a: 30)], $fn.as([20, 10, 20, 30, 40, 50], age)) [{"$value":20,"age":20,"$items":[{"n":"a","a":20},{"n":"a","a":20}]},{"$value":10,"age":10,"$items":[{"n":"a","a":10}]},{"$value":30,"age":30,"$items":[{"n":"a","a":30}]}]
$fn.groupBy([group(n: "a", a: 20), group(n: "a", a: 10), group(n: "a", a: 20), group(n: "a", a: 30)], [1,2,3]) [{"$value":1,"$items":[{"n":"a","a":20}]},{"$value":2,"$items":[{"n":"a","a":10}]},{"$value":3,"$items":[{"n":"a","a":20}]},{"$value":null,"$items":[{"n":"a","a":30}]}]
$fn.groupBy([group(n: "a", a: 20), group(n: "a", a: 10), group(n: "a", a: 20), group(n: "a", a: 30)], $fn.as([1,2,3],"num")) [{"$value":1,"num":1,"$items":[{"n":"a","a":20}]},{"$value":2,"num":2,"$items":[{"n":"a","a":10}]},{"$value":3,"num":3,"$items":[{"n":"a","a":20}]},{"$value":null,"num":null,"$items":[{"n":"a","a":30}]}]
$fn.count( $fn.groupBy([group(n: "a", a: 20), group(n: "a", a: 10), group(n: "a", a: 20), group(n: "a", a: 30)], [20, 10, 20, 30]) ) [2,1,1]
$fn.sum( $fn.as([20, 10, 20, 30]) ) 80

duration()

duration(0) "0 ms"
duration(0, 1) ["0 ms"]
duration(0, 2) ["0 ms"]
duration(1) "1 ms"
duration(999) "999 ms"
duration(1000) "1 second"
duration(60*60*1000) "1 hour"
duration(3*7*24*60*60*1000) "3 weeks"
duration(3*7*24*60*60*1000, 1) ["3 weeks"]
duration(3*7*24*60*60*1000, 4) ["3 weeks"]
duration((3*7*24 + 3) * 60*60*1000, 4) ["3 weeks"]
duration(60*60*1000+1, 1) ["1 hour"]
duration(60*60*1000+1, 2) ["1 hour"]
duration(60*60*1000+1, 5) ["1 hour"]
duration(7*24*60*60*1000) "1 week"
duration(2*31*24*60*60*1000, "day") "62 days"
duration(2*31*24*60*60*1000, "days") "62 days"
duration(24*60*60*1000, "day") "1 day"
duration(24*60*60*1000, "days") "1 day"
duration(7*24*60*60*1000,2) ["1 week"]
duration(3*7*24*60*60*1000+4*24*60*60*1000+1000,3) ["3 weeks", "4 days"]
duration(2*31*24*60*60*1000) "2 months"
duration(2*31*24*60*60*1000, 2) ["2 months"]
duration(6*52*7*24*60*60*1000+2*31*24*60*60*1000) "6 years"
duration(6*52*7*24*60*60*1000 + 2*31*24*60*60*1000,4) ["6 years", "2 months"]
duration(-2*3600*1000) "-2 hours"
duration(-72*3600*1000) "-3 days"
duration(-72*3600*1000 + -2*3600*1000 + -51*60*1000 + -29*1000 + -81) "-3 days"
duration(-72*3600*1000 + -2*3600*1000 + -51*60*1000 + -29*1000 + -81, 5) ["-3 days", "-2 hours", "-51 minutes", "-29 seconds", "-81 ms"]
duration(-72*3600*1000 + -2*3600*1000 + -51*60*1000 + -29*1000 + -81, 2) ["-3 days", "-2 hours"]
duration(-72*3600*1000 + -2*3600*1000 + -51*60*1000 + -29*1000 + -81, "days") "-3 days"
duration(-72*3600*1000 + -2*3600*1000 + -51*60*1000 + -29*1000 + -81, "hours") "-74 hours"

pluralize()

pluralize(1, "country", "countries") "1 country"
pluralize(2, "country", "countries") "2 countries"
pluralize(2, many: "countries", one: "country") "2 countries"
pluralize(0.5, "country", "countries") "0.5 countries"
pluralize(-1, "country", "countries") "-1 country"
pluralize(1, "χώρα", "χώρες", lang: 'el') "1 χώρα"
pluralize(2, "χώρα", "χώρες", lang: 'el') "2 χώρες"
pluralize(1, one: 'страна', few: 'страны', many: 'стран', lang: 'ru') "1 страна"
[pluralize(1, one: 'страна', few: 'страны', many: 'стран')] [pluralize(2, one: 'страна', few: 'страны', many: 'стран')] [pluralize(5, one: 'страна', few: 'страны', many: 'стран')] 1 страна 2 страны 5 стран

url()

url("foo", url: "https://mavo.io?foo=bar") "bar"
url("bar", url: "https://mavo.io?bar&foo=bar") ""
url("foo", url: "https://mavo.io/foo") ""
url("foo", url: "https://mavo.io/foo/bar") "bar"
url("bar", url: "https://mavo.io/foo/bar/baz") "baz"
url("foo", url: "https://mavo.io/foo/bar/baz") "bar"
url("foo", url: "https://mavo.io/foo/bar?foo=baz") "baz"
url("foo", url: "https://mavo.io/foo/bar?foo=baz", type: "query") "baz"
url("foo", url: "https://mavo.io/foo/bar?foo=baz", type: "path") "bar"
url("Foo", url: "https://mavo.io/Foo/bar?foo=baz", case_sensitive: true) "bar"
url("Foo", url: "https://mavo.io/Foo/bar?Foo=baz", case_sensitive: true) "baz"
url("foo", url: "https://mavo.io/foo/bar?foo=baz", multiple: true) ["baz"]
url("foo", url: "https://mavo.io/foo/bar?foo=baz", multiple: true, type: "path") ["bar"]
url("foo", url: "https://mavo.io/foo/bar?foo=baz&foo=yolo", multiple: true) ["baz", "yolo"]
url("foo", url: "https://mavo.io/foo", multiple: true) [""]
url("Foo", case_sensitive: true, multiple: true, url: "https://mavo.io/foo/bar?Foo=baz&foo=yolo") ["baz"]
url("foo", url: "https://mavo.io/Foo/bar", case_sensitive: true) null
url("baz", url: "https://mavo.io/foo/bar?foo=baz", multiple: true) []
url("") null
url("", multiple: true) []
url("foo", url: "https://mavo.io?foo=Pâté") "Pâté"
url("foo", url: "https://mavo.io?foo=foo%26bar") "foo&bar"
url("foo", url: "https://mavo.io?foo=bar", baz: "yolo") "bar"

Click here first for these tests to pass.

url() "https://test.mavo.io/functions.html?foo=bar"
url("foo") "bar"