-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplyis.js
375 lines (342 loc) · 12.7 KB
/
simplyis.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
(function(global, factory) {
'use strict';
if (typeof module === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define([], factory);
} else {
window.simplyIs = factory(_);
}
}(typeof window !== "undefined" ? window : this, function factory() {
var type = function(x) {
return {
type: Object.prototype.toString.call(x).replace('[object ', '').replace(']', '').toLowerCase(),
is: function(y) {
return this.type === y;
}
};
};
var forIn = function(obj, callback) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
callback(obj[key], key, obj)
}
}
}
var clone = function(x) {
var copy;
if (type(x).is('date')) {
copy = new Date();
copy.setTime(x.getTime());
return copy;
}
if (type(x).is('array')) {
copy = [].concat(x);
return copy;
}
if (type(x).is('object')) {
copy = {};
forIn(x, function(value, key) {
copy[key] = value;
});
return copy;
};
if (type(x).is('null') || type(x).is('undefined') || !type(x).is('object')) {
return x;
}
}
var REGEXP = {
EMAIL: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
ALPHANUMERIC: /^[a-zA-Z0-9]*$/,
ALPHA: /^[a-zA-Z ]*$/,
NUMERIC: /^[0-9]*$/,
PHONE: /^[0-9-()+. ]*$/
}
var simplyIs = (function() {
// Private
var check = {
if: function() {
//variable arguments x,testfor,not
var args = [];
for (var a = 0; a < arguments.length; a++) {
args.push(arguments[a]);
}
var not = args.pop(); //last argument is NOT/reverse, determines whether or not to reverse the result
var testfor = args.pop(); //second to last argument is the type of test
if (not) {
return !this['is_' + testfor].apply(this, args);
} else {
return this['is_' + testfor].apply(this, args);
}
},
is_alpha: function(x){
return REGEXP.ALPHA.test(String(x));
},
is_alphanumeric: function(x){
return REGEXP.ALPHANUMERIC.test(String(x));
},
is_argument: function(x) {
return type(x).is('arguments');
},
is_array: function(x) {
return type(x).is('array');
},
is_date: function(x) {
return type(x).is('date');
},
is_boolean: function(x) {
return type(x).is('boolean');
},
is_decimal: function(x) {
return this.is_number(x) && !this.is_infinite(x) && !this.is_nan(x) && (x.toString().indexOf('.') > 0);
},
is_defined: function(x) {
return !this.is_undefined(x);
},
is_email: function(x){
return REGEXP.EMAIL.test(String(x));
},
is_empty: function(x) {
if (this.is_undefined(x) || this.is_null(x)) {
return true;
}
if (this.is_string(x)) {
return x === "";
}
if (this.is_array(x)) {
return x.length === 0;
}
if (this.is_object(x)) {
for (var prop in x) {
if (x.hasOwnProperty(prop)) {
return false;
}
}
return true;
}
return !x;
},
is_error: function(x) {
return type(x).is('error');
},
is_even: function(x) {
return this.is_number(x) && !this.is_infinite(x) && !this.is_nan(x) && x % 2 === 0;
},
is_equal: function(x, y) {
if (this.is_object(x) && this.is_object(y)) {
// do they have the same properties?
if (Object.keys(x).sort().join(",") !== Object.keys(y).sort().join(",")) {
return false;
} else {
// Check if all values are equal
// For some reason, forIn isn't working here.
// Not a scoping issue, perhaps has to do with breaking out of the loop from the callback?
for (var key in x) {
if (x.hasOwnProperty(key)) {
if (x[key] !== y[key]) {
return false;
}
}
}
return true;
}
} else {
return x === y;
}
},
is_false: function(x) {
return x === false;
},
is_function: function(x) {
return type(x).is('function');
},
is_infinite: function(x) {
return this.is_number(x) && (x === Infinity || x === -Infinity);
},
is_integer: function(x) {
return this.is_number(x) && !this.is_infinite(x) && !this.is_nan(x) && (x.toString().indexOf('.') <= 0);
},
is_inArray: function(x, arr) {
if (!this.is_array(arr)) {
throw new TypeError('check.is_inArray needs a valid array');
} else {
return arr.indexOf(x) >= 0;
}
},
is_inObject: function(x, obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key) && (key === x || obj[key] === x)) {
return true;
}
}
return false;
},
is_instanceOf: function(child, parentConstructor) {
return child instanceof parentConstructor;
},
is_inside: function(x, target) {
if (this.is_object(target)) {
return this.is_inObject(x, target);
} else if (this.is_array(target)) {
return this.is_inArray(x, target);
} else if (this.is_string(target)) {
return target.indexOf(x) >= 0;
}
return false;
},
is_json: function(x) {
if (this.is_string(x)) {
try {
if (this.is_object(JSON.parse(x)) || this.is_array(JSON.parse(x))) {
return true;
}
} catch (e) {
//console.log(e.name + ": " + e.message);
return false;
}
}
return false;
},
is_nan: function(x) {
return this.is_number(x) && isNaN(x);
},
is_null: function(x) {
return type(x).is('null');
},
is_numeric: function(x){
return REGEXP.NUMERIC.test(String(x));
},
is_number: function(x) {
return type(x).is('number');
},
is_numberInvalid: function(x) {
return this.is_nan(x) || this.is_infinite(x) || !this.is_number(x)
},
is_numberValid: function(x){
return !this.is_numberInvalid(x);
},
is_object: function(x) {
return type(x).is('object');
},
is_odd: function(x) {
return this.is_number(x) && !this.is_infinite(x) && !this.is_nan(x) && x % 2 !== 0;
},
is_phone: function(x){
return REGEXP.PHONE.test(String(x)) && x.length >= 9;
},
is_regexp: function(x) {
return type(x).is('regexp');
},
is_sameType: function(obj, typecompare){
var check = this;
var result = true;
forIn(obj, function(val,key){
// if the type comparison object doesn't have the key, don't restrain/check it
if( check.is_defined(typecompare[key]) ){
if( !type(val).is( typecompare[key].toLowerCase() ) ){
result = false;
return false;
}
}
});
return result;
},
is_string: function(x) {
return type(x).is('string');
},
is_true: function(x) {
return x === true;
},
is_undefined: function(x) {
return type(x).is('undefined');
}
};
// Reassigning all methods of the check object that starts with is_ to the is object
// check.is_number() ---> is.number()
var is = {};
is.negate = false;
forIn(check, function(value, key) {
if (key.indexOf('is_') >= 0) {
key = key.replace('is_', '');
is[key] = function() { //variable arguments
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// additional arguments
args.push(key);
args.push(this.negate);
return check.if.apply(check, args);
};
}
});
// chaining
is.a = is;
is.an = is;
// negation
is.not = clone(is);
is.not.negate = true;
/*
* The exported library can be both:
* (1) A function that returns an IS object with methods that call itself with the given value
*
* is(3).number(); // example usage
*
* is.number --> function(x){...} // is
* is.number --> function(){ is.number(x) } // form of is returned by calling the function
*
* (2) An is 'object' (properties are added to the function)
* is.number(3);
*
*/
var isfunc = function() { //variable arguments
var newobj = {};
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// these functions need more than one argument
var functions = ['equal', 'inside', 'inArray', 'inObject', 'instanceOf', 'sameType', 'siblingOf'];
// Go through all properties of the is object
forIn(is, function(value, key) {
// we only care about the functions
if (is.function(is[key])) {
// if the method is inside the list of functions that need more than one argument
if (functions.indexOf(key) >= 0) {
newobj[key] = function(target) {
var newargs = [];
newargs = args.concat([target]);
return is[key].apply(this, newargs);
};
} else {
// lazy initialization, only calculate the result when you need it, instead of doing all calculations beforehand and saving the result
Object.defineProperty(newobj, key, {
get: function() {
return is[key].apply(is, args);
}
});
}
} else {
newobj[key] = is[key];
}
});
newobj.a = newobj;
newobj.an = newobj;
newobj.not = clone(newobj);
newobj.not.negate = true;
return newobj;
};
for (var key in is) {
if (is.hasOwnProperty(key)) {
isfunc[key] = is[key];
}
}
return isfunc;
})();
if (typeof window === 'object') {
window.simplyIs = simplyIs;
} else {
this.simplyIs = simplyIs;
}
return simplyIs;
}));