isEmpty
lang
Checks if value is an empty object, collection, map, or set.
Installation
Import
import { isEmpty } from '@tulx/utils';Source Code
Implementation
/**
* Checks if value is an empty object, collection, map, or set.
*
* @param value - The value to check.
* @returns Returns true if value is empty, else false.
*
* @example
* ```ts
* isEmpty(null); // true
* isEmpty(true); // true
* isEmpty(1); // true
* isEmpty([1, 2, 3]); // false
* isEmpty({ 'a': 1 }); // false
* isEmpty('abc'); // false
* isEmpty(new Map([['key', 'value']])); // false
* isEmpty(new Set([1, 2, 3])); // false
* ```
*/
export function isEmpty(value: unknown): boolean {
if (value === null || value === undefined) {
return true;
}
if (Array.isArray(value) || typeof value === 'string') {
return value.length === 0;
}
if (value instanceof Map || value instanceof Set) {
return value.size === 0;
}
if (typeof value === 'object') {
return Object.keys(value).length === 0;
}
return true;
}
Example
import { isEmpty } from '@tulx/utils';
isEmpty(null); // true
isEmpty(true); // true
isEmpty(1); // true
isEmpty([1, 2, 3]); // false
isEmpty({ 'a': 1 }); // false
isEmpty('abc'); // false
isEmpty(new Map([['key', 'value']])); // false
isEmpty(new Set([1, 2, 3])); // falseRelated Functions
castArray
Casts value as an array if it's not one.
clone
Creates a shallow clone of value.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.