import { extremaBy } from './_extremaby.js'; import { ExtremaOptions } from './extremaoptions.js'; import { equalityComparerAsync } from '../util/comparer.js'; /** * Returns the elements in an async-enumerable sequence with the minimum key value. * * @template TSource The type of the elements in the source sequence. * @template TKey The type of the key computed for each element in the source sequence. * @param {AsyncIterable} source An async-iterable sequence to get the minimum elements for. * @param {ExtremaOptions} options The options which include an optional comparer and abort signal. * @returns {Promise} A promise containing a list of zero or more elements that have a minimum key value. */ export function minBy( source: AsyncIterable, options?: ExtremaOptions ): Promise { const { ['comparer']: comparer = equalityComparerAsync, ['selector']: selector, ['signal']: signal, } = options || {}; const newComparer = async (key: TKey, minValue: TKey) => -(await comparer(key, minValue)); return extremaBy(source, selector!, newComparer, signal); }