Function split

  • Splits values into separate lists when predicate returns true (or resolves with true):

    import {pipe, split} from 'iter-ops';

    const input = [1, 2, 0, 3, 4, 0, 5, 6];

    const i = pipe(input, split(a => a === 0));

    console.log(...i); //=> [1, 2] [3, 4] [5, 6]

    When option toggle is set, the split uses the toggle start/end logic:

    import {pipe, split} from 'iter-ops';

    const input = [1, 2, 0, 3, 4, 0, 5, 6, 0, 7, 8];

    const i = pipe(input, split(a => a === 0, {toggle: true}));

    console.log(...i); //=> [3, 4] [7, 8]

    // i.e. first toggle starts collection, second one stops, third starts again, and so on.

    When you know only the split value of each block, you can use the default split mode, with carryEnd set to 1/forward (in case you do not want it skipped):

    import {pipe, split, SplitValueCarry} from 'iter-ops';

    const input = [1, 2, 0, 3, 4, 0, 5, 6, 0, 7, 8];

    const i = pipe(input, split(a => a === 0, {carryEnd: SplitValueCarry.forward}));

    console.log(...i); //=> [ 1, 2 ] [ 0, 3, 4 ] [ 0, 5, 6 ] [ 0, 7, 8 ]

    When you know only the end value of each block, you can use the default split mode, with carryEnd set to -1/back (in case you do not want it skipped):

    import {pipe, split, SplitValueCarry} from 'iter-ops';

    const input = [1, 2, 0, 3, 4, 0, 5, 6, 0, 7, 8];

    const i = pipe(input, split(a => a === 0, {carryEnd: SplitValueCarry.back}));

    console.log(...i); //=> [ 1, 2, 0 ] [ 3, 4, 0 ] [ 5, 6, 0 ] [ 7, 8 ]

    When you know both start and end values of each block, you can use the toggle mode, with carryStart set to 1/forward, and carryEnd set to -1/back, unless you want either of those skipped, then leave them at 0/none.

    Note that in toggle mode, you cannot use carryStart=back (it will be ignored), because it would delay emission of the current block indefinitely, plus carrying block start backward doesn't make much sense anyway.

    Note that the predicate can only return a Promise inside an asynchronous pipeline, or else the Promise will be treated as a truthy value.

    Type Parameters

    • T

    Parameters

    Returns Operation<T, T[]>