All files map.ts

100% Statements 17/17
83.33% Branches 5/6
100% Functions 8/8
100% Lines 15/15

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 6222x 22x 22x                                       22x 52x     57x       2x             44x 57x 155x               22x               22x 5x   22x 22x    
import { purry } from './purry';
import { _reduceLazy, LazyResult } from './_reduceLazy';
import { _toLazyIndexed } from './_toLazyIndexed';
import { Pred, PredIndexedOptional, PredIndexed } from './_types';
 
/**
 * Map each element of an array using a defined callback function.
 * @param array The array to map.
 * @param fn The function mapper.
 * @returns The new mapped array.
 * @signature
 *    P.map(array, fn)
 * @signature
 *    P.map(fn)(array)
 * @example
 *    P.map([1, 2, 3], x => x * 2) // => [2, 4, 6]
 *    P.pipe([0, 1, 2], P.map(x => x * 2)) // => [2, 4, 6]
 * @category Array, Pipe
 */
export function map<T, K>(array: readonly T[], fn: Pred<T, K>): K[];
export function map<T, K>(fn: Pred<T, K>): (array: readonly T[]) => K[];
 
export function map() {
  return purry(_map(false), arguments, map.lazy);
}
 
const _map = (indexed: boolean) => <T, K>(
  array: readonly T[],
  fn: PredIndexedOptional<T, K>
) => {
  return _reduceLazy(
    array,
    indexed ? map.lazyIndexed(fn) : map.lazy(fn),
    indexed
  );
};
 
const _lazy = (indexed: boolean) => <T, K>(fn: PredIndexedOptional<T, K>) => {
  return (value: T, index?: number, array?: readonly T[]): LazyResult<K> => {
    return {
      done: false,
      hasNext: true,
      next: indexed ? fn(value, index, array) : fn(value),
    };
  };
};
 
export namespace map {
  export function indexed<T, K>(
    array: readonly T[],
    fn: PredIndexed<T, K>
  ): K[];
  export function indexed<T, K>(
    fn: PredIndexed<T, K>
  ): (array: readonly T[]) => K[];
  export function indexed() {
    return purry(_map(true), arguments, map.lazyIndexed);
  }
  export const lazy = _lazy(false);
  export const lazyIndexed = _toLazyIndexed(_lazy(true));
}