🕳️ Checks if a collection (Array, Object, String, Map, Set, or ArrayBuffer) is empty.

Info

The following types are supported:

  • Array

  • Plain Object

  • String

  • Map

  • Set

  • ArrayBuffer

Syntax

TypeScript
import { isEmpty } from '@opentf/std';

isEmpty(val: unknown, sparse: boolean = false): boolean

Parameters

  • val: The value to check.

  • sparse: If true, it skips counting sparse array empty slots.

Returns

true if the collection is empty, false otherwise.

Examples

TypeScript
// --- Empty Cases ---
isEmpty('') //=> true
isEmpty([]) //=> true
isEmpty({}) //=> true
isEmpty(new Map()) //=> true
isEmpty(new Set()) //=> true
isEmpty(new ArrayBuffer(0)) //=> true
isEmpty(new Array(5), true) //=> true

// --- Non-Empty Cases ---
isEmpty(' ') //=> false
isEmpty('a') //=> false
isEmpty([1]) //=> false
isEmpty({ [Symbol('x')]: 1 }) //=> false
isEmpty({ length: 0, size: 0, byteLength: 0 }) //=> false
isEmpty([,]) //=> false

// --- Other Values ---
isEmpty(null) //=> false
isEmpty(undefined) //=> false
isEmpty(1) //=> false
isEmpty(false) //=> false
Last updated on
Edit this page