Skip to main content

compact

compact() function

Returns Iterable/AsyncIterable with all null and undefined values removed.

Signature:
declare function compact<T extends Iterable<unknown> | AsyncIterable<unknown>>(iterable: T): ReturnIterableIteratorType<T, NonNullable<IterableInfer<T>>>;

Example

const iter = compact([0, 1, undefined, 3, null]);
iter.next() // {done:false, value: 0}
iter.next() // {done:false, value: 1}
iter.next() // {done:false, value: 3}
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
[0, 1, undefined, 3, null],
compact,
toArray,
); // [0, 1, 3]

await pipe(
Promise.resolve([0, 1, undefined, 3, null]),
compact,
toArray,
); // [0, 1, 3]

// with toAsync
await pipe(
[Promise.resolve(0), Promise.resolve(1), Promise.resolve(undefined),
Promise.resolve(3), Promise.resolve(null)],
toAsync,
compact,
toArray,
); // [0, 1, 3]

Try It

see pipe, toAsync, toArray

Open Source Code