import {groupValues} from '../internal/array/group'; import type {Key, KeyedValue, PlainObject, Simplify} from '../models'; // #region Functions /** * Create a record from an array of items using callbacks * * If multiple items have the same key, the latest item will be used * * @param array Array to convert * @param key Callback to get an item's grouping key * @param value Callback to get an item's value * @returns Record of keyed values * * @example * ```typescript * toRecord( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value, * item => item.id, * ); // => { 10: 3, 20: 2 } * ``` */ export function toRecord< Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown, >( array: Item[], key: KeyCallback, value: ValueCallback, ): Record, ReturnType>; /** * Create a record from an array of items using a callback and value * * If multiple items have the same key, the latest item will be used * * @param array Array to convert * @param callback Callback to get an item's grouping key * @param value Key to use for value * @returns Record with keys * * @example * ```typescript * toRecord( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value, * 'id', * ); // => { 10: 3, 20: 2 } * ``` */ export function toRecord< Item extends PlainObject, Callback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item, >( array: Item[], callback: Callback, value: ItemValue, ): Record, Item[ItemValue]>; /** * Create a record from an array of items using a key and callback * * If multiple items have the same key, the latest item will be used * * @param array Array to convert * @param key Key to use for grouping * @param callback Callback to get an item's value * @returns Record with keys * * @example * ```typescript * toRecord( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * 'value', * item => item.id, * ); // => { 10: 3, 20: 2 } * ``` */ export function toRecord< Item extends PlainObject, ItemKey extends keyof Item, Callback extends (item: Item, index: number, array: Item[]) => unknown, >( array: Item[], key: ItemKey, callback: Callback, ): Simplify, ReturnType>>; /** * Create a record from an array of items using a key and value * * If multiple items have the same key, the latest item will be used * * @param array Array to convert * @param key Key to use for grouping * @param value Key to use for value * @returns Record of keyed values * * @example * ```typescript * toRecord( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * 'value', * 'id', * ); // => { 10: 3, 20: 2 } * ``` */ export function toRecord< Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item, >( array: Item[], key: ItemKey, value: ItemValue, ): Simplify, Item[ItemValue]>>; /** * Create a record from an array of items using a callback * * If multiple items have the same key, the latest item will be used * * @param array Array to convert * @param callback Callback to get an item's grouping key * @returns Record of keyed values * * @example * ```typescript * toRecord( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value, * ); // => { 10: {id: 3, value: 10}, 20: {id: 2, value: 20} } * ``` */ export function toRecord Key>( array: Item[], callback: Callback, ): Record, Item>; /** * Create a record from an array of items using a key * * If multiple items have the same key, the latest item will be used * * @param array Array to convert * @param key Key to use for grouping * @returns Record of keyed values * * @example * ```typescript * toRecord( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * 'value', * ); // => { 10: {id: 3, value: 10}, 20: {id: 2, value: 20} } * ``` */ export function toRecord( array: Item[], key: ItemKey, ): Simplify, Item>>; /** * Create a record from an array of items _(using indices as keys)_ * * @param array Array to convert * @returns Record of indiced values * * @example * ```typescript * toRecord( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * ); // => { 0: {id: 1, value: 10}, 1: {id: 2, value: 20}, 2: {id: 3, value: 10} } * ``` */ export function toRecord(array: Item[]): Record; export function toRecord(array: unknown[], first?: unknown, second?: unknown): unknown { return groupValues(array, first, second, false); } toRecord.arrays = toRecordArrays; /** * Create a record from an array of items using callbacks, grouping values into arrays * * _Available as `toRecordArrays` and `toRecord.arrays`_ * * @param array Array to convert * @param key Callback to get an item's grouping key * @param value Callback to get an item's value * @returns Record of keyed arrays of values * * @example * ```typescript * toRecordArrays( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value, * item => item.id, * ); // => { 10: [1, 3], 20: [2] } * ``` */ export function toRecordArrays< Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown, >( array: Item[], key: KeyCallback, value: ValueCallback, ): Record, ReturnType[]>; /** * Create a record from an array of items using a callback and value, grouping values into arrays * * _Available as `toRecordArrays` and `toRecord.arrays`_ * * @param array Array to convert * @param callback Callback to get an item's grouping key * @param value Key to use for value * @returns Record of keyed arrays of values * * @example * ```typescript * toRecordArrays( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value, * 'id', * ); // => { 10: [1, 3], 20: [2] } * ``` */ export function toRecordArrays< Item extends PlainObject, Callback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item, >( array: Item[], callback: Callback, value: ItemValue, ): Record, Item[ItemValue][]>; /** * Create a record from an array of items using a key and callback, grouping values into arrays * * _Available as `toRecordArrays` and `toRecord.arrays`_ * * @param array Array to convert * @param key Key to use for grouping * @param callback Callback to get an item's value * @returns Record of keyed arrays of values * * @example * ```typescript * toRecordArrays( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * 'value', * item => item.id, * ); // => { 10: [1, 3], 20: [2] } * ``` */ export function toRecordArrays< Item extends PlainObject, ItemKey extends keyof Item, Callback extends (item: Item, index: number, array: Item[]) => unknown, >( array: Item[], key: ItemKey, callback: Callback, ): Simplify, ReturnType[]>>; /** * Create a record from an array of items using a key and value, grouping values into arrays * * _Available as `toRecordArrays` and `toRecord.arrays`_ * * @param array Array to convert * @param key Key to use for grouping * @param value Key to use for value * @returns Record of keyed arrays of values * * @example * ```typescript * toRecordArrays( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * 'value', * 'id', * ); // => { 10: [1, 3], 20: [2] } * ``` */ export function toRecordArrays< Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item, >( array: Item[], key: ItemKey, value: ItemValue, ): Simplify, Item[ItemValue][]>>; /** * Create a record from an array of items using a callback, grouping items into arrays * * _Available as `toRecordArrays` and `toRecord.arrays`_ * * @param array Array to convert * @param callback Callback to get an item's grouping key * @returns Record of keyed arrays of items * * @example * ```typescript * toRecordArrays( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * item => item.value, * ); // => { 10: [{id: 1, value: 10}, {id: 3, value: 10}], 20: [{id: 2, value: 20}] } * ``` */ export function toRecordArrays< Item, Callback extends (item: Item, index: number, array: Item[]) => Key, >(array: Item[], callback: Callback): Record, Item[]>; /** * Create a record from an array of items using a key, grouping items into arrays * * _Available as `toRecordArrays` and `toRecord.arrays`_ * * @param array Array to convert * @param key Key to use for grouping * @returns Record of keyed arrays of items * * @example * ```typescript * toRecordArrays( * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}], * 'value', * ); // => { 10: [{id: 1, value: 10}, {id: 3, value: 10}], 20: [{id: 2, value: 20}] } * ``` */ export function toRecordArrays( array: Item[], key: ItemKey, ): Simplify, Item[]>>; export function toRecordArrays(array: unknown[], first?: unknown, second?: unknown): unknown { return groupValues(array, first, second, true); } // #endregion