isEmpty
Determine if a value is empty
493 bytes
Usage
Pass in a value and get a boolean telling you if the value is empty.
import * as _ from 'radashi'
_.isEmpty([]) // => true_.isEmpty('') // => true
_.isEmpty('hello') // => false_.isEmpty(['hello']) // => false
Empty values include:
null
undefined
0
- empty string
- empty array
- invalid
Date
time - object with
length
property of0
- object with
size
property of0
- object with no enumerable keys
Type Guards
In some cases, isEmpty
acts as a type guard, which helps TypeScript infer more specific types based on the check.
Due to TypeScript limitations, object types cannot be narrowed, except for arrays and functions.
import * as _ from 'radashi'
// Example with stringconst value1: string = ''if (_.isEmpty(value1)) { // TypeScript infers value1 as '' console.log('Value is an empty string')} else { // TypeScript infers value1 as string console.log('Value is a non-empty string')}
// Example with arrayconst value2: string[] = []if (_.isEmpty(value2)) { // TypeScript infers value2 as never[] console.log('Value is an empty array')} else { // TypeScript infers value2 as string[] console.log('Value is a non-empty array')}