tamda
Version:
Practical functional programming library for TypeScript
46 lines (45 loc) • 1.88 kB
TypeScript
import { ObjectMap } from '../object-map';
/**
* Transforms an `array` into an `ObjectMap` using the keys extracted from a function `keyFn`.
*
* `ObjectMap`s are plain JavaScript objects that are similar to native `Map`s,
* but serializable because the key is always a `string`.
*
* @note `number` keys wil be converted to `string`.
* @param array Array to transform.
* @param keyFn Function that extracts a key from each item.
* @example
* toObjectMap(
* [{ id: 4, name: 'John' }, { id: 1, name: 'Mary' }],
* x => x.id
* )
* // { '1': { id: 1, name: 'Mary' }, '4': { id: 4, name: 'John' }}
*/
export declare function toObjectMap<T>(array: T[], keyFn: KeyFn<T>): ObjectMap<T>;
/**
* Returns a function that
* transforms an `array` into an `ObjectMap` using the keys extracted from a function `keyFn`.
*
* `ObjectMap`s are plain JavaScript objects that are similar to native `Map`s,
* but serializable because the key is always a `string`.
*
* @note `number` keys wil be converted to `string`.
* @param keyFn Function that extracts a key from each item.
* @example
* const byId = toObjectMap(x => x.id);
* byId([{ id: 4, name: 'John' }, { id: 1, name: 'Mary' }])
* // { '1': { id: 1, name: 'Mary' }, '4': { id: 4, name: 'John' }}
*/
export declare function toObjectMap<T>(keyFn: KeyFn<T>): typeof deferred;
/**
* Transforms an `array` into an `ObjectMap` using the keys extracted from a previously specified function `keyFn`.
*
* `ObjectMap`s are plain JavaScript objects that are similar to native `Map`s,
* but serializable because the key is always a `string`.
*
* @note `number` keys wil be converted to `string`.
* @param array Array to transform.
*/
declare function deferred<T>(array: T[]): ObjectMap<T>;
declare type KeyFn<T> = (item: T, index: number) => string | number;
export {};