pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
21 lines (16 loc) • 542 B
text/typescript
import { Dictionary, getValueOr, Mappable } from "./main.ts";
export function indexBy<A>(
mkIndex: Mappable<A, string>,
coll: A[]
): Dictionary<A>;
export function indexBy<A>(
mkIndex: Mappable<A, string>
): (coll: A[]) => Dictionary<A>;
export function indexBy<A>(mkIndex: Mappable<A, string>, coll?: A[]) {
if (arguments.length === 1)
return (theColl: A[]) => indexBy(mkIndex, theColl);
return getValueOr([], coll).reduce((accum: Dictionary<A>, next: A) => {
accum[mkIndex(next)] = next;
return accum;
}, {});
}