forked from mrDarcyMurphy/is
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis-too.js
More file actions
629 lines (546 loc) · 12.7 KB
/
is-too.js
File metadata and controls
629 lines (546 loc) · 12.7 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*!
* is-too - simple validations and type-checking
* https://github.com/LoudBit/is-too
* copyright Loud Bit LLC
* MIT License
*/
// PRIVATE HELPERS
/**
* sort style comparison of provided dates
*
* @method sortDates
* @param {Date} subject
* @param {Date} standard, optional
*/
function sortDates(x, y) {
y = !is.date(y) ? new Date() : y
var a = 'getFullYear', b = 'getMonth', c = 'getDate'
return x[a]() < y[a]() ? -1
: x[a]() > y[a]() ? 1
: x[b]() < y[b]() ? -1
: x[b]() > y[b]() ? 1
: x[c]() < y[c]() ? -1
: x[c]() > y[c]() ? 1
: 0
}
function reghex(x) {
return x.replace && x.replace(/^-?\+?(0x)?/i, '') || x
}
var is = {
// OPERATORS
/**
* Is the subject not null, undefined, or an empty string?
*
* @method present
* @param {Object} subject, required
*/
present: function(x) {
return x != null && !is.emtStr(x)
},
/**
* Is the subject loosely equivalent to the standard?
*
* @method equal
* @param {Object} subject, required
* @param {Object} standard, required
*/
equal: function(x, y) {
return x == y
},
/**
* Is the subject loosely different from the standard?
*
* @method equal
* @param {Object} subject, required
* @param {Object} standard, required
*/
unequal: function(x, y) {
return x != y
},
/**
* Is the subject strictly equivalent to the standard?
*
* @method exactly
* @param {Object} subject, required
* @param {Object} standard, required
*/
exactly: function(x, y) {
return x === y
},
/**
* Is the subject strictly equivalent to the standard?
*
* @method exactly
* @param {Object} subject, required
* @param {Object} standard, required
*/
notExactly: function(x, y) {
return x !== y
},
/**
* Is the subject greater than the standard?
*
* Performs basic JS greater than operator, which will cast values
* http://bclary.com/2004/11/07/#a-11.8.2
*
* @method greaterThan
* @param {Object} subject
* @param {Object} standard, optional, defaults to 0
*/
greaterThan: function(x, y) {
y = y || 0
return x > y
},
/**
* Is the subject greater than or equal to the standard?
*
* Performs basic JS greater than or equal to operator, which will cast values
* http://bclary.com/2004/11/07/#a-11.8.4
*
* @method greaterThanOrEqualTo
* @param {Object} subject
* @param {Object} standard, optional, defaults to 0
*/
greaterThanOrEqualTo: function(x, y) {
y = y || 0
return x >= y
},
/**
* Is the subject less than the standard?
*
* Performs basic JS less than operator, which will cast values
* http://bclary.com/2004/11/07/#a-11.8.1
*
* @method lessThan
* @param {Object} subject
* @param {Object} standard, optional, defaults to 0
*/
lessThan: function(x, y) {
y = y || 0
return x < y
},
/**
* Is the subject less than or equal to the standard?
*
* Performs basic JS less than or equal to operator, which will cast values
* http://bclary.com/2004/11/07/#a-11.8.3
*
* @method lessThanOrEqualTo
* @param {Object} subject
* @param {Object} standard, optional, defaults to 0
*/
lessThanOrEqualTo: function(x, y) {
y = y || 0
return x <= y
},
/**
* Is the subject an instance of the standard?
*
* @method instanceOf
* @param {Object} subject
* @param {Object} standard, optional, defaults to 0
*/
instanceOf: function(x, y) {
return is.present(y) && x instanceof y
},
/**
* Is the subject longer than the standard?
*
* Performs basic JS longer than operator, which will cast values
* http://bclary.com/2004/11/07/#a-11.8.2
*
* @method longerThan
* @param {Object} subject
* @param {Number} standard, optional, defaults to 0
*/
longerThan: function(x, y) {
y = y || 0
return !is.undef(x) && !is.nil(x) && x.length > y
},
/**
* Is the subject longer than or equal to the standard?
*
* @method longerThanOrEqualTo
* @param {Object} subject
* @param {Number} standard, optional, defaults to 0
*/
longerThanOrEqualTo: function(x, y) {
y = y || 0
return !is.undef(x) && !is.nil(x) && x.length >= y
},
/**
* Is the subject shorter than the standard?
*
* Performs basic JS shorter than operator, which will cast values
* http://bclary.com/2004/11/07/#a-11.8.2
*
* @method shorterThan
* @param {Object} subject
* @param {Number} standard, optional, defaults to 0
*/
shorterThan: function(x, y) {
y = y || 0
return !is.undef(x) && !is.nil(x) && x.length < y
},
/**
* Is the subject shorter than or equal to the standard?
*
* @method shorterThanOrEqualTo
* @param {Object} subject
* @param {Number} standard, optional, defaults to 0
*/
shorterThanOrEqualTo: function(x, y) {
y = y || 0
return !is.undef(x) && !is.nil(x) && x.length <= y
},
// PRIMITIVES
/**
* Is the subject a boolean?
*
* @method boolean
* @param {Boolean} subject, required
*/
boolean: function(x) {
return typeof x == 'boolean'
},
/**
* Is the subject exactly null?
*
* @method nil
* @param {Null} subject, required
*/
nil: function(x) {
return x === null
},
/**
* Is the subject exactly undefined?
*
* @method undefined
* @param {undefined} subject, required
*/
undef: function(x) {
return x === void 0
},
/**
* Is the subject exactly false?
*
* @method false
* @param {Object} subject
*/
false: function(x) {
return x === false
},
/**
* Is the subject falsy?
*
* @method false
* @param {Object} subject
*/
falsy: function(x) {
return !x
},
/**
* Is the subject exactly true?
*
* @method true
* @param {Object} subject
*/
true: function(x) {
return x === true
},
/**
* Is the subject truthy?
*
* @method truthy
* @param {Object} subject
*/
truthy: function(x) {
return !!x
},
// OBJECTS
/**
* Is the subject an Object?
*
* @method object
* @param {Object} subject, required
*/
object: function(x) {
return typeof x == 'object'
},
/**
* Is the subject a plain Object?
*
* @method plainObject
* @param {Object} subject, required
*/
plainObject: function(x) {
// covers most things
if (!x || !is.obj(x) || x.nodeType || is.args(x) || is.arr(x) || is.bool(x) || is.date(x) || is.func(x) || is.num(x) || is.str(x) || is.rx(x))
return false
// Object.create(null) creates objects w/o `valueOf` which throws when the object is an argument of isNaN
// yet that should evaluate to `true`
return x.valueOf && isNaN(x) || true
},
/**
* Is the subject an arguments object?
*
* @method array
* @param {Arguments} subject, required
*/
args: function(x) {
return Object.prototype.toString.call(x) == '[object Arguments]'
},
/**
* Is the subject an array?
*
* @method array
* @param {Array} subject, required
*/
array: function(x) {
return Array.isArray(x)
},
/**
* Is the subject a function?
*
* @method func
* @param {Function} subject, required
*/
func: function(x) {
return typeof x == 'function'
},
// REGULAR EXPRESSIONS
/**
* Is the subject a regular expression?
*
* @method regex
* @param {RegEx} subject, required
*/
regex: function(x) {
return Object.prototype.toString.call(x) == '[object RegExp]'
},
/**
* Does the subject match the provided regular expression?
*
* @method match
* @param {String} subject, required
* @param {RegExp} standard, required
*/
match: function(x, rx) {
return is.rx(rx) && rx.test(x)
},
// STRINGS
/**
* Is the subject a string?
*
* @method string
* @param {String} subject, required
*/
string: function(x) {
return typeof x == 'string'
},
/**
* Is the subject an empty string?
* Empty being nothing or only whitespace.
*
* @method emptyString
* @param {String} subject, required
*/
emptyString: function(x) {
return is.str(x) ? x.replace(/\s/g, '') === '' : false
},
// NUMBERS
/**
* Is the subject a number?
*
* @method number
* @param {Object} subject, required
*/
number: function(x) {
return typeof x === 'number' && !isNaN(x)
},
/**
* Is the subject a number, or a number in a string?
*
* @method numberString
* @param {Object} subject, required
*/
numberString: function(x) {
return is.str(x) && (is.hexstr(x) || !isNaN(parseFloat(reghex(x))))
},
/**
* Is the subject an integer?
*
* @method integer
* @param {Number} subject, required
*/
integer: function(x) {
return typeof x === 'number' && x % 1 === 0
},
/**
* Is the subject an integer, or an integer in a string?
*
* @method integerString
* @param {Object} subject, required
*/
integerString: function(x) {
return is.str(x) && is.int(is.toNum(reghex(x)))
},
/**
* Is the subject a hexadecimal number?
*
* @method integer
* @param {Number} subject, required
*/
hexadecimal: function(x) {
return is.not.str(x) && parseInt(x, 16).toString(16) == x
},
/**
* Is the subject a hexadecimal string?
*
* @method integer
* @param {Number} subject, required
*/
hexadecimalString: function(x) {
if (is.not.str(x))
return false
var z = reghex(x)
return parseInt(z, 16).toString(16) == z
},
/**
* Is the subject exactly zero?
*
* @method zero
* @param {Number} subject, required
*/
zero: function(x) {
return x === 0
},
/**
* Is the subject zero, or a zero in a string?
*
* @method aZero
* @param {Object} subject, required
*/
zeroString: function(x) {
return is.str(x) && is.zero(is.toNum(reghex(x)))
},
// DATES
/**
* Is the subject a date?
*
* @method date
* @param {Object} required
*/
date: function(x) {
return Object.prototype.toString.call(x) == '[object Date]'
},
/**
* Is the subject the same date as standard?
* Does not test date times.
*
* @method sameDate
* @param {Date} subject, required
* @param {Date} standard, required
*/
sameDate: function(x, y) {
return is.date(x) && is.date(y) && !sortDates(x, y)
},
/**
* Is the subject today?
*
* @method today
* @param {Date} subject
*/
today: function(x) {
return is.date(x) && !sortDates(x)
},
/**
* Is the subject a date in the future, or in the future of the provided standard?
* Does not test date times.
*
* @method futureDate
* @param {Date} subject, required
* @param {Date} standard, optional
*/
futureDate: function(x, y) {
return is.date(x) && sortDates(x, y) > 0
},
/**
* Is the subject a date in the past, or in the past of the provided standard?
* Does not test date times.
*
* @method pastDate
* @param {Date} subject
* @param {Date} standard, optional
*/
pastDate: function(x, y) {
return is.date(x) && sortDates(x, y) < 0
}
}
// ALIASES
is.required = is.present
is.eq = is.equal
is.gt = is.greaterThan
is.gte = is.greaterThanOrEqualTo
is.lt = is.lessThan
is.lte = is.lessThanOrEqualTo
is.inst = is.instanceOf
is.lengthy = is.longerThan
is.minlength = is.longerThanOrEqualTo
is.maxlength = is.shorterThanOrEqualTo
is.fn = is.func
is.bool = is.boolean
is.obj = is.object
is.arr = is.array
is.rx = is.regex
is.str = is.string
is.emtStr = is.emptyString
is.numstr = is.numberString
is.intstr = is.integerString
is.num = is.number
is.int = is.integer
is.hex = is.hexadecimal
is.hexStr = is.hexstr = is.hexadecimalString
is.past = is.pastDate
is.future = is.futureDate
// ANTONYMS
/**
* Creates `is.not` object and attaches all existing methods to it.
* They return the opposite of their `is.methodName` counterparts.
*/
var isnt = is.not = {}
var method
for (method in is) {
// grab all methods except toThing helpers
if (is.hasOwnProperty(method) && typeof is[method] == 'function') {
isnt[method] = (function(method){
return function(){
return !is[method].apply(is, arguments)
}
})(method)
}
}
// PUBLIC HELPERS
/**
* Attempts to convert subject to an integer.
* Will cast `NaN` to `null` because you can still do math with `null`.
*
* @method toInteger
* @param {Object} subject, required
*/
is.toInt = is.toInteger = function(x) {
var y = parseInt(x,10)
return isNaN(y) ? null : y
}
/**
* Attempts to convert subject to a number.
* Will cast `NaN` to `null` because you can still do math with `null`.
*
* @method toNumber
* @param {Object} subject, required
*/
is.toNum = is.toNumber = function(x) {
var y = parseFloat(x)
return isNaN(y) ? null : y
}
module.exports = is