All files splitWhen.ts

100% Statements 8/8
100% Branches 2/2
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 378x 8x                                             8x 2x       2x 8x 1x     1x    
import { splitAt } from './splitAt';
import { purry } from './purry';
 
/**
 * Splits a given array at the first index where the given predicate returns true.
 * @param array the array to split
 * @param fn the predicate
 * @signature
 *    P.splitWhen(array, fn)
 * @signature
 *    P.splitWhen(fn)(array)
 * @example
 *    P.splitWhen([1, 2, 3], x => x === 2) // => [[1], [2, 3]]
 *    P.splitWhen(x => x === 2)([1, 2, 3]) // => [[1], [2, 3]]
 * @category Array, Pipe
 */
export function splitWhen<T>(
  array: readonly T[],
  fn: (item: T) => boolean
): [T[], T[]];
export function splitWhen<T>(
  fn: (item: T) => boolean
): (array: readonly T[]) => [T[], T[]];
 
export function splitWhen() {
  return purry(_splitWhen, arguments);
}
 
function _splitWhen<T>(array: T[], fn: (item: T) => boolean) {
  for (let i = 0; i < array.length; i++) {
    if (fn(array[i])) {
      return splitAt(array, i);
    }
  }
  return [array, []];
}