-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (39 loc) · 1.07 KB
/
index.js
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
/**
* @param {Mixed} val
* @return {Boolean}
*/
const empty = value => {
// If value is null on undefined:
if (value == null) return true;
// By types:
if ('boolean' == typeof value) return !value;
if ('number' == typeof value) return value === 0;
if ('string' == typeof value) return value === '0' || value.length === 0;
if ('function' == typeof value) return value.length === 0;
// Empty arrays:
if (Array.isArray(value)) return value.length === 0;
// Error with empty message:
if (value instanceof Error) return value.message === '';
// Empty objects:
if (value.toString == Object.prototype.toString) {
switch (value.toString()) {
case '[object File]':
case '[object Map]':
case '[object Set]': {
return value.size === 0
}
case '[object Object]': {
for (let key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) return false
}
return true
}
}
}
// Everything else...
return false
}
/**
* @type {Function}
*/
module.exports = empty;