All files _reduceLazy.ts

100% Statements 8/8
100% Branches 6/6
100% Functions 2/2
100% Lines 8/8

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                                              16x         25x 82x 82x 12x 70x 48x   82x      
export type LazyResult<T> = LazyEmpty<T> | LazyNext<T> | LazyMany<T>;
 
interface LazyEmpty<T> {
  done: boolean;
  hasNext: false;
  hasMany?: false | undefined;
  next?: undefined;
}
 
interface LazyNext<T> {
  done: boolean;
  hasNext: true;
  hasMany?: false | undefined;
  next: T;
}
 
interface LazyMany<T> {
  done: boolean;
  hasNext: true;
  hasMany: true;
  next: T[];
}
 
export function _reduceLazy<T, K>(
  array: T[],
  lazy: (item: T, index?: number, array?: T[]) => LazyResult<K>,
  indexed?: boolean
) {
  return array.reduce((acc: K[], item, index) => {
    const result = indexed ? lazy(item, index, array) : lazy(item);
    if (result.hasMany === true) {
      acc.push(...result.next);
    } else if (result.hasNext === true) {
      acc.push(result.next);
    }
    return acc;
  }, []);
}