takeUntil
takeUntil() function
Returns Iterable/AsyncIterable that taken values until truthy when given f
is applied.
declare function takeUntil<A, B>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<A>;
declare function takeUntil<A, B>(f: (a: A) => B, iterable: AsyncIterable<A>): AsyncIterableIterator<A>;
declare function takeUntil<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(f: (a: IterableInfer<A>) => B): (iterable: A) => ReturnIterableIteratorType<A>;
Example
const iter = takeUntil(a => a % 2 === 0, [1, 2, 3, 4, 5, 6]);
iter.next() // {done:false, value: 1}
iter.next() // {done:false, value: 2}
iter.next() // {done:true, value: undefined}
// with pipe
pipe(
[1, 2, 3, 4, 5, 6],
takeUntil(a => a % 2 === 0),
toArray,
); // [1, 2]
await pipe(
Promise.resolve([1, 2, 3, 4, 5, 6]),
takeUntil(a => a % 2 === 0),
toArray,
); // [1, 2]
// if you want to use asynchronous callback
await pipe(
Promise.resolve([1, 2, 3, 4, 5, 6]),
toAsync,
takeUntil(async (a) => a % 2 === 0),
toArray,
); // [1, 2]
// with toAsync
await pipe(
[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3),
Promise.resolve(4), Promise.resolve(5), Promise.resolve(6)],
toAsync,
takeUntil(a => a % 2 === 0),
toArray,
); // [1, 2]