All files take.ts

100% Statements 14/14
83.33% Branches 5/6
100% Functions 5/5
100% Lines 14/14

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 42 43 44 45 46 47 48 49 50 51 52 5314x 14x                                   14x 17x       2x     14x 14x 17x 48x 8x         40x 40x 16x           24x                
import { purry } from './purry';
import { _reduceLazy, LazyResult } from './_reduceLazy';
 
/**
 * Returns the first `n` elements of `array`.
 * @param array the array
 * @param n the number of elements to take
 * @signature
 *    P.take(array, n)
 * @signature
 *    P.take(n)(array)
 * @example
 *    P.take([1, 2, 3, 4, 3, 2, 1], 3) // => [1, 2, 3]
 *    P.pipe([1, 2, 3, 4, 3, 2, 1], P.take(n)) // => [1, 2, 3]
 * @category Array, Pipe
 */
export function take<T>(array: readonly T[], n: number): T[];
export function take<T>(n: number): (array: readonly T[]) => T[];
 
export function take() {
  return purry(_take, arguments, take.lazy);
}
 
function _take<T>(array: T[], n: number) {
  return _reduceLazy(array, take.lazy(n));
}
 
export namespace take {
  export function lazy<T>(n: number) {
    return (value: T): LazyResult<T> => {
      if (n === 0) {
        return {
          done: true,
          hasNext: false,
        };
      }
      n--;
      if (n === 0) {
        return {
          done: true,
          hasNext: true,
          next: value,
        };
      }
      return {
        done: false,
        hasNext: true,
        next: value,
      };
    };
  }
}