Skip to content

dropUntil() function

返回排除从开头删除的元素的 Iterable/AsyncIterable。删除元素直到应用于 f 的值返回真值。(包括第一个应用为 true 的值也会被删除)

Signature:

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

Example

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

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

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

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

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

see pipe, toAsync, toArray

Open Source Code

Released under the Apache-2.0 License.