Function flat

  • Expands / flattens sub-iterables up to the specified depth (default is 1).

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

    const i = pipe(
    ['one', [2, 3, [4, 5]]],
    flat(2)
    );

    console.log(...i); //=> 'o', 'n', 'e', 2, 3, 4, 5

    It implements the logic consistent with Array.flat, handling non-iterable values without throwing errors (unlike spread), and with optional depth support.

    Note that when handling a synchronous iterable, this operator can only expand synchronous sub-iterables. But when handling an asynchronous iterable, it can expand mixed sub-iterables, i.e. any combination of synchronous and asynchronous sub-iterables.

    Compare it to a more strict spread operator.

    Type Parameters

    • T

    • N extends number = 1

    Parameters

    • Optional depth: N

    Returns Operation<T, Flatten<T, N>>