UNPKG

1.25 kBJavaScriptView Raw
1import { comparer } from '../util/comparer';
2import { wrapWithAbort } from './operators/withabort';
3import { throwIfAborted } from '../aborterror';
4/**
5 * Determines whether an async-itreable includes a certain value among its entries, returning true or false as appropriate.
6 *
7 * @export
8 * @template T The type of elements in the source sequence.
9 * @param {AsyncIterable<T>} source The source sequence to search for the item.
10 * @param {T} valueToFind The value to search for.
11 * @param {number} [fromIndex=0] The position in this async-iterable at which to begin searching for valueToFind.
12 * @param {AbortSignal} [signal] An optional abort signal to cancel the operation at any time.
13 * @returns {Promise<boolean>} Returns a promise containing true if the value valueToFind is found within the async-iterable.
14 */
15export async function includes(source, valueToFind, fromIndex = 0, signal) {
16 throwIfAborted(signal);
17 let fromIdx = fromIndex;
18 let i = 0;
19 if (Math.abs(fromIdx)) {
20 fromIdx = 0;
21 }
22 for await (const item of wrapWithAbort(source, signal)) {
23 if (i++ > fromIdx && comparer(item, valueToFind)) {
24 return true;
25 }
26 }
27 return false;
28}
29
30//# sourceMappingURL=includes.mjs.map