Skip to content
VersionSize

compact

The compact utility creates a new array with all falsy values removed. This is particularly useful for cleaning up sparse arrays or removing optional values that weren't provided.

Implementation

View Source Code
ts
import { assert } from '../function/assert';
import { IS_ARRAY_ERROR_MSG, isArray } from '../typed/isArray';

/**
 * Removes falsy values from an array.
 *
 * @example
 * ```ts
 * const arr = [0, 1, false, 2, '', 3];
 * compact(arr); // [1, 2, 3]
 * ```
 *
 * @param array - The array to compact.
 *
 * @returns A new array with falsy values removed.
 *
 * @throws {TypeError} If the provided array is not an array.
 */
export function compact<T>(array: T[]): NonNullable<T>[] {
  assert(isArray(array), IS_ARRAY_ERROR_MSG, { args: { array }, type: TypeError });

  return array.filter(Boolean) as NonNullable<T>[];
}

Features

  • Isomorphic: Works in both Browser and Node.js.
  • Type-safe: Properly filters out null, undefined, and other falsy types from the resulting array type.
  • Immutable: Returns a new array, leaving the original unchanged.

API

ts
function compact<T>(array: T[]): NonNullable<T>[];

Parameters

  • array: The array to compact.

Returns

  • A new array containing only the truthy elements from the original array.

Examples

Cleaning Mixed Arrays

ts
import { compact } from '@vielzeug/toolkit';

const mixed = [0, 1, false, 2, '', 3, null, undefined, NaN, 'hello'];
const clean = compact(mixed);
// [1, 2, 3, 'hello']

Sanitizing Data Structures

ts
import { compact, map } from '@vielzeug/toolkit';

const users = [{ id: 1, name: 'Alice' }, null, { id: 2, name: 'Bob' }, undefined];

const validUsers = compact(users);
// [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]

Implementation Notes

  • The following values are considered falsy and will be removed: false, null, undefined, 0, "" (empty string), and NaN.
  • Throws TypeError if the first argument is not an array.
  • For deep cleaning of nested arrays, you may need to combine this with map or use a recursive approach.

See Also

  • filter: Filter elements based on a custom predicate.
  • uniq: Remove duplicate values from an array.
  • flatten: Flatten nested arrays into a single level.