Function filter

  • Standard Array.filter logic for the iterable, extended with iteration state.

    In the example below, we are able to use a type guard to filter out all the nullable values.

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

    const i = pipe(
    [1, null, "foo"],
    filter((value): value is NonNullable<typeof value> => value != null)
    );

    Note: The asynchronous version of this function cannot make use of type guards.
    Give a :+1: to the Asynchronous Type Guards issue if you want support for this.

    Type Parameters

    • T
    • S = T

    Parameters

    • cb: ((value: T, index: number, state: IterationState) => value is S)
        • (value, index, state): value is S
        • Parameters

          Returns value is S

    Returns Operation<T, S>

  • Standard Array.filter logic for the iterable, extended with iteration state + async.

    In the example below, we take advantage of the IterationState, to detect and remove repeated values (do not confuse with distinct, which removes all duplicates).

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

    const i = pipe(
    iterable,
    filter((value, index, state) => {
    if(value === state.previousValue) {
    return false;
    }
    state.previousValue = value;
    return true;
    })
    );

    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

    • cb: ((value: T, index: number, state: IterationState) => boolean | Promise<boolean>)
        • (value, index, state): boolean | Promise<boolean>
        • Parameters

          Returns boolean | Promise<boolean>

    Returns Operation<T, T>