UNPKG

1.06 kBJavaScriptView Raw
1import { equalityComparer } from '../util/comparer';
2import { identity } from '../util/identity';
3/**
4 * * Returns the minimum element with the optional selector.
5 *
6 * @export
7 * @template TSource The type of the elements in the source sequence.
8 * @param {Iterable<TSource>} source An iterable sequence to determine the minimum element of.
9 * @param {ExtremaByOptions<TKey>} [options] The options which include an optional comparer.
10 * @returns {TResult} The minimum element.
11 */
12export function min(source, options) {
13 const { ['comparer']: comparer = equalityComparer, ['selector']: selector = identity } = options || {};
14 const it = source[Symbol.iterator]();
15 let next = it.next();
16 if (next.done) {
17 throw new Error('Sequence contains no elements');
18 }
19 let minValue = selector(next.value);
20 while (!(next = it.next()).done) {
21 const current = selector(next.value);
22 if (comparer(current, minValue) < 0) {
23 minValue = current;
24 }
25 }
26 return minValue;
27}
28
29//# sourceMappingURL=min.mjs.map