Skip to main content

dropWhile

dropWhile() function

Returns Iterable/AsyncIterable excluding elements dropped from the beginning. Elements are dropped until the value applied to f returns falsey.

Signature:
declare function dropWhile<A, B = unknown>(f: (a: A) => B, iterable: Iterable<A>): IterableIterator<A>;

declare function dropWhile<A, B = unknown>(f: (a: A) => B, iterable: AsyncIterable<A>): AsyncIterableIterator<A>;

declare function dropWhile<A extends Iterable<unknown> | AsyncIterable<unknown>, B>(f: (a: IterableInfer<A>) => B): (iterable: A) => ReturnIterableIteratorType<A>;

Example

const iter = dropWhile((a) => a < 3, [1, 2, 3, 4, 5]);
iter.next(); // {done:false, value: 3}
iter.next(); // {done:false, value: 4}
iter.next(); // {done:false, value: 5}

// with pipe
pipe(
[1, 2, 3, 4, 5],
dropWhile((a) => a < 3),
toArray,
); // [3, 4, 5]

await pipe(
Promise.resolve([1, 2, 3, 4, 5]),
dropWhile((a) => a < 3),
toArray,
); // [3, 4, 5]

// if you want to use asynchronous callback
await pipe(
Promise.resolve([1, 2, 3, 4, 5]),
toAsync,
dropWhile(async (a) => a < 3),
toArray,
); // [3, 4, 5]

// with toAsync
await pipe(
[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3), Promise.resolve(4), Promise.resolve(5)],
toAsync,
dropWhile((a) => a < 3),
toArray,
); // [3, 4, 5]

see pipe, toAsync, toArray

Open Source Code