All files flatten.ts

100% Statements 11/11
75% Branches 3/4
100% Functions 5/5
100% Lines 11/11

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 5210x 10x                                             10x 21x       18x     10x 10x 21x 46x 43x             3x                
import { _reduceLazy, LazyResult } from './_reduceLazy';
import { purry } from './purry';
 
type Flatten<T> = T extends ReadonlyArray<infer K> ? K : T;
 
/**
 * Flattens `array` a single level deep.
 * Note: In `pipe`, use `flatten()` form instead of `flatten`. Otherwise, the inferred type is lost.
 
 * @param items the target array
 * @signature P.flatten(array)
 * @example
 *    P.flatten([[1, 2], [3], [4, 5]]) // => [1, 2, 3, 4, 5]
 *    P.pipe(
 *      [[1, 2], [3], [4, 5]],
 *      P.flatten(),
 *    ); // => [1, 2, 3, 4, 5]
 * @category Array
 * @pipeable
 */
export function flatten<T>(items: readonly T[]): Array<Flatten<T>>;
 
export function flatten<T>(): (items: readonly T[]) => Array<Flatten<T>>;
 
export function flatten() {
  return purry(_flatten, arguments, flatten.lazy);
}
 
function _flatten<T>(items: T[]): Array<Flatten<T>> {
  return _reduceLazy(items, flatten.lazy());
}
 
export namespace flatten {
  export function lazy<T>() {
    return (next: T): LazyResult<any> => {
      if (Array.isArray(next)) {
        return {
          done: false,
          hasNext: true,
          hasMany: true,
          next: next,
        };
      }
      return {
        done: false,
        hasNext: true,
        next,
      };
    };
  }
}