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 39 40 41 421x                                                     1x 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
 *    R.takeWhile(array, fn)
 * @example
 *    R.takeWhile([1, 2, 3, 4, 3, 2, 1], x => x !== 4) // => [1, 2, 3]
 * @data_first
 * @category Array
 */
export function takeWhile<T>(array: T[], fn: (item: T) => boolean): T[];
 
/**
 * Returns elements from the array until predicate returns false.
 * @param fn the predicate
 * @signature
 *    R.takeWhile(fn)(array)
 * @example
 *    R.pipe([1, 2, 3, 4, 3, 2, 1], R.takeWhile(x => x !== 4))  // => [1, 2, 3]
 * @data_last
 * @category Array
 */
export function takeWhile<T>(fn: (item: T) => boolean): (array: 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;
}