@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
31 lines (29 loc) • 1.25 kB
JavaScript
import { comparer } from '../util/comparer';
import { wrapWithAbort } from './operators/withabort';
import { throwIfAborted } from '../aborterror';
/**
* Determines whether an async-itreable includes a certain value among its entries, returning true or false as appropriate.
*
* @export
* @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 async function includes(source, valueToFind, fromIndex = 0, signal) {
throwIfAborted(signal);
let fromIdx = fromIndex;
let i = 0;
if (Math.abs(fromIdx)) {
fromIdx = 0;
}
for await (const item of wrapWithAbort(source, signal)) {
if (i++ > fromIdx && comparer(item, valueToFind)) {
return true;
}
}
return false;
}
//# sourceMappingURL=includes.mjs.map