-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.js
107 lines (96 loc) · 3.22 KB
/
utils.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* utils.js :: contains helper functions that are used for deep-cleaner.
*/
'use strict';
module.exports = {
/**
* repr :: gets the string representation of `arg`
* @param {} arg :: unknown function argument
* @returns {String} :: a string representation of `arg`
*/
repr: function(arg) {
return Object.prototype.toString.call(arg);
},
/**
* isArray
* @param {} arg :: unknown function argument
* @returns {Boolean} :: returns true if `arg` is an Array, false otherwise
*/
isArray: function(arg) {
return Array.isArray ? Array.isArray(arg) : this.repr(arg) === '[object Array]';
},
/**
* isObject :: checks if `arg` is an object.
* @param {} arg :: unknown function argument
* @returns {Boolean} :: returns true if `arg` is an object.
*/
isObject: function(arg) {
return this.repr(arg) === '[object Object]';
},
/**
* isTruthyish :: checks if `arg` is not null or undefined.
*
* for example, statements like `!""`, `!0`, `!null`, or `!undefined`
* evaluate to `true`. However, sometimes deep-cleaner is only interested
* if something is null or undefined, so `isTruthyish("")` and
* `isTruthyish(0)` evaluate to `true`, while `isTruthyish(null)` and
* `isTruthyish(undefined)` still evaluate to `false`.
*
* @param {} arg :: unknown function argument.
* @returns {Boolean}
*/
isTruthyish: function(arg) {
if (arg === false) {
return false;
}
return !(this.isNull(arg) || this.isUndefined(arg));
},
/**
* isString :: checks if `arg` is a string.
* @param {} arg :: unknown function argument
* @returns {Boolean} :: returns true if `arg` is a String, false otherwise
*/
isString: function(arg) {
return this.repr(arg) === '[object String]';
},
/**
* isNull :: checks if `arg` is null.
* @param {} arg :: unknown function argument
* @returns {Boolean} :: returns true if `arg` is of type Null, false otherwise
*/
isNull: function(arg) {
return this.repr(arg) === '[object Null]';
},
/**
* isUndefined :: checks if `arg` is undefined.
* @param {} arg :: unknown function argument
* @returns {Boolean} :: Returns true if `arg` is of type Undefined, false otherwise
*/
isUndefined: function(arg) {
try {
return typeof(arg) === 'undefined';
} catch (e) {
if (e instanceof ReferenceError) {
return true;
}
throw e;
}
},
/**
* isEmpty :: Checks if `arg` is an empty string, array, or object.
*
* @param {} arg :: unknown function argument
* @returns {Boolean} :: Returns true if `arg` is an empty string,
* array, or object. Also returns true is `arg` is null or
* undefined. Returns true otherwise.
*/
isEmpty: function(arg) {
return (
this.isUndefined(arg)
|| this.isNull(arg)
|| (this.isString(arg) && arg.length === 0)
|| (this.isArray(arg) && arg.length === 0)
|| (this.isObject(arg) && Object.keys(arg).length === 0)
);
},
};