isEmpty
Determine if a value is empty
380 bytes
since v12.1.0
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']) // => falseEmpty values include:
nullundefined0- empty string
- empty array
- invalid
Datetime - object with
lengthproperty of0 - object with
sizeproperty 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')}