import { identityAsync } from '../util/identity' ; import { wrapWithAbort } from './operators/withabort' ; import { throwIfAborted } from '../aborterror' ; interface template TSource template TElement export interface ToMapOptions < TSource , TElement > { memberof keySelector : ( item : TSource , signal ? : AbortSignal ) => TElement | Promise < TElement > ; memberof elementSelector ? : ( item : TSource , signal ? : AbortSignal ) => TElement | Promise < TElement > ; type { AbortSignal } memberof signal ? : AbortSignal ; } template TSource template TKey template TElement param { AsyncIterable < TSource > } source param { ToMapOptions < TSource , TElement > } options returns { ( Promise < Map < TKey , TElement | TSource > > ) } export async function toMap < TSource , TKey , TElement = TSource > ( source , options ) { const { [ 'signal' ] : signal , [ 'elementSelector' ] : elementSelector = identityAsync as any , [ 'keySelector' ] : keySelector = identityAsync as any , } = options || { } ; throwIfAborted ( signal ) ; const map = new Map < TKey , TElement | TSource > ( ) ; for await ( const item of wrapWithAbort ( source , signal ) ) { const value = await elementSelector ! ( item , signal ) ; const key = await keySelector ( item , signal ) ; map . set ( key , value ) ; } return map ; }