UNPKG

@quinck/collections

Version:

Allows extra operations on JavaScript collections: Array, Map and Set.

41 lines (40 loc) 2.24 kB
export type Mapper<Input, Output> = (input: Input) => Output; export type GroupByMergeFunction<V> = (a: V, b: V) => V; declare global { interface Array<T> { /** * Perform a group by operation grouping by the result of the keyMapper. * @param keyMapper used to get the key * @param valueMapper used to get the value * @param mergeFunction unify two items into a unique items with the same key * @returns a map with the elements grouped by the keys generated using the keyMapper */ groupBy<K, V>(keyMapper: Mapper<T, K>, valueMapper: Mapper<T, V>, mergeFunction?: GroupByMergeFunction<V>): Map<K, V>; /** * Perform a group by operation grouping by the result of the keyMapper. * @param keyMapper used to get the key * @param valueMapper used to get the value * @param mergeFunction unify two items into a unique items with the same key * @returns an object with the elements grouped by the keys generated using the keyMapper */ groupByToDictionary<K extends string, V>(keyMapper: Mapper<T, K>, valueMapper: Mapper<T, V>, mergeFunction?: GroupByMergeFunction<V>): Record<K, V>; /** * Perform a group by operation grouping by the result of the keyMapper. * As the simplified version of a groupby this method only allows to * group by objects by specific keys generating an array of elements * for each key. * @param keyMapper used to get the key * @returns a map with the elements grouped by the keys generated using the keyMapper */ simpleGroupBy<K>(keyMapper: Mapper<T, K>): Map<K, T[]>; /** * Perform a group by operation grouping by the result of the keyMapper. * As the simplified version of a groupby this method only allows to * group by objects by specific keys generating an array of elements * for each key. * @param keyMapper used to get the key * @returns a map with the elements grouped by the keys generated using the keyMapper */ simpleGroupByToDictionary<K extends string>(keyMapper: Mapper<T, K>): Record<K, T[]>; } }