All files / iterable/src dropWhile.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 3/3
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                              9x 9x         9x 28x   28x 18x        
import { Predicate } from "@benneq/predicate";
 
/**
 * Omits the first elements of an {@link Iterable} until a {@link Predicate}
 * returns `false`.
 *
 * @example
 * ```ts
 * const dropWhileLessThan3 = dropWhile(x => x < 3);
 * const iterable = dropWhileLessThan3([1,3,2,4]);
 * console.log(iterable); // [3,2,4]
 * ```
 *
 * @see {@link limit}, {@link skip} and {@link takeWhile} for similar operations.
 *
 * @typeParam T - the {@link Iterable} value type
 * @param predicate
 * @returns an {@link Iterable} that does not emit values until the given {@link Predicate} returns `false`
 */
type DropWhile = <T>(
  predicate: Predicate<[T]>
) => (iterable: Iterable<T>) => IterableIterator<T>;
 
export const dropWhile: DropWhile = (predicate) =>
  function* (
    iterable,
    // internal variables:
    drop = true
  ) {
    for (const value of iterable) {
      drop &&= predicate(value);
 
      if (!drop) {
        yield value;
      }
    }
  };