All files takeWhile.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 2/2
100% Lines 9/9

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 398x                                               8x 2x       2x 8x 8x 2x   6x   2x    
import { purry } from './purry';
 
/**
 * Returns elements from the array until predicate returns false.
 * @param array the array
 * @param fn the predicate
 * @signature
 *    P.takeWhile(array, fn)
 * @signature
 *    P.takeWhile(fn)(array)
 * @example
 *    P.takeWhile([1, 2, 3, 4, 3, 2, 1], x => x !== 4) // => [1, 2, 3]
 *    P.pipe([1, 2, 3, 4, 3, 2, 1], P.takeWhile(x => x !== 4))  // => [1, 2, 3]
 * @category Array, Pipe
 */
export function takeWhile<T>(
  array: readonly T[],
  fn: (item: T) => boolean
): T[];
 
export function takeWhile<T>(
  fn: (item: T) => boolean
): (array: readonly T[]) => T[];
 
export function takeWhile() {
  return purry(_takeWhile, arguments);
}
 
function _takeWhile<T>(array: T[], fn: (item: T) => boolean) {
  const ret: T[] = [];
  for (const item of array) {
    if (!fn(item)) {
      break;
    }
    ret.push(item);
  }
  return ret;
}