All files flatMap.ts

92.31% Statements 12/13
50% Branches 2/4
100% Functions 6/6
91.67% Lines 11/12

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 601x 1x                                                           1x 3x       4x     1x 1x 1x 2x 2x 2x                              
import { flatten } from './flatten';
import { purry } from './purry';
 
/**
 * Map each element of an array using a defined callback function and flatten the mapped result.
 * @param array The array to map.
 * @param fn The function mapper.
 * @signature
 *    R.flatMap(array, fn)
 * @example
 *    R.flatMap([1, 2, 3], x => [x, x * 10]) // => [1, 10, 2, 20, 3, 30]
 * @data_first
 * @pipeable
 * @category Array
 */
export function flatMap<T, K>(array: T[], fn: (input: T) => K | K[]): K[];
 
/**
 * Map each element of an array using a defined callback function and flatten the mapped result.
 * @param array The array to map.
 * @param fn The function mapper.
 * @signature
 *    R.flatMap(fn)(array)
 * @example
 *    R.pipe([1, 2, 3], R.flatMap(x => [x, x * 10])) // => [1, 10, 2, 20, 3, 30]
 * @data_last
 * @pipeable
 * @category Array
 */
export function flatMap<T, K>(fn: (input: T) => K | K[]): (array: T[]) => K[];
 
export function flatMap() {
  return purry(_flatMap, arguments, flatMap.lazy);
}
 
function _flatMap<T, K>(array: T[], fn: (input: T) => K[]): K[] {
  return flatten(array.map(item => fn(item)));
}
 
export namespace flatMap {
  export function lazy<T, K>(fn: (input: T) => K | K[]) {
    return (value: T) => {
      const next = fn(value);
      Eif (Array.isArray(next)) {
        return {
          done: false,
          hasNext: true,
          hasMany: true,
          next: next,
        };
      }
      return {
        done: false,
        hasNext: true,
        next,
      };
    };
  }
}