Skip to content

Enumerable

Stef Heyenrath edited this page Sep 27, 2020 · 13 revisions

Custom Enumerable Block Helpers

These block helpers allow for some simple selecting, querying, filtering on arrays, lists and collections.


Distinct

Summary Return a collection of distinct elements from an array
Returns Updated collection of distinct elements
Remarks If object is not an array, this will drop into the else block. Note: this can be used with an array of maps/objects, it does not need to be an array of simple types(string, numeric, bool, etc..)
Parameters
array Array of objects to make distinct

Example

Context

{
    "array": [
        "one",
        "two",
        "two",
        "two",
        "three"
    ],
    "badInput": {
        "something": true
    }
}

Usage

<strong>result:</strong>
{{#Enumerable.Distinct array}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Distinct}}

{{#Enumerable.Distinct badInput}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Distinct}}

Returns

<strong>result:</strong>
<strong>True</strong>
<ul>
    <li>Value: one</li>
    <li>Value: two</li>
    <li>Value: three</li>
</ul>

<strong>False</strong>

IsFirst

Summary Determine whether or not the object passed in is the first element in the passed in array
Returns Whether or not the object is the first element in the array
Remarks
Parameters
array Array of objects to inspect
object Object to match on

Example

Context

{
    "array": [
        "one",
        "two",
        "three"
    ]
}

Usage

<strong>result:</strong>
{{#Enumerable.IsFirst array "one"}}
    <strong>True</strong>
{{else}}
    <strong>False</strong>
{{/IsFirst}}

{{#Enumerable.IsFirst array "two"}}
    <strong>True</strong>
{{else}}
    <strong>False</strong>
{{/IsFirst}}

Returns

<strong>result:</strong>
<strong>True</strong>

<strong>False</strong>

IsLast

Summary Determine whether or not the object passed in is the last element in the passed in array
Returns Whether or not the object is the last element in the array
Remarks
Parameters
array Array of objects to inspect
object Object to match on

Example

Context

{
    "array": [
        "one",
        "two",
        "three"
    ]
}

Usage

<strong>result:</strong>
{{#Enumerable.IsLast array "one"}}
    <strong>True</strong>
{{else}}
    <strong>False</strong>
{{/IsLast}}

{{#Enumerable.IsLast array "three"}}
    <strong>True</strong>
{{else}}
    <strong>False</strong>
{{/IsLast}}

Returns

<strong>result:</strong>
<strong>False</strong>

<strong>True</strong>

Page

Summary Return a collection of elements from an array, skipping N elements
Returns A single page worth of elements
Remarks If object is not an array, if page number is not passed in, or if page number or countPerPage are <= 0, this will drop into the else block. Note: this can be used with an array of maps/objects, it does not need to be an array of simple types(string, numeric, bool, etc..)
Parameters
array Array of objects to evaluate
pageNumber Page number
countPerPage Number of elements per page, default 0

Example

Context

{
    "array": [
        "one",
        "two",
        "three",
        "four",
        "five"
    ],
    "badInput": {
        "something": true
    }
}

Usage

<strong>result:</strong>
{{#Enumerable.Page array 2 2}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Page}}

{{#Enumerable.Page badInput 1 3}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Page}}

{{#Enumerable.Page array 1 -1}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Page}}

{{#Enumerable.Page array 0 2}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Page}}

Returns

<strong>result:</strong>
<strong>True</strong>
<ul>
    <li>Value: three</li>
    <li>Value: four</li>
</ul>

<strong>False</strong>

<strong>False</strong>

<strong>False</strong>

Reverse

Summary Reverses the order of an array
Returns An array with the element order reversed
Remarks If object is not an array, if page number is not passed in, or if page number or countPerPage are <= 0, this will drop into the else block. Note: this can be used with an array of maps/objects, it does not need to be an array of simple types(string, numeric, bool, etc..)
Parameters
input Array of elements to reverse

Example

Context

{
    "array": [
        "one",
        "two",
        "three",
        "four",
        "five"
    ],
    "badInput": {
        "something": true
    }
}

Usage

<strong>Result:</strong>
{{#Enumerable.Reverse array}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Reverse}}

<strong>Input Not an Array Result:</strong>
{{#Enumerable.Reverse badInput}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Reverse}}

Returns

<strong>Result:</strong>
<strong>True</strong>
<ul>
    <li>Value: five</li>
    <li>Value: four</li>
    <li>Value: three</li>
    <li>Value: two</li>
    <li>Value: one</li>
</ul>

<strong>Input Not an Array Result:</strong>
<strong>False</strong>

Select

Summary Return a collection of properties from an array
Returns Updated collection of elements
Remarks If input is not an array, this will drop into the else block. Note: this can be used with an array of maps/objects, it does not need to be an array of simple types(string, numeric, bool, etc..)
If property is not found, this will return an empty array
Parameters
input Array of objects to search
property Property to match on

Example

Context

{
    "array": [
        {
            "id": 1,
            "name": "test1",
            "type": {
                "id": 5,
                "name": "type1"
            }
        },
        {
            "id": 2,
            "name": "test2",
            "type": {
                "id": 5,
                "name": "type1",
                "color": "blue"
            }
        },
        {
            "id": 3,
            "name": "test2",
            "type": {
                "id": 6,
                "name": "type2",
                "color": "red"
            }
        },
        {
            "id": 4,
            "name": "test3",
            "type": {
                "id": 7,
                "name": "type3",
                "color": "red"
            }
        }
    ],
    "notArray": {
        "id": 1
    }
}

Usage

<strong>Direct Property:</strong>
{{#Enumerable.Select array "name"}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Select}}

<strong>Object Property:</strong>
{{#Enumerable.Select array "type"}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this.id}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Select}}

<strong>Nested Property:</strong>
{{#Enumerable.Select array "type.name"}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Select}}

<strong>Property Exists in Some Elements:</strong>
{{#Enumerable.Select array "type.color"}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Select}}

<strong>Property Doesen't Exist:</strong>
{{#Enumerable.Select array "doesnotexist"}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Select}}

<strong>Input Not an Array:</strong>
{{#Enumerable.Select notArray "id"}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Select}}

Returns

<strong>Direct Property:</strong>
<strong>True</strong>
<ul>
    <li>Value: test1</li>
    <li>Value: test2</li>
    <li>Value: test2</li>
    <li>Value: test3</li>
</ul>

<strong>Object Property:</strong>
<strong>True</strong>
<ul>
    <li>Value: 5</li>
    <li>Value: 5</li>
    <li>Value: 6</li>
    <li>Value: 7</li>
</ul>

<strong>Nested Property:</strong>
<strong>True</strong>
<ul>
    <li>Value: type1</li>
    <li>Value: type1</li>
    <li>Value: type2</li>
    <li>Value: type3</li>
</ul>

<strong>Property Exists in Some Elements:</strong>
<strong>True</strong>
<ul>
    <li>Value: blue</li>
    <li>Value: red</li>
    <li>Value: red</li>
</ul>

<strong>Property Doesen't Exist:</strong>
<strong>True</strong>
<ul>
</ul>

<strong>Input Not an Array:</strong>
<strong>False</strong>

Skip

Summary Return a collection of elements from an array, skipping N elements
Returns Collection of elements, minus the ones that were skipped
Remarks If object is not an array, this will drop into the else block. Note: this can be used with an array of maps/objects, it does not need to be an array of simple types(string, numeric, bool, etc..)
Parameters
array Array of objects to evaluate
skipN Number of elements to skip

Example

Context

{
    "array": [
        "one",
        "two",
        "three"
    ],
    "badInput": {
        "something": true
    }
}

Usage

<strong>result:</strong>
{{#Enumerable.Skip array 2}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Skip}}

{{#Enumerable.Skip badInput 1}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Skip}}

Returns

<strong>result:</strong>
<strong>True</strong>
<ul>
    <li>Value: three</li>
</ul>

<strong>False</strong>

Take

Summary Return a collection of N elements from an array
Returns Collection of N elements
Remarks If object is not an array, this will drop into the else block. Note: this can be used with an array of maps/objects, it does not need to be an array of simple types(string, numeric, bool, etc..)
Parameters
array Array of objects to evaluate
takeN Number of elements to return

Example

Context

{
    "array": [
        "one",
        "two",
        "three"
    ],
    "badInput": {
        "something": true
    }
}

Usage

<strong>result:</strong>
{{#Enumerable.Take array 2}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Take}}

{{#Enumerable.Take badInput 1}}
    <strong>True</strong>
    <ul>
    {{#each this}}
        <li>Value: {{this}}</li>
    {{/each}}
    </ul>
{{else}}
    <strong>False</strong>
{{/Take}}

Returns

<strong>result:</strong>
<strong>True</strong>
<ul>
    <li>Value: one</li>
    <li>Value: two</li>
</ul>

<strong>False</strong>
Clone this wiki locally