Back to Es Toolkit

compact (Functional Programming)

docs/fp/reference/compact.md

1.49.0913 B
Original Source

compact (Functional Programming)

Creates a function that removes falsey values from an array. Use it with pipe.

typescript
const result = pipe(array, compact());

::: info

Prefer the original es-toolkit compact in ordinary code. Use this fp variant when composing transformations with pipe.

:::

Usage

compact removes false, null, undefined, 0, -0, 0n, an empty string, and NaN. It is lazy-capable inside pipe, so a trailing take can stop the walk early.

typescript
import { compact, pipe } from 'es-toolkit/fp';

pipe([0, 1, false, 2, '', 3], compact()); // => [1, 2, 3]

Parameters

This function takes no arguments; call it as compact().

Returns

((array: readonly T[]) => Array<NotFalsey<T>>): A function that maps a readonly T[] to an array without falsey values.