Function tests

These tests test whether MaVue helper functions produce correct results.

iff()

iff(4, 1, 2) 1
iff(0, 1, 2) 2
iff([], 1, 2) 1
iff("", 1, 2) 2
iff(4 > 2, 1, 2) 1
iff("foo".length, 1, 2) 1
iff(true, 1) 1
iff(isNaN(42), 1) ""

get()

get({foo: 4}, "foo") 4
get({foo: 4}, "bar") undefined
get([ {foo: "fooname"}, {bar: "barname"} ], "foo") ["fooname", null]
get([ {foo: "fooname"}, {bar: "barname"} ], "baz") [null, null]
get([ {id: "foo", name: "fooname"}, {id: "bar", name: "barname"} ], "name") ["fooname", "barname"]
get({bar: [1,2,3]}, "*") [1,2,3]
get({foo: [1,2], bar:[3,4]}, "*") [1,2,3,4]
get({ bar: [ {foo: 1}, {foo: 2}, {foo: 3} ] }, "*") [{"foo": 1}, {"foo": 2}, {"foo": 3}]
get({ bar: [ {foo: 1}, {foo: 2}, {foo: 3} ] }, "bar.*.foo") [1,2,3]
get({ bar: [ {foo: 1}, {foo: 2}, {foo: 3} ] }, "*.foo") [null]
get({ bar: [ {foo: 1}, {foo: 2}, {foo: 3} ] }, "bar.*") [{"foo": 1}, {"foo": 2}, {"foo": 3}]
get([ {id: "foo", name: "fooname"}, {id: "bar", name: "barname"} ], ["id"]) ["foo", "bar"]
get({ bar: [{foo: 1}, {foo: 2}, {foo: 3}] }, ["bar", "*", "foo"]) [1, 2, 3]
get([1, 2, 3], 1) 2
get([1, [2, 3]], 1) [2, 3]
get({foo: true}, "foo == true") TypeError
get({foo: true}, ["foo == true"]) undefined

sum()

sum(1, 2, 3) 6
sum( "", " ", "foo", false, null, undefined, [], {}, NaN ) 0
sum([]) 0
sum([1, 2, 3]) 6
sum([1, "foo", 2, false, 3, undefined]) 6
sum([], []) 0
sum([1, 2, 3], [-1, -2, -3]) 0
sum( [1, "foo", 2, false, 3, undefined], [null, -1, -2, -3, {}] ) 0
sum(-5, [1, 2], 3, [4, 5]) 10
sum(-5, [1, 2], false, 3, [4, "", 5], null) 10
sum([1, 2, [3, 4, "foo", null], 3, [4, 5]]) 6

count()

count( "", " ", "foo", false, true, null, undefined, [], {}, NaN ) 4
count([1, "foo", 2, false, 3, undefined, true]) 5
count( [1, "foo", 2, false, 3, undefined], [null, -1, -2, -3, {} ]) 8
count( -5, [1, 2, {}], false, 3, [4, "", 5], null ) 7
count([2, [3, "foo", null], [3, [4, 5]] ]) 3