-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
203 lines (166 loc) · 4.92 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Module dependencies.
*/
var toString = Object.prototype.toString;
/**
* Expose.
*/
module.exports = {
/**
* Returns `true` if the passed value is an email.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isEmail: function (value) {
return "string" === typeof value && /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/.test(value);
},
/**
* Returns `true` if the passed value is an Array.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isArray: ("isArray" in Array) ? Array.isArray : function (value) {
return toString.call(value) === "[object Array]";
},
/**
* Returns `true` if the passed value is empty.
*
* @param {Object} value The value to test.
* @param {Boolean} allowEmptyString
* @return {Boolean}
*/
isEmpty: function (value, allowEmptyString) {
value = this.isString(value) && !allowEmptyString ? value.trim() : value;
return (value === null) || (value === undefined) || (!allowEmptyString ? value === '' : false) || (this.isArray(value) && value.length === 0);
},
/**
* Returns `true` if the passed value is a Date.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isDate: function (value) {
return toString.call(value) === "[object Date]";
},
/**
* Returns `true` is the passed value is a String.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isString: function (value) {
return "string" == typeof value;
},
/**
* Returns `true` if the passed value is a Boolean.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isBoolean: function(value) {
return typeof value === 'boolean';
},
/**
* Returns `true` if the passed value is a JavaScript Object, `false` otherwise.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isObject: (toString.call(null) === '[object Object]') ?
function(value) {
// check ownerDocument here as well to exclude DOM nodes
return value !== null && value !== undefined && toString.call(value) === '[object Object]' && value.ownerDocument === undefined;
} :
function(value) {
return toString.call(value) === '[object Object]';
},
/**
* Returns `true` if the passed value is a simple Object.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isSimpleObject: function(value) {
return value instanceof Object && value.constructor === Object;
},
/**
* Returns `true` if the passed value is a JavaScript 'primitive', a string, number or Boolean.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isPrimitive: function(value) {
var type = typeof value;
return type === 'string' || type === 'number' || type === 'boolean';
},
/**
* Returns `true` if the passed value is a JavaScript Function, `false` otherwise.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isFunction:
// Safari 3.x and 4.x returns 'function' for typeof <NodeList>, hence we need to fall back to using
// Object.prorotype.toString (slower)
(typeof document !== 'undefined' && typeof document.getElementsByTagName('body') === 'function') ? function(value) {
return toString.call(value) === '[object Function]';
} : function(value) {
return typeof value === 'function';
},
/**
* Returns `true` if the passed value is a number. Returns `false` for non-finite numbers.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isNumber: function(value) {
return typeof value === 'number' && isFinite(value);
},
/**
* Returns `true` if the passed value is numeric.
*
* @param {Object} value Examples: 1, '1', '2.34'
* @return {Boolean} `true` if numeric, `false` otherwise.
*/
isNumeric: function(value) {
return !isNaN(parseFloat(value)) && isFinite(value);
},
/**
* Returns `true` if the passed value is an HTMLElement.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isElement: function(value) {
return value ? value.nodeType === 1 : false;
},
/**
* Returns `true` if the passed value is a TextNode.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isTextNode: function(value) {
return value ? value.nodeName === "#text" : false;
},
/**
* Returns `true` if the passed value is defined.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isDefined: function(value) {
return typeof value !== 'undefined';
},
/**
* Returns `true` if the passed value is iterable, `false` otherwise.
*
* @param {Object} value The value to test.
* @return {Boolean}
*/
isIterable: function(value) {
return (value && typeof value !== 'string') ? value.length !== undefined : false;
}
};