import { curryN } from "@unboxing/function"; type Pred = (v: T, index: number, arr: ArrayLike) => string; interface IndexBy { (fn: Pred, list: ArrayLike): Record; (fn: Pred): (list: ArrayLike) => Record; } /** * Given a function that generates a key, turns a list of objects into an * object indexing the objects by the given key. Note that if multiple * objects generate the same value for the indexing key only the last value * will be included in the generated object. */ export const indexBy = curryN(2, (fn: Pred, arr: ArrayLike = []) => { const result:any = {}; for (let i = 0; i < arr.length; i++) { result[fn(arr[i], i, arr)] = arr[i]; } return result; }) as IndexBy