Skip to main content

Collections

The collections module provides utility functions to convert arrays into Map and Set data structures with a custom key or value selector.

Import

import { createMapFromArray, createSetFromArray } from '@open-kerno/commons/collections';

createMapFromArray

Converts an array into a Map, using a function to extract the key from each item.

Signature

function createMapFromArray<T, K>(items: T[], getKey: (item: T) => K): Map<K, T>

Parameters

ParameterTypeDescription
itemsT[]The source array
getKey(item: T) => KA function that returns the map key for each item

Returns

A Map<K, T> where each key maps to the original item.

Example

import { createMapFromArray } from '@open-kerno/commons/collections';

const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
];

const userMap = createMapFromArray(users, (user) => user.id);

console.log(userMap.get(1)); // { id: 1, name: 'Alice' }
console.log(userMap.get(2)); // { id: 2, name: 'Bob' }

createSetFromArray

Converts an array into a Set by extracting a value from each item.

Signature

function createSetFromArray<T, V>(items: T[], getValue: (item: T) => V): Set<V>

Parameters

ParameterTypeDescription
itemsT[]The source array
getValue(item: T) => VA function that returns the set value for each item

Returns

A Set<V> containing the extracted values.

Example

import { createSetFromArray } from '@open-kerno/commons/collections';

const orders = [
{ id: 10, status: 'pending' },
{ id: 11, status: 'shipped' },
{ id: 12, status: 'pending' },
];

const statuses = createSetFromArray(orders, (order) => order.status);

console.log(statuses); // Set { 'pending', 'shipped' }