@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
36 lines (35 loc) • 1.73 kB
TypeScript
import { type MaybePromise } from '@augment-vir/core';
/**
* Polyfill for `Object.groupBy`:
* https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
*
* @category Array
* @category Object
* @example
*
* ```ts
* import {arrayToObject} from '@augment-vir/common';
*
* const result = arrayToObject(
* [
* 'a',
* 'b',
* ],
* (value) => `key-${value}`,
* );
* // result is `{key-a: ['a'], key-b: ['b']}`
* ```
*/
export declare function groupArrayBy<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Partial<Record<NewKey, ElementType[]>>;
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => Promise<{
key: NewKey;
value: NewValue;
} | undefined>): Promise<Partial<Record<NewKey, NewValue>>>;
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => {
key: NewKey;
value: NewValue;
} | undefined): Partial<Record<NewKey, NewValue>>;
export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MaybePromise<{
key: NewKey;
value: NewValue;
} | undefined>): MaybePromise<Partial<Record<NewKey, NewValue>>>;