UNPKG

729 BJavaScriptView Raw
1/**
2 * Returns the first element of an iterable sequence that matches the predicate if provided, or undefined if no such element exists.
3 *
4 * @export
5 * @template T The type of the elements in the source sequence.
6 * @param {Iterable<T>} source Source async-enumerable sequence.
7 * @returns {(S | undefined)} The first element in the iterable sequence, or undefined if no such element exists.
8 */
9export function first(source, options) {
10 const { ['thisArg']: thisArg, ['predicate']: predicate = () => true } = options || {};
11 let i = 0;
12 for (const item of source) {
13 if (predicate.call(thisArg, item, i++)) {
14 return item;
15 }
16 }
17 return undefined;
18}
19
20//# sourceMappingURL=first.mjs.map