프로젝트

일반

사용자정보

통계
| 개정판:

root / HServer / 00.Server / 00.Program / node_modules / is / index.js

이력 | 보기 | 이력해설 | 다운로드 (14.8 KB)

1 39 HKM
2
/**!
3
 * is
4
 * the definitive JavaScript type testing library
5
 *
6
 * @copyright 2013 Enrico Marino
7
 * @license MIT
8
 */
9
10
var objProto = Object.prototype;
11
var owns = objProto.hasOwnProperty;
12
var toString = objProto.toString;
13
var isActualNaN = function (value) {
14
  return value !== value;
15
};
16
var NON_HOST_TYPES = {
17
  "boolean": 1,
18
  "number": 1,
19
  "string": 1,
20
  "undefined": 1
21
};
22
23
/**
24
 * Expose `is`
25
 */
26
27
var is = module.exports = {};
28
29
/**
30
 * Test general.
31
 */
32
33
/**
34
 * is.type
35
 * Test if `value` is a type of `type`.
36
 *
37
 * @param {Mixed} value value to test
38
 * @param {String} type type
39
 * @return {Boolean} true if `value` is a type of `type`, false otherwise
40
 * @api public
41
 */
42
43
is.a =
44
is.type = function (value, type) {
45
  return typeof value === type;
46
};
47
48
/**
49
 * is.defined
50
 * Test if `value` is defined.
51
 *
52
 * @param {Mixed} value value to test
53
 * @return {Boolean} true if 'value' is defined, false otherwise
54
 * @api public
55
 */
56
57
is.defined = function (value) {
58
  return value !== undefined;
59
};
60
61
/**
62
 * is.empty
63
 * Test if `value` is empty.
64
 *
65
 * @param {Mixed} value value to test
66
 * @return {Boolean} true if `value` is empty, false otherwise
67
 * @api public
68
 */
69
70
is.empty = function (value) {
71
  var type = toString.call(value);
72
  var key;
73
74
  if ('[object Array]' === type || '[object Arguments]' === type) {
75
    return value.length === 0;
76
  }
77
78
  if ('[object Object]' === type) {
79
    for (key in value) if (owns.call(value, key)) return false;
80
    return true;
81
  }
82
83
  if ('[object String]' === type) {
84
    return '' === value;
85
  }
86
87
  return false;
88
};
89
90
/**
91
 * is.equal
92
 * Test if `value` is equal to `other`.
93
 *
94
 * @param {Mixed} value value to test
95
 * @param {Mixed} other value to compare with
96
 * @return {Boolean} true if `value` is equal to `other`, false otherwise
97
 */
98
99
is.equal = function (value, other) {
100
  var type = toString.call(value)
101
  var key;
102
103
  if (type !== toString.call(other)) {
104
    return false;
105
  }
106
107
  if ('[object Object]' === type) {
108
    for (key in value) {
109
      if (!is.equal(value[key], other[key])) {
110
        return false;
111
      }
112
    }
113
    return true;
114
  }
115
116
  if ('[object Array]' === type) {
117
    key = value.length;
118
    if (key !== other.length) {
119
      return false;
120
    }
121
    while (--key) {
122
      if (!is.equal(value[key], other[key])) {
123
        return false;
124
      }
125
    }
126
    return true;
127
  }
128
129
  if ('[object Function]' === type) {
130
    return value.prototype === other.prototype;
131
  }
132
133
  if ('[object Date]' === type) {
134
    return value.getTime() === other.getTime();
135
  }
136
137
  return value === other;
138
};
139
140
/**
141
 * is.hosted
142
 * Test if `value` is hosted by `host`.
143
 *
144
 * @param {Mixed} value to test
145
 * @param {Mixed} host host to test with
146
 * @return {Boolean} true if `value` is hosted by `host`, false otherwise
147
 * @api public
148
 */
149
150
is.hosted = function (value, host) {
151
  var type = typeof host[value];
152
  return type === 'object' ? !!host[value] : !NON_HOST_TYPES[type];
153
};
154
155
/**
156
 * is.instance
157
 * Test if `value` is an instance of `constructor`.
158
 *
159
 * @param {Mixed} value value to test
160
 * @return {Boolean} true if `value` is an instance of `constructor`
161
 * @api public
162
 */
163
164
is.instance = is['instanceof'] = function (value, constructor) {
165
  return value instanceof constructor;
166
};
167
168
/**
169
 * is.null
170
 * Test if `value` is null.
171
 *
172
 * @param {Mixed} value value to test
173
 * @return {Boolean} true if `value` is null, false otherwise
174
 * @api public
175
 */
176
177
is['null'] = function (value) {
178
  return value === null;
179
};
180
181
/**
182
 * is.undefined
183
 * Test if `value` is undefined.
184
 *
185
 * @param {Mixed} value value to test
186
 * @return {Boolean} true if `value` is undefined, false otherwise
187
 * @api public
188
 */
