Skip to content

Conformance Examples 0.1.0 — Transforms: transform-sum

sum-integers

Sums a list of integers; a whole-number total renders without a decimal point.

Template

{{ xs | sum }}

Context

{
    "xs": [
        1,
        2,
        3
    ]
}

Output

6

sum-fractional

A non-whole total renders with its fractional part.

Template

{{ xs | sum }}

Context

{
    "xs": [
        1.5,
        2.0
    ]
}

Output

3.5

sum-numeric-strings

Numeric strings are coerced to numbers before summing.

Template

{{ xs | sum }}

Context

{
    "xs": [
        "1",
        "2",
        "3"
    ]
}

Output

6

sum-by-property

With a property argument, the property of each struct is summed.

Template

{{ items | sum: "n" }}

Context

{
    "items": [
        {
            "n": 2
        },
        {
            "n": 3
        }
    ]
}

Output

5

sum-empty-list

Summing an empty list yields zero.

Template

{{ xs | sum }}

Context

{
    "xs": []
}

Output

0

sum-non-numeric-is-null

A non-numeric element means the list does not match the transform's shape, so the result is null.

Template

{{ xs | sum }}

Context

{
    "xs": [
        1,
        "abc",
        3
    ]
}

Output

null

sum-property-missing-is-null

A struct missing the requested property yields a non-numeric value, so the result is null.

Template

{{ items | sum: "n" }}

Context

{
    "items": [
        {
            "n": 2
        },
        {
            "m": 3
        }
    ]
}

Output

null

sum-property-non-struct-is-null

With a property, a non-struct element cannot supply the value, so the result is null.

Template

{{ xs | sum: "n" }}

Context

{
    "xs": [
        1,
        2
    ]
}

Output

null

sum-non-list-is-null

sum operates only on lists; a string input yields null.

Template

{{ x | sum }}

Context

{
    "x": "hi"
}

Output

null

sum-boolean-element-is-null

A boolean element is neither numeric nor a numeric string, so the result is null.

Template

{{ xs | sum }}

Context

{
    "xs": [
        true
    ]
}

Output

null

Source fixture: transforms/transform-sum.json