Assert
isEmpty

Checks if the given collection is empty.

The following type of values are supported:

  • Array

  • Plain Object

  • String

  • Map

  • Set

  • ArrayBuffer

Syntax

import { isEmpty } from '@opentf/std';
 
isEmpty(val: unknown, sparse = false): boolean;

If sparse is true, then it skips counting Sparsed (opens in a new tab) array empty slots.

Examples

// Truthy 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
 
// Falsy cases
// *********
 
isEmpty() //=> false
 
isEmpty(undefined) //=> false
 
isEmpty(null) //=> false
 
isEmpty(1) //=> false
 
isEmpty(1.5) //=> false
 
isEmpty(1n) //=> false
 
isEmpty(false) //=> false
 
isEmpty(true) //=> false
 
isEmpty(' ') //=> false
 
isEmpty('a') //=> false
 
isEmpty([1]) //=> false
 
isEmpty({ length: 0, size: 0, byteLength: 0 }) //=> false
 
isEmpty([,]) //=> false

Try