189
190
is.undefined = function (value) {
191
  return value === undefined;
192
};
193
194
/**
195
 * Test arguments.
196
 */
197
198
/**
199
 * is.arguments
200
 * Test if `value` is an arguments object.
201
 *
202
 * @param {Mixed} value value to test
203
 * @return {Boolean} true if `value` is an arguments object, false otherwise
204
 * @api public
205
 */
206
207
is.arguments = function (value) {
208
  var isStandardArguments = '[object Arguments]' === toString.call(value);
209
  var isOldArguments = !is.array(value) && is.arraylike(value) && is.object(value) && is.fn(value.callee);
210
  return isStandardArguments || isOldArguments;
211
};
212
213
/**
214
 * Test array.
215
 */
216
217
/**
218
 * is.array
219
 * Test if 'value' is an array.
220
 *
221
 * @param {Mixed} value value to test
222
 * @return {Boolean} true if `value` is an array, false otherwise
223
 * @api public
224
 */
225
226
is.array = function (value) {
227
  return '[object Array]' === toString.call(value);
228
};
229
230
/**
231
 * is.arguments.empty
232
 * Test if `value` is an empty arguments object.
233
 *
234
 * @param {Mixed} value value to test
235
 * @return {Boolean} true if `value` is an empty arguments object, false otherwise
236
 * @api public
237
 */
238
is.arguments.empty = function (value) {
239
  return is.arguments(value) && value.length === 0;
240
};
241
242
/**
243
 * is.array.empty
244
 * Test if `value` is an empty array.
245
 *
246
 * @param {Mixed} value value to test
247
 * @return {Boolean} true if `value` is an empty array, false otherwise
248
 * @api public
249
 */
250
is.array.empty = function (value) {
251
  return is.array(value) && value.length === 0;
252
};
253
254
/**
255
 * is.arraylike
256
 * Test if `value` is an arraylike object.
257
 *
258
 * @param {Mixed} value value to test
259
 * @return {Boolean} true if `value` is an arguments object, false otherwise
260
 * @api public
261
 */
262
263
is.arraylike = function (value) {
264
  return !!value && !is.boolean(value)
265
    && owns.call(value, 'length')
266
    && isFinite(value.length)
267
    && is.number(value.length)
268
    && value.length >= 0;
269
};
270
271
/**
272
 * Test boolean.
273
 */
274
275
/**
276
 * is.boolean
277
 * Test if `value` is a boolean.
278
 *
279
 * @param {Mixed} value value to test
280
 * @return {Boolean} true if `value` is a boolean, false otherwise
281
 * @api public
282
 */
283
284
is.boolean = function (value) {
285
  return '[object Boolean]' === toString.call(value);
286
};
287
288
/**
289
 * is.false
290
 * Test if `value` is false.
291
 *
292
 * @param {Mixed} value value to test
293
 * @return {Boolean} true if `value` is false, false otherwise
294
 * @api public
295
 */
296
297
is['false'] = function (value) {
298
  return is.boolean(value) && (value === false || value.valueOf() === false);
299
};
300
301
/**
302
 * is.true
303
 * Test if `value` is true.
304
 *
305
 * @param {Mixed} value value to test
306
 * @return {Boolean} true if `value` is true, false otherwise
307
 * @api public
308
 */
309
310
is['true'] = function (value) {
311
  return is.boolean(value) && (value === true || value.valueOf() === true);
312
};
313
314
/**
315
 * Test date.
316
 */
317
318
/**
319
 * is.date
320
 * Test if `value` is a date.
321
 *
322
 * @param {Mixed} value value to test
323
 * @return {Boolean} true if `value` is a date, false otherwise
324
 * @api public
325
 */
326
327
is.date = function (value) {
328
  return '[object Date]' === toString.call(value);
329
};
330
331
/**
332
 * Test element.
333
 */
334
335
/**
336
 * is.element
337
 * Test if `value` is an html element.
338
 *
339
 * @param {Mixed} value value to test
340
 * @return {Boolean} true if `value` is an HTML Element, false otherwise
341
 * @api public
342
 */
343
344
is.element = function (value) {
345
  return value !== undefined
346
    && typeof HTMLElement !== 'undefined'
347
    && value instanceof HTMLElement
348
    && value.nodeType === 1;
349
};
350
351
/**
352
 * Test error.
353
 */
354
355
/**
356
 * is.error
357
 * Test if `value` is an error object.
358
 *
359
 * @param {Mixed} value value to test
360
 * @return {Boolean} true if `value` is an error object, false otherwise
361
 * @api public
362
 */
363
364
is.error = function (value) {
365
  return '[object Error]' === toString.call(value);
366
};
367
368
/**
369
 * Test function.
370
 */
