UNPKG

ix

Version:

The Interactive Extensions for JavaScript

46 lines (44 loc) 1.87 kB
import { __asyncValues, __awaiter } from "tslib"; import { comparer } from '../util/comparer.mjs'; import { wrapWithAbort } from './operators/withabort.mjs'; import { throwIfAborted } from '../aborterror.mjs'; /** * Determines whether an async-itreable includes a certain value among its entries, returning true or false as appropriate. * * @template T The type of elements in the source sequence. * @param {AsyncIterable<T>} source The source sequence to search for the item. * @param {T} valueToFind The value to search for. * @param {number} [fromIndex=0] The position in this async-iterable at which to begin searching for valueToFind. * @param {AbortSignal} [signal] An optional abort signal to cancel the operation at any time. * @returns {Promise<boolean>} Returns a promise containing true if the value valueToFind is found within the async-iterable. */ export function includes(source, valueToFind, fromIndex = 0, signal) { var _a, e_1, _b, _c; return __awaiter(this, void 0, void 0, function* () { throwIfAborted(signal); let fromIdx = fromIndex; let i = 0; if (Math.abs(fromIdx)) { fromIdx = 0; } try { for (var _d = true, _e = __asyncValues(wrapWithAbort(source, signal)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const item = _c; if (i++ > fromIdx && comparer(item, valueToFind)) { return true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } return false; }); } //# sourceMappingURL=includes.mjs.map