Sentinel
Import: collection
The collection
import provides helpers for working with maps and lists.
filter
filter(items, predicate)
Calls predicate for each element in collection, returning a list of elements that the predicate does return true for.
Arguments
Name | Description |
---|---|
items | the list or map to perform the filter against. |
predicate | a predicate function to call for each element in the collection. A response of true will add the element to the result list. |
Examples
Return all even numbers:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
find
find(items, predicate)
Find and return an element within a collection according to the provided predicate. If nothing is found, returns undefined.
Arguments
Name | Description |
---|---|
items | the list or map to perform the find against. |
predicate | a predicate function to call for each element in the collection. A response of true will return the element and complete the find. |
Examples
Find an element in a collection based on its value:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Find an element in a collection based on its key:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
matches
matches(items, partial)
Compare each element in a collection against a partial map using deep comparison. Returns the list of elements that returned true for the partial comparison. If no matches are found, returns an empty list.
Arguments
Name | Description |
---|---|
items | the list or map to perform the match against. |
partial | the map used for partial deep comparison against each element in the collection. |
Examples
Return all items that contain {"foo": {"bar": "wip"}}
:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
reduce
reduce(items, accumulator[, initial])
Call an accumulator function for each element in collection, supplying the previous accumulated value as the accumulation parameter.
Arguments
Name | Description |
---|---|
items | the list or map to perform the reduce against. |
accumulator | a function that is called for each element in the collection, used to accumulate the value. The first argument is the current accumulated value, the remaining arguments use the same rules as predicate functions. |
initial | the initial value to use. It is an optional argument, and if not provided the first element in the collection is used as the initial value. |
Examples
Reduce the collection by adding each element together:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
reject
reject(items, predicate)
Calls predicate for each element in collection, returning a list of elements that the predicate does not return true for.
Arguments
Name | Description |
---|---|
items | the list or map to perform the reject against. |
predicate | a predicate function to call for each element in the collection. A response of false will add the element to the result list. |
Examples
Return all odd numbers by rejecting even ones:
Sentinel Playground
Loading the playground...
Press "Run" to get policy output
Predicates
Some helpers accept a predicate function, which has the purpose of making an assertion. Each predicate may accept differing arguments and return differing types. If the collection is a list, the parameters will be the element and index. If the collection is a Map, the parameters will be the value and the key.
Examples
A predicate for a list of items:
// including index
func(item, index) {
return true
}
// excluding index
func(item) {
return true
}
A predicate for a map of items:
// including key
func(value, key) {
return true
}
// excluding key
func(value) {
return true
}