371
372
/**
373
 * is.fn / is.function (deprecated)
374
 * Test if `value` is a function.
375
 *
376
 * @param {Mixed} value value to test
377
 * @return {Boolean} true if `value` is a function, false otherwise
378
 * @api public
379
 */
380
381
is.fn = is['function'] = function (value) {
382
  var isAlert = typeof window !== 'undefined' && value === window.alert;
383
  return isAlert || '[object Function]' === toString.call(value);
384
};
385
386
/**
387
 * Test number.
388
 */
389
390
/**
391
 * is.number
392
 * Test if `value` is a number.
393
 *
394
 * @param {Mixed} value value to test
395
 * @return {Boolean} true if `value` is a number, false otherwise
396
 * @api public
397
 */
398
399
is.number = function (value) {
400
  return '[object Number]' === toString.call(value);
401
};
402
403
/**
404
 * is.infinite
405
 * Test if `value` is positive or negative infinity.
406
 *
407
 * @param {Mixed} value value to test
408
 * @return {Boolean} true if `value` is positive or negative Infinity, false otherwise
409
 * @api public
410
 */
411
is.infinite = function (value) {
412
  return value === Infinity || value === -Infinity;
413
};
414
415
/**
416
 * is.decimal
417
 * Test if `value` is a decimal number.
418
 *
419
 * @param {Mixed} value value to test
420
 * @return {Boolean} true if `value` is a decimal number, false otherwise
421
 * @api public
422
 */
423
424
is.decimal = function (value) {
425
  return is.number(value) && !isActualNaN(value) && !is.infinite(value) && value % 1 !== 0;
426
};
427
428
/**
429
 * is.divisibleBy
430
 * Test if `value` is divisible by `n`.
431
 *
432
 * @param {Number} value value to test
433
 * @param {Number} n dividend
434
 * @return {Boolean} true if `value` is divisible by `n`, false otherwise
435
 * @api public
436
 */
437
438
is.divisibleBy = function (value, n) {
439
  var isDividendInfinite = is.infinite(value);
440
  var isDivisorInfinite = is.infinite(n);
441
  var isNonZeroNumber = is.number(value) && !isActualNaN(value) && is.number(n) && !isActualNaN(n) && n !== 0;
442
  return isDividendInfinite || isDivisorInfinite || (isNonZeroNumber && value % n === 0);
443
};
444
445
/**
446
 * is.int
447
 * Test if `value` is an integer.
448
 *
449
 * @param value to test
450
 * @return {Boolean} true if `value` is an integer, false otherwise
451
 * @api public
452
 */
453
454
is.int = function (value) {
455
  return is.number(value) && !isActualNaN(value) && value % 1 === 0;
456
};
457
458
/**
459
 * is.maximum
460
 * Test if `value` is greater than 'others' values.
461
 *
462
 * @param {Number} value value to test
463
 * @param {Array} others values to compare with
464
 * @return {Boolean} true if `value` is greater than `others` values
465
 * @api public
466
 */
467
468
is.maximum = function (value, others) {
469
  if (isActualNaN(value)) {
470
    throw new TypeError('NaN is not a valid value');
471
  } else if (!is.arraylike(others)) {
472
    throw new TypeError('second argument must be array-like');
473
  }
474
  var len = others.length;
475
476
  while (--len >= 0) {
477
    if (value < others[len]) {
478
      return false;
479
    }
480
  }
481
482
  return true;
483
};
484
485
/**
486
 * is.minimum
487
 * Test if `value` is less than `others` values.
488
 *
489
 * @param {Number} value value to test
490
 * @param {Array} others values to compare with
491
 * @return {Boolean} true if `value` is less than `others` values
492
 * @api public
493
 */
494
495
is.minimum = function (value, others) {
496
  if (isActualNaN(value)) {
497
    throw new TypeError('NaN is not a valid value');
498
  } else if (!is.arraylike(others)) {
499
    throw new TypeError('second argument must be array-like');
500
  }
501
  var len = others.length;
502
503
  while (--len >= 0) {
504
    if (value > others[len]) {
505
      return false;
506
    }
507
  }
508
509
  return true;
510
};
511
512
/**
513
 * is.nan
514
 * Test if `value` is not a number.
515
 *
516
 * @param {Mixed} value value to test
517
 * @return {Boolean} true if `value` is not a number, false otherwise
518
 * @api public
519
 */
520
521
is.nan = function (value) {
522
  return !is.number(value) || value !== value;
523
};
524
525
/**
526
 * is.even
527
 * Test if `value` is an even number.
528
 *
529
 * @param {Number} value value to test
530
 * @return {Boolean} true if `value` is an even number, false otherwise
531
 * @api public
532
 */
