-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathis-empty.ts
50 lines (45 loc) · 913 Bytes
/
is-empty.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
/**
* Checks if `value` is an empty object or collection.
*
* @example
*
* isEmpty(null)
* // true
*
* isEmpty('')
* // true
*
* isEmpty({})
* // true
*
* isEmpty([])
* // true
*
* isEmpty({a: '1'})
* // false
*/
export function isEmpty(value: unknown): boolean {
// Check for null or undefined
if (value == null) {
return true;
}
// Check for empty string or array
if (typeof value === "string" || Array.isArray(value)) {
return value.length === 0;
}
// Check for empty Map or Set
if (value instanceof Map || value instanceof Set) {
return value.size === 0;
}
// Check for empty object
if (typeof value === "object") {
return Object.keys(value as object).length === 0;
}
// All other values are considered non-empty
return false;
}