All files partition.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 3/3
100% Lines 15/15

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 52 53 54 55 56 57 58 59 608x                                                       8x 2x             8x               2x 2x 2x 16x 16x 4x 4x 4x     12x 12x     2x  
import { purry } from './purry';
import { Pred } from './_types';
import { _toLazyIndexed } from './_toLazyIndexed';
 
/**
 * Filter the elements of an array that meet the condition specified in a callback function.
 * @param array The array to filter.
 * @param fn the callback function.
 * @signature
 *    P.filter(array, fn)
 * @signature
 *    P.filter(fn)(array)
 * @example
 *    P.filter([1, 2, 3], x => x % 2 === 1) // => [1, 3]
 *
 *    P.pipe([1, 2, 3], P.filter(x => x % 2 === 1)) // => [1, 3]
 * @category Array, Pipe
 */
export function partition<T, S extends T>(
    array: readonly T[],
    fn: (value: T) => value is S
): [S[], Exclude<T, S>[]];
export function partition<T>(array: readonly T[], fn: Pred<T, boolean>): T[];
export function partition<T, S extends T>(
    fn: (input: T) => input is S
): (array: readonly T[]) => [S[], Exclude<T, S>[]];
export function partition<T>(fn: Pred<T, boolean>): (array: readonly T[]) => [T[], T[]];
 
export function partition() {
    return purry(_partition(), arguments);
}
export type PredIndexed<T, K> = (
    input: T,
    index: number,
    array: readonly T[]
) => K;
const _partition = () => <T>(
    array: T[],
    fn: (
        input: T,
        index: number,
        array: readonly T[]
    ) => any
) => {
    let right = []
    let left = []
    let index = 0
    for (const s of array) {
        if (fn(s, index, array)) {
            index++
            right.push(s)
            continue
        }
 
        index++
        left.push(s)
    }
 
    return [right, left]
};