533
534
is.even = function (value) {
535
  return is.infinite(value) || (is.number(value) && value === value && value % 2 === 0);
536
};
537
538
/**
539
 * is.odd
540
 * Test if `value` is an odd number.
541
 *
542
 * @param {Number} value value to test
543
 * @return {Boolean} true if `value` is an odd number, false otherwise
544
 * @api public
545
 */
546
547
is.odd = function (value) {
548
  return is.infinite(value) || (is.number(value) && value === value && value % 2 !== 0);
549
};
550
551
/**
552
 * is.ge
553
 * Test if `value` is greater than or equal to `other`.
554
 *
555
 * @param {Number} value value to test
556
 * @param {Number} other value to compare with
557
 * @return {Boolean}
558
 * @api public
559
 */
560
561
is.ge = function (value, other) {
562
  if (isActualNaN(value) || isActualNaN(other)) {
563
    throw new TypeError('NaN is not a valid value');
564
  }
565
  return !is.infinite(value) && !is.infinite(other) && value >= other;
566
};
567
568
/**
569
 * is.gt
570
 * Test if `value` is greater than `other`.
571
 *
572
 * @param {Number} value value to test
573
 * @param {Number} other value to compare with
574
 * @return {Boolean}
575
 * @api public
576
 */
577
578
is.gt = function (value, other) {
579
  if (isActualNaN(value) || isActualNaN(other)) {
580
    throw new TypeError('NaN is not a valid value');
581
  }
582
  return !is.infinite(value) && !is.infinite(other) && value > other;
583
};
584
585
/**
586
 * is.le
587
 * Test if `value` is less than or equal to `other`.
588
 *
589
 * @param {Number} value value to test
590
 * @param {Number} other value to compare with
591
 * @return {Boolean} if 'value' is less than or equal to 'other'
592
 * @api public
593
 */
594
595
is.le = function (value, other) {
596
  if (isActualNaN(value) || isActualNaN(other)) {
597
    throw new TypeError('NaN is not a valid value');
598
  }
599
  return !is.infinite(value) && !is.infinite(other) && value <= other;
600
};
601
602
/**
603
 * is.lt
604
 * Test if `value` is less than `other`.
605
 *
606
 * @param {Number} value value to test
607
 * @param {Number} other value to compare with
608
 * @return {Boolean} if `value` is less than `other`
609
 * @api public
610
 */
611
612
is.lt = function (value, other) {
613
  if (isActualNaN(value) || isActualNaN(other)) {
614
    throw new TypeError('NaN is not a valid value');
615
  }
616
  return !is.infinite(value) && !is.infinite(other) && value < other;
617
};
618
619
/**
620
 * is.within
621
 * Test if `value` is within `start` and `finish`.
622
 *
623
 * @param {Number} value value to test
624
 * @param {Number} start lower bound
625
 * @param {Number} finish upper bound
626
 * @return {Boolean} true if 'value' is is within 'start' and 'finish'
627
 * @api public
628
 */
629
is.within = function (value, start, finish) {
630
  if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) {
631
    throw new TypeError('NaN is not a valid value');
632
  } else if (!is.number(value) || !is.number(start) || !is.number(finish)) {
633
    throw new TypeError('all arguments must be numbers');
634
  }
635
  var isAnyInfinite = is.infinite(value) || is.infinite(start) || is.infinite(finish);
636
  return isAnyInfinite || (value >= start && value <= finish);
637
};
638
639
/**
640
 * Test object.
641
 */
642
643
/**
644
 * is.object
645
 * Test if `value` is an object.
646
 *
647
 * @param {Mixed} value value to test
648
 * @return {Boolean} true if `value` is an object, false otherwise
649
 * @api public
650
 */
651
652
is.object = function (value) {
653
  return value && '[object Object]' === toString.call(value);
654
};
655
656
/**
657
 * is.hash
658
 * Test if `value` is a hash - a plain object literal.
659
 *
660
 * @param {Mixed} value value to test
661
 * @return {Boolean} true if `value` is a hash, false otherwise
662
 * @api public
663
 */
664
665
is.hash = function (value) {
666
  return is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval;
667
};
668
669
/**
670
 * Test regexp.
671
 */
672
673
/**
674
 * is.regexp
675
 * Test if `value` is a regular expression.
676
 *
677
 * @param {Mixed} value value to test
678
 * @return {Boolean} true if `value` is a regexp, false otherwise
679
 * @api public
680
 */
681
682
is.regexp = function (value) {
683
  return '[object RegExp]' === toString.call(value);
684
};
685
686
/**
687
 * Test string.
688
 */
689
690
/**
691
 * is.string
692
 * Test if `value` is a string.
693
 *
694
 * @param {Mixed} value value to test
695
 * @return {Boolean} true if 'value' is a string, false otherwise
696
 * @api public
697
 */
698
699
is.string = function (value) {
700
  return '[object String]' === toString.call(value);
701
};