All files / seq/operator chunkBySize.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 3/3
100% Lines 13/13

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 231x       7x 14x 14x 2x   12x 12x 48x 48x 20x 20x     12x 6x        
import {toSequence} from '../build';
import {Seq} from 'fnxt/fnxt-types';
 
 
export const chunkBySize = <E>(count: number) => (seq1: Seq<E>): Seq<E[]> =>
  toSequence(function* () {
    if (count <= 0) {
      throw new Error('count must be greater then 0');
    }
    let arr = [];
    for (const e of seq1) {
      arr.push(e);
      if (arr.length + 1 > count) { // +1 > to handle decimals
        yield arr;
        arr = [];
      }
    }
    if (arr.length) {
      yield arr;
    }
  });