@rx-angular/cdk
Version:
@rx-angular/cdk is a Component Development Kit for ergonomic and highly performant angular applications. It helps to to build Large scale applications, UI libs, state management, rendering systems and much more. Furthermore the unique way of mixing reacti
53 lines (52 loc) • 1.57 kB
TypeScript
import { OnlyKeysOfSpecificType } from '../_internals/guards';
/**
* @description
* Converts an array of objects to a dictionary {[key: string]: T}.
* Accepts array T[] and key of type string, number or symbol as inputs.
*
*
* @example
*
* const creatures = [{id: 1, type: 'cat'}, {id: 2, type: 'dog'}, {id: 3, type: 'parrot'}];
*
* const creaturesDictionary = toDictionary(creatures, 'id');
*
* // creaturesDictionary will be:
* // {
* // 1: {id: 1, type: 'cat'},
* // 2: {id: 2, type: 'dog'},
* // 3: {id: 3, type: 'parrot'}
* // };
* @example
* // Usage with RxState
*
* export class ListComponent {
*
* readonly convertToDictionary$ = new Subject();
*
* constructor(private state: RxState<ComponentState>) {
* // Reactive implementation
* state.connect(
* 'creaturesDictionary',
* this.convertToDictionary$,
* ({ creatures }) => {
* return toDictionary(creatures, 'id');
* }
* );
* }
*
* // Imperative implementation
* convertToDictionary(): void {
* this.state.set({ creaturesDictionary: toDictionary(this.state.get().creatures, 'id'});
* }
* }
*
* @see {@link OnlyKeysOfSpecificType}
* @param {OnlyKeysOfSpecificType<T, S>} key
* @returns { [key: string]: T[] }
* @docsPage toDictionary
* @docsCategory transformation-helpers
*/
export declare function toDictionary<T extends object>(source: T[], key: OnlyKeysOfSpecificType<T, number> | OnlyKeysOfSpecificType<T, string> | OnlyKeysOfSpecificType<T, symbol>): {
[key: string]: T;
};