-
Notifications
You must be signed in to change notification settings - Fork 0
/
isEmpty.ts
27 lines (26 loc) · 857 Bytes
/
isEmpty.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.
* @param value - The value to check.
* @returns True if the value is an empty object/collection, false otherwise.
* @example
* ```typescript
* isEmpty([]); // true
* isEmpty({}); // true
* isEmpty(''); // true
* isEmpty([1, 2]); // false
* isEmpty({ a: 1, b: 2 }); // false
* isEmpty('text'); // false
* isEmpty(123); // true - type is not considered a collection
* isEmpty(true); // true - type is not considered a collection
* ```
* @category Type
* @since 0.1.0
* @public
*/
const isEmpty = (value: any): boolean => {
if (value === null || value === undefined) return true;
const keys = Object.keys(value);
if (keys.length) return false;
return !value.length;
};
export default isEmpty;