All files flatMap.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 3/3
100% Lines 5/5

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 479x 9x                                                                     9x 16x             35x    
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
 *    P.flatMap(array, fn)
 * @example
 *    P.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: readonly T[],
  fn: (input: T) => K | readonly 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
 *    P.flatMap(fn)(array)
 * @example
 *    P.pipe([1, 2, 3], P.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: readonly T[]) => readonly K[];
 
export function flatMap() {
  return purry(_flatMap, arguments);
}
 
function _flatMap<T, K>(
  array: readonly T[],
  fn: (input: T, index: number, arr: readonly T[]) => K[]
): readonly K[] {
  return flatten(array.map((item, index, array) => fn(item, index, array)));
}