@sentrie/collection
The @sentrie/collection module provides utilities for both list/array and map/object manipulation and operations. Functions are prefixed with list_ for list operations and map_ for map operations.
use { list_includes, map_keys, map_get } from @sentrie/collectionList Functions
Section titled “List Functions”list_includes(arr: any[], item: any): boolean
Section titled “list_includes(arr: any[], item: any): boolean”Checks if an array includes a specific item. Uses deep equality comparison.
Parameters:
arr- The array to search initem- The item to search for
Returns: true if the item is found in the array, false otherwise
Example:
use { list_includes } from @sentrie/collectionlet numbers = [1, 2, 3, 4, 5]let hasThree = collection.list_includes(numbers, 3) // truelist_indexOf(arr: any[], item: any): number
Section titled “list_indexOf(arr: any[], item: any): number”Finds the first index of an item in an array. Uses deep equality comparison.
Parameters:
arr- The array to search initem- The item to search for
Returns: The index of the first occurrence, or -1 if not found
Example:
use { list_indexOf } from @sentrie/collectionlet numbers = [1, 2, 3, 4, 5]let idx = collection.list_indexOf(numbers, 3) // 2list_lastIndexOf(arr: any[], item: any): number
Section titled “list_lastIndexOf(arr: any[], item: any): number”Finds the last index of an item in an array. Uses deep equality comparison.
Parameters:
arr- The array to search initem- The item to search for
Returns: The index of the last occurrence, or -1 if not found
list_sort(arr: any[]): any[]
Section titled “list_sort(arr: any[]): any[]”Sorts an array in ascending order. Sorts numbers numerically and strings lexicographically.
Parameters:
arr- The array to sort
Returns: A new sorted array (original array is not modified)
Throws: Error if the input is not an array
Example:
use { list_sort } from @sentrie/collectionlet numbers = [3, 1, 4, 1, 5]let sorted = collection.list_sort(numbers) // [1, 1, 3, 4, 5]list_unique(arr: any[]): any[]
Section titled “list_unique(arr: any[]): any[]”Removes duplicate values from an array. Uses equality comparison to detect duplicates.
Parameters:
arr- The array to deduplicate
Returns: A new array with unique values (original array is not modified)
Throws: Error if the input is not an array
Example:
use { list_unique } from @sentrie/collectionlet numbers = [1, 2, 2, 3, 3, 3]let unique = collection.list_unique(numbers) // [1, 2, 3]list_chunk(arr: any[], size: number): any[][]
Section titled “list_chunk(arr: any[], size: number): any[][]”Splits an array into chunks of a specified size.
Parameters:
arr- The array to chunksize- The size of each chunk (must be positive)
Returns: Array of chunks, where each chunk is an array of the specified size (except possibly the last)
Throws: Error if the input is not an array or size is not positive
Example:
use { list_chunk } from @sentrie/collectionlet numbers = [1, 2, 3, 4, 5, 6]let chunks = collection.list_chunk(numbers, 2) // [[1, 2], [3, 4], [5, 6]]list_flatten(arr: any[]): any[]
Section titled “list_flatten(arr: any[]): any[]”Flattens a nested array structure by one level or recursively.
Parameters:
arr- The nested array to flatten
Returns: A new flattened array (original array is not modified)
Throws: Error if the input is not an array
Example:
use { list_flatten } from @sentrie/collectionlet nested = [[1, 2], [3, 4], [5, 6]]let flat = collection.list_flatten(nested) // [1, 2, 3, 4, 5, 6]Map Functions
Section titled “Map Functions”map_keys(map: Record<string, any>): any[]
Section titled “map_keys(map: Record<string, any>): any[]”Gets all keys from a map/object.
Parameters:
map- The map/object to extract keys from
Returns: Array of all keys in the map
Throws: Error if the input is not a map
Example:
use { map_keys } from @sentrie/collectionlet user = {"name": "John", "age": 30, "role": "admin"}let keys = collection.map_keys(user) // ["name", "age", "role"]map_values(map: Record<string, any>): any[]
Section titled “map_values(map: Record<string, any>): any[]”Gets all values from a map/object.
Parameters:
map- The map/object to extract values from
Returns: Array of all values in the map
Throws: Error if the input is not a map
Example:
use { map_values } from @sentrie/collectionlet user = {"name": "John", "age": 30, "role": "admin"}let values = collection.map_values(user) // ["John", 30, "admin"]map_entries(map: Record<string, any>): [any, any][]
Section titled “map_entries(map: Record<string, any>): [any, any][]”Gets all key-value pairs from a map/object as an array of [key, value] tuples.
Parameters:
map- The map/object to extract entries from
Returns: Array of [key, value] pairs
Throws: Error if the input is not a map
Example:
use { map_entries } from @sentrie/collectionlet user = {"name": "John", "age": 30}let entries = collection.map_entries(user) // [["name", "John"], ["age", 30]]map_has(map: Record<string, any>, key: any): boolean
Section titled “map_has(map: Record<string, any>, key: any): boolean”Checks if a map/object contains a specific key.
Parameters:
map- The map/object to checkkey- The key to check for
Returns: true if the key exists in the map, false otherwise
Example:
use { map_has } from @sentrie/collectionlet user = {"name": "John", "age": 30}let hasName = collection.map_has(user, "name") // truelet hasEmail = collection.map_has(user, "email") // falsemap_get(map: Record<string, any>, key: any, defaultValue?: any): any
Section titled “map_get(map: Record<string, any>, key: any, defaultValue?: any): any”Gets a value from a map/object by key, with an optional default value.
Parameters:
map- The map/object to get the value fromkey- The key to look updefaultValue- Optional default value to return if the key is not found
Returns: The value associated with the key, or the default value if the key is not found (or undefined if no default provided)
Example:
use { map_get } from @sentrie/collectionlet user = {"name": "John", "age": 30}let name = collection.map_get(user, "name") // "John"let email = collection.map_get(user, "email", "unknown") // "unknown"map_size(map: Record<string, any>): number
Section titled “map_size(map: Record<string, any>): number”Gets the number of key-value pairs in a map/object.
Parameters:
map- The map/object to get the size of
Returns: The number of entries in the map
Throws: Error if the input is not a map
Example:
use { map_size } from @sentrie/collectionlet user = {"name": "John", "age": 30, "role": "admin"}let size = collection.map_size(user) // 3map_isEmpty(map: Record<string, any>): boolean
Section titled “map_isEmpty(map: Record<string, any>): boolean”Checks if a map/object is empty (has no entries).
Parameters:
map- The map/object to check
Returns: true if the map has no entries, false otherwise
Throws: Error if the input is not a map
Example:
use { map_isEmpty } from @sentrie/collectionlet empty = {}let notEmpty = {"name": "John"}let isEmpty1 = collection.map_isEmpty(empty) // truelet isEmpty2 = collection.map_isEmpty(notEmpty) // falsemap_merge(map1: Record<string, any>, map2: Record<string, any>, ...maps: Record<string, any>[]): Record<string, any>
Section titled “map_merge(map1: Record<string, any>, map2: Record<string, any>, ...maps: Record<string, any>[]): Record<string, any>”Merges multiple maps/objects into a single map. Later maps override earlier maps if they have the same keys.
Parameters:
map1- The first map/objectmap2- The second map/object...maps- Additional maps/objects to merge
Returns: A new merged map (original maps are not modified)
Throws: Error if any argument is not a map
Example:
use { map_merge } from @sentrie/collectionlet user1 = {"name": "John", "age": 30}let user2 = {"age": 31, "role": "admin"}let merged = collection.map_merge(user1, user2) // {"name": "John", "age": 31, "role": "admin"}Complete Example
Section titled “Complete Example”namespace com/example/mypolicy
policy mypolicy { use { list_includes, list_sort, map_keys, map_get } from @sentrie/collection fact numbers!: list[number] fact user!: document
rule myrule = default false { let sorted = collection.list_sort(numbers) let hasFive = collection.list_includes(sorted, 5) let userKeys = collection.map_keys(user) let userName = collection.map_get(user, "name", "Unknown") yield hasFive and userName != "Unknown" }
export decision of myrule}