All files forEach.ts

100% Statements 20/20
83.33% Branches 5/6
100% Functions 8/8
100% Lines 18/18

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 931x 1x 1x                                                                                                     1x 3x     6x       2x             2x 6x 16x 8x   8x   16x               1x     1x 3x   1x 1x    
import { purry } from './purry';
import { _reduceLazy, LazyResult } from './_reduceLazy';
import { _toLazyIndexed } from './_toLazyIndexed';
import { Pred, PredIndexedOptional, PredIndexed } from './_types';
 
/**
 * Iterate an array using a defined callback function. The original array is returned instead of `void`.
 * @param array The array.
 * @param fn The callback function.
 * @returns The original array
 * @signature
 *    R.forEach(array, fn)
 *    R.forEach.indexed(array, fn)
 * @example
 *    R.forEach([1, 2, 3], x => {
 *      console.log(x)
 *    }) // => [1, 2, 3]
 *    R.forEach.indexed([1, 2, 3], (x, i) => {
 *      console.log(x, i)
 *    }) // => [1, 2, 3]
 * @data_first
 * @indexed
 * @pipeable
 * @category Array
 */
export function forEach<T>(array: T[], fn: Pred<T, void>): T[];
 
/**
 * Iterate an array using a defined callback function. The original array is returned instead of `void`.
 * @param fn the function mapper
 * @signature
 *    R.forEach(fn)(array)
 *    R.forEach.indexed(fn)(array)
 * @example
 *    R.pipe(
 *      [1, 2, 3],
 *      R.forEach(x => {
 *        console.log(x)
 *      })
 *    ) // => [1, 2, 3]
 *    R.pipe(
 *      [1, 2, 3],
 *      R.forEach.indexed((x, i) => {
 *        console.log(x, i)
 *      })
 *    ) // => [1, 2, 3]
 * @data_last
 * @indexed
 * @pipeable
 * @category Array
 */
export function forEach<T>(fn: Pred<T, void>): (array: T[]) => T[];
 
export function forEach() {
  return purry(_forEach(false), arguments, forEach.lazy);
}
 
const _forEach = (indexed: boolean) => <T, K>(
  array: T[],
  fn: PredIndexedOptional<T, K>
) => {
  return _reduceLazy(
    array,
    indexed ? forEach.lazyIndexed(fn) : forEach.lazy(fn),
    indexed
  );
};
 
const _lazy = (indexed: boolean) => <T>(fn: PredIndexedOptional<T, void>) => {
  return (value: T, index?: number, array?: T[]): LazyResult<T> => {
    if (indexed) {
      fn(value, index, array);
    } else {
      fn(value);
    }
    return {
      done: false,
      hasNext: true,
      next: value,
    };
  };
};
 
export namespace forEach {
  export function indexed<T>(array: T[], fn: PredIndexed<T, void>): T[];
  export function indexed<T>(fn: PredIndexed<T, void>): (array: T[]) => T[];
  export function indexed() {
    return purry(_forEach(true), arguments, forEach.lazyIndexed);
  }
  export const lazy = _lazy(false);
  export const lazyIndexed = _toLazyIndexed(_lazy(true));
}