UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

43 lines 1.32 kB
//#region src/array/indexBy.d.ts /** * `indexBy(target, by, transform?)` * * Creates a record by indexing the `target` array using the `by` function to generate keys. Optionally transforms values using the `transform` function. * * ```ts * const users = [ * { id: 1, name: "Alice" }, * { id: 2, name: "Bob" }, * ]; * * indexBy(users, (user) => user.id); * // { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } } * * indexBy( * users, * (user) => user.id, * (user) => user.name, * ); // { 1: "Alice", 2: "Bob" } * ``` * * ```ts * pipe( * users, * indexBy((user) => user.id), * ); // { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } } * * pipe( * users, * indexBy( * (user) => user.id, * (user) => user.name, * ), * ); // { 1: "Alice", 2: "Bob" } * ``` */ declare const indexBy: { <T extends object, U extends PropertyKey, V>(target: readonly T[], by: (value: NoInfer<T>, idx: number, target: readonly NoInfer<T>[]) => U, transform: (value: NoInfer<T>, key: U, idx: number, target: readonly NoInfer<T>[]) => V): Record<U, V>; <T extends object, U extends PropertyKey>(target: readonly T[], by: (value: NoInfer<T>, idx: number, target: readonly NoInfer<T>[]) => U): Record<U, T>; }; //#endregion export { indexBy };