Newer
Older
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
Returns true if the passed value is null or undefined.
hasNoValue(); // true
hasNoValue(null); // true
hasNoValue(undefined); // true
hasNoValue(''); // false
hasNoValue([]); // false
hasNoValue(function() {}); // false
*/
export function hasNoValue(obj?: any): boolean {
return obj === null || obj === undefined;
}
/**
Returns true if the passed value is not null or undefined.
hasValue(); // false
hasValue(null); // false
hasValue(undefined); // false
hasValue(''); // true
hasValue([]); // true
hasValue(function() {}); // true
*/
export function hasValue(obj?: any): boolean {
return !hasNoValue(obj);
}
/**
Verifies that a value is `null` or an empty string, empty array,
or empty function.
isEmpty(); // true
isEmpty(null); // true
isEmpty(undefined); // true
isEmpty(''); // true
isEmpty([]); // true
isEmpty({}); // false
isEmpty('Adam Hawkins'); // false
isEmpty([0,1,2]); // false
isEmpty('\n\t'); // false
isEmpty(' '); // false
*/
export function isEmpty(obj?: any): boolean {
if (hasNoValue(obj)) {
return true;
}
if (typeof obj.size === 'number') {
return !obj.size;
}
let objectType = typeof obj;
if (objectType === 'object') {
let size = obj['size'];
if (typeof size === 'number') {
return !size;
}
}
if (typeof obj.length === 'number' && objectType !== 'function') {
return !obj.length;
}
if (objectType === 'object') {
let length = obj['length'];
if (typeof length === 'number') {
return !length;
}
}
return false;
}
/**
Verifies that a value is not `null`, an empty string, empty array,
or empty function.
isNotEmpty(); // false
isNotEmpty(null); // false
isNotEmpty(undefined); // false
isNotEmpty(''); // false
isNotEmpty([]); // false
isNotEmpty({}); // true
isNotEmpty('Adam Hawkins'); // true
isNotEmpty([0,1,2]); // true
isNotEmpty('\n\t'); // true
isNotEmpty(' '); // true
*/
export function isNotEmpty(obj?: any): boolean {
return !isEmpty(obj);
}