pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
23 lines (19 loc) • 627 B
text/typescript
import { Dictionary, getValueOr, Mappable } from "./main.ts";
export function groupBy<A>(
mkGroup: Mappable<A, string>,
coll: A[]
): Dictionary<A[]>;
export function groupBy<A>(
mkGroup: Mappable<A, string>
): (coll: A[]) => Dictionary<A[]>;
export function groupBy<A>(mkGroup: Mappable<A, string>, coll?: A[]) {
if (arguments.length === 1) {
return (theColl: A[]) => groupBy(mkGroup, theColl);
}
return getValueOr([], coll).reduce((accum: Dictionary<A[]>, value: A) => {
const group = mkGroup(value);
if (!accum[group]) accum[group] = [];
accum[group].push(value);
return accum;
}, {});
}