UNPKG

@reactivex/ix-es5-esm

Version:

The Interactive Extensions for JavaScript

43 lines (41 loc) 1.73 kB
import { __values } from "tslib"; /** * Returns the only element of an iterable sequence that matches the predicate if specified, * or undefined if no such element exists; this method reports an exception if there is more * than one element in the iterable sequence. * * @template T The type of the elements in the source sequence. * @param {AsyncIterable<T>} source Source iterable sequence. * @param {OptionalFindOptions<T>} [options] The optional options which includes a predicate for filtering, * and thisArg for predicate binding. * @returns {(T | undefined)} The single element in the iterable sequence that satisfies * the condition in the predicate, or undefined if no such element exists. */ export function single(source, options) { var e_1, _a; var _b = options || {}, thisArg = _b["thisArg"], _c = _b["predicate"], predicate = _c === void 0 ? function () { return true; } : _c; var result; var hasResult = false; var i = 0; try { for (var source_1 = __values(source), source_1_1 = source_1.next(); !source_1_1.done; source_1_1 = source_1.next()) { var item = source_1_1.value; if (hasResult && predicate.call(thisArg, item, i++)) { throw new Error('More than one element was found'); } if (predicate.call(thisArg, item, i++)) { result = item; hasResult = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (source_1_1 && !source_1_1.done && (_a = source_1.return)) _a.call(source_1); } finally { if (e_1) throw e_1.error; } } return result; } //# sourceMappingURL=single.js.map