-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
915 lines (799 loc) · 19.5 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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
"use strict";
/**!
* fy.js
* 一个通用的 JavaScript 类型测试库
*
* @copyright 2016-2023 By zoeblow
* @license MIT
*/
var objProto = Object.prototype;
var owns = objProto.hasOwnProperty;
var toStr = objProto.toString;
var symbolValueOf;
if (typeof Symbol === "function") {
symbolValueOf = Symbol.prototype.valueOf;
}
var bigIntValueOf;
if (typeof BigInt === "function") {
bigIntValueOf = BigInt.prototype.valueOf;
}
var isActualNaN = function (value) {
return value !== value;
};
var NON_HOST_TYPES = {
boolean: 1,
number: 1,
string: 1,
undefined: 1,
};
var base64Regex =
/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/;
var hexRegex = /^[A-Fa-f0-9]+$/;
/**
* 声明 `fy`
*/
var fy = {};
/**
* Test general.
*/
/**
* isType
* Test if `value` is a type of `typeof`.
*
* @param {*} value value to test
* @param {String} type type
* @return {Boolean} true if `value` is a type of `type`, false otherwise
* @api public
*/
fy.isA = fy.isType = function (value, type) {
return typeof value === type;
};
/**
* fy.isDefined
* Test if `value` is defined.
* 测试是否定义了`value`。
*
* @param {*} value value to test
* @return {Boolean} true if 'value' is defined, false otherwise
* @api public
*/
fy.isDefined = function (value) {
return typeof value !== "undefined";
};
/**
* fy.isEmpty
* Test if `value` is empty.
* 测试`value`是否为空。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is empty, false otherwise
* @api public
*/
fy.isEmpty = function (value) {
var type = toStr.call(value);
var key;
if (
type === "[object Array]" ||
type === "[object Arguments]" ||
type === "[object String]"
) {
return value.length === 0;
}
if (type === "[object Object]") {
for (key in value) {
if (owns.call(value, key)) {
return false;
}
}
return true;
}
return !value;
};
/**
* fy.isEqual
* Test if `value` is equal to `other`.
* 测试`value`是否等于`other`。
*
* @param {*} value value to test
* @param {*} other value to compare with
* @return {Boolean} true if `value` is equal to `other`, false otherwise
*/
fy.isEqual = function equal(value, other) {
if (value === other) {
return true;
}
var type = toStr.call(value);
var key;
if (type !== toStr.call(other)) {
return false;
}
if (type === "[object Object]") {
for (key in value) {
if (!fy.isEqual(value[key], other[key]) || !(key in other)) {
return false;
}
}
for (key in other) {
if (!fy.isEqual(value[key], other[key]) || !(key in value)) {
return false;
}
}
return true;
}
if (type === "[object Array]") {
key = value.length;
if (key !== other.length) {
return false;
}
while (key--) {
if (!fy.isEqual(value[key], other[key])) {
return false;
}
}
return true;
}
if (type === "[object Function]") {
return value.prototype === other.prototype;
}
if (type === "[object Date]") {
return value.getTime() === other.getTime();
}
return false;
};
/**
* fy.isHosted
* Test if `value` is hosted by `host`.
* 测试`value`是否由`Host`托管。
*
* @param {*} value to test
* @param {*} host host to test with
* @return {Boolean} true if `value` is hosted by `host`, false otherwise
* @api public
*/
fy.isHosted = function (value, host) {
var type = typeof host[value];
return type === "object" ? !!host[value] : !NON_HOST_TYPES[type];
};
/**
* fy.isInstance
* Test if `value` is an instance of `constructor`.
* 测试`value`是否为`constructor`的实例。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an instance of `constructor`
* @api public
*/
fy.isInstance = fy["instanceof"] = function (value, constructor) {
return value instanceof constructor;
};
/**
* fy.isNull / fy.null
* Test if `value` is null.
* 测试`value`是否 `null`。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is null, false otherwise
* @api public
*/
fy.isNull = fy["null"] = function (value) {
return value === null;
};
/**
* fy.isUndefined / fy.undefined
* Test if `value` is undefined.
* 测试`value`是否 `undefined`。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is undefined, false otherwise
* @api public
*/
fy.isUndefined = fy["undefined"] = function (value) {
return typeof value === "undefined";
};
/**
* Test arguments.
*/
/**
* fy.isArgs
* Test if `value` is an arguments object.
* 测试`value`是否为参数对象。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an arguments object, false otherwise
* @api public
*/
fy.isArgs = fy.isArguments = function (value) {
var isStandardArguments = toStr.call(value) === "[object Arguments]";
var isOldArguments =
!fy.isArray(value) &&
fy.isArrayLike(value) &&
fy.isObject(value) &&
fy.isFn(value.callee);
return isStandardArguments || isOldArguments;
};
/**
* Test array.
*/
/**
* fy.isArray
* Test if 'value' is an array.
* 测试‘Value’是否为数组。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an array, false otherwise
* @api public
*/
fy.isArray =
Array.isArray ||
function (value) {
return toStr.call(value) === "[object Array]";
};
/**
* fy.isArguments.isEmpty
* Test if `value` is an empty arguments object.
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an empty arguments object, false otherwise
* @api public
*/
fy.isArgs.isEmpty = function (value) {
return fy.isArgs(value) && value.length === 0;
};
/**
* fy.isArray.isEmpty
* Test if `value` is an empty array.
* 测试`value`是否为空数组。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an empty array, false otherwise
* @api public
*/
fy.isArray.isEmpty = function (value) {
return fy.isArray(value) && value.length === 0;
};
/**
* fy.isArrayLike
* Test if `value` is an arraylike object.
* 测试`value`是否是类数组的对象。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an arguments object, false otherwise
* @api public
*/
fy.isArrayLike = function (value) {
return (
!!value &&
!fy.isBool(value) &&
owns.call(value, "length") &&
isFinite(value.length) &&
fy.isNumber(value.length) &&
value.length >= 0
);
};
/**
* Test boolean.
*/
/**
* fy.isBool
* Test if `value` is a boolean.
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a boolean, false otherwise
* @api public
*/
fy.isBool = fy["boolean"] = function (value) {
return toStr.call(value) === "[object Boolean]";
};
/**
* fy.false
* Test if `value` is false.
*
* @param {*} value value to test
* @return {Boolean} true if `value` is false, false otherwise
* @api public
*/
fy.isFalse = fy["false"] = function (value) {
return fy.isBool(value) && Boolean(Number(value)) === false;
};
/**
* fy.true
* Test if `value` is true.
*
* @param {*} value value to test
* @return {Boolean} true if `value` is true, false otherwise
* @api public
*/
fy.isTrue = fy["true"] = function (value) {
return fy.isBool(value) && Boolean(Number(value)) === true;
};
/**
* Test date.
*/
/**
* fy.isDate
* Test if `value` is a date.
* 测试`value`是否为日期。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a date, false otherwise
* @api public
*/
fy.isDate = function (value) {
return toStr.call(value) === "[object Date]";
};
/**
* fy.isDate.valid
* Test if `value` is a valid date.
*
* @param {*} value value to test
* @returns {Boolean} true if `value` is a valid date, false otherwise
*/
fy.isDate.valid = function (value) {
return fy.isDate(value) && !isNaN(Number(value));
};
/**
* Test element.
*/
/**
* fy.isElement
* Test if `value` is an html element.
* 测试`value`是否为html元素。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an HTML Element, false otherwise
* @api public
*/
fy.isElement = function (value) {
return (
value !== undefined &&
typeof HTMLElement !== "undefined" &&
value instanceof HTMLElement &&
value.nodeType === 1
);
};
/**
* Test error.
*/
/**
* fy.isError
* Test if `value` is an error object.
* 测试`value`是否为`error`对象。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an error object, false otherwise
* @api public
*/
fy.isError = function (value) {
return toStr.call(value) === "[object Error]";
};
/**
* Test function.
*/
/**
* fy.isFn / fy.function (deprecated)
* Test if `value` is a function.
* 测试`value`是否为函数。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a function, false otherwise
* @api public
*/
fy.isFn = fy["function"] = function (value) {
var isAlert = typeof window !== "undefined" && value === window.isAlert;
if (isAlert) {
return true;
}
var str = toStr.call(value);
return (
str === "[object Function]" ||
str === "[object GeneratorFunction]" ||
str === "[object AsyncFunction]"
);
};
/**
* Test number.
*/
/**
* fy.isNumber
* Test if `value` is a number.
* 测试`value`是否为数字。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a number, false otherwise
* @api public
*/
fy.isNumber = function (value) {
return toStr.call(value) === "[object Number]";
};
/**
* fy.isInfinite
* Test if `value` is positive or negative infinity.
* 测试`value`是正无穷大还是负无穷大。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is positive or negative Infinity, false otherwise
* @api public
*/
fy.isInfinite = function (value) {
return value === Infinity || value === -Infinity;
};
/**
* fy.isDecimal
* Test if `value` is a decimal number.
* 测试`value`是否为十进制数。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a decimal number, false otherwise
* @api public
*/
fy.isDecimal = function (value) {
return (
fy.isNumber(value) &&
!isActualNaN(value) &&
!fy.isInfinite(value) &&
value % 1 !== 0
);
};
/**
* fy.isDivisibleBy
* Test if `value` is divisible by `n`.
* 测试`value`是否可以被`N`整除。
*
* @param {Number} value value to test
* @param {Number} n dividend
* @return {Boolean} true if `value` is divisible by `n`, false otherwise
* @api public
*/
fy.isDivisibleBy = function (value, n) {
var isDividendInfinite = fy.isInfinite(value);
var isDivisorInfinite = fy.isInfinite(n);
var isNonZeroNumber =
fy.isNumber(value) &&
!isActualNaN(value) &&
fy.isNumber(n) &&
!isActualNaN(n) &&
n !== 0;
return (
isDividendInfinite ||
isDivisorInfinite ||
(isNonZeroNumber && value % n === 0)
);
};
/**
* fy.isInteger
* Test if `value` is an integer.
* 测试`value`是否为整数。
*
* @param value to test
* @return {Boolean} true if `value` is an integer, false otherwise
* @api public
*/
fy.isInteger = fy["int"] = function (value) {
return fy.isNumber(value) && !isActualNaN(value) && value % 1 === 0;
};
/**
* fy.isMaximum
* Test if `value` is greater than 'others' values.
* 测试`value`是否是`others`值中最大的。
*
* @param {Number} value value to test
* @param {Array} others values to compare with
* @return {Boolean} true if `value` is greater than `others` values
* @api public
*/
fy.isMax = fy.isMaximum = function (value, others) {
if (isActualNaN(value)) {
throw new TypeError("NaN is not a valid value");
} else if (!fy.isArrayLike(others)) {
throw new TypeError("second argument must be array-like");
}
var len = others.length;
while (--len >= 0) {
if (value < others[len]) {
return false;
}
}
return true;
};
/**
* fy.isMinimum
* Test if `value` is less than `others` values.
* 测试`value`是否小于`others`值。
*
* @param {Number} value value to test
* @param {Array} others values to compare with
* @return {Boolean} true if `value` is less than `others` values
* @api public
*/
fy.isMin = fy.isMinimum = function (value, others) {
if (isActualNaN(value)) {
throw new TypeError("NaN is not a valid value");
} else if (!fy.isArrayLike(others)) {
throw new TypeError("second argument must be array-like");
}
var len = others.length;
while (--len >= 0) {
if (value > others[len]) {
return false;
}
}
return true;
};
/**
* fy.isNaN
* Test if `value` is not a number.
* 测试`value`是否不是NaN。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is not a number, false otherwise
* @api public
*/
fy.isNaN = function (value) {
return !fy.isNumber(value) || value !== value;
};
/**
* fy.isEven
* Test if `value` is an even number.
* 测试`value`是否为偶数。
*
* @param {Number} value value to test
* @return {Boolean} true if `value` is an even number, false otherwise
* @api public
*/
fy.isEven = function (value) {
return (
fy.isInfinite(value) ||
(fy.isNumber(value) && value === value && value % 2 === 0)
);
};
/**
* fy.isOdd
* Test if `value` is an odd number.
* 测试`value`是否为奇数。
*
* @param {Number} value value to test
* @return {Boolean} true if `value` is an odd number, false otherwise
* @api public
*/
fy.isOdd = function (value) {
return (
fy.isInfinite(value) ||
(fy.isNumber(value) && value === value && value % 2 !== 0)
);
};
/**
* fy.isGe
* Test if `value` is greater than or equal to `other`.
* 测试`value`是否大于或等于`other`。
*
* @param {Number} value value to test
* @param {Number} other value to compare with
* @return {Boolean}
* @api public
*/
fy.isGe = function (value, other) {
if (isActualNaN(value) || isActualNaN(other)) {
throw new TypeError("NaN is not a valid value");
}
return !fy.isInfinite(value) && !fy.isInfinite(other) && value >= other;
};
/**
* fy.isGt
* Test if `value` is greater than `other`.
* 测试`value`是否大于`other`。
*
* @param {Number} value value to test
* @param {Number} other value to compare with
* @return {Boolean}
* @api public
*/
fy.isGt = function (value, other) {
if (isActualNaN(value) || isActualNaN(other)) {
throw new TypeError("NaN is not a valid value");
}
return !fy.isInfinite(value) && !fy.isInfinite(other) && value > other;
};
/**
* fy.isLe
* Test if `value` is less than or equal to `other`.
* 测试`value`是否小于等于`other`。
*
* @param {Number} value value to test
* @param {Number} other value to compare with
* @return {Boolean} if 'value' is less than or equal to 'other'
* @api public
*/
fy.isLe = function (value, other) {
if (isActualNaN(value) || isActualNaN(other)) {
throw new TypeError("NaN is not a valid value");
}
return !fy.isInfinite(value) && !fy.isInfinite(other) && value <= other;
};
/**
* fy.isLt
* Test if `value` is less than `other`.
* 测试`value`是否小于`other`。
* @param {Number} value value to test
* @param {Number} other value to compare with
* @return {Boolean} if `value` is less than `other`
* @api public
*/
fy.isLt = function (value, other) {
if (isActualNaN(value) || isActualNaN(other)) {
throw new TypeError("NaN is not a valid value");
}
return !fy.isInfinite(value) && !fy.isInfinite(other) && value < other;
};
/**
* fy.isWithin
* Test if `value` is within `start` and `finish`.
* 测试`value`是否在`start`和`finish`之间。
*
* @param {Number} value value to test
* @param {Number} start lower bound
* @param {Number} finish upper bound
* @return {Boolean} true if 'value' is is within 'start' and 'finish'
* @api public
*/
fy.isWithin = function (value, start, finish) {
if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) {
throw new TypeError("NaN is not a valid value");
} else if (
!fy.isNumber(value) ||
!fy.isNumber(start) ||
!fy.isNumber(finish)
) {
throw new TypeError("all arguments must be numbers");
}
var isAnyInfinite =
fy.isInfinite(value) || fy.isInfinite(start) || fy.isInfinite(finish);
return isAnyInfinite || (value >= start && value <= finish);
};
/**
* Test object.
*/
/**
* fy.isObject
* Test if `value` is an object.
* 测试`value`是否为Object。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is an object, false otherwise
* @api public
*/
fy.isObject = function (value) {
return toStr.call(value) === "[object Object]";
};
/**
* fy.primitive
* Test if `value` is a primitive.
* 测试`value`是否为原始值。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a primitive, false otherwise
* @api public
*/
fy.isPrimitive = function isPrimitive(value) {
if (!value) {
return true;
}
if (
typeof value === "object" ||
fy.isObject(value) ||
fy.isFn(value) ||
fy.isArray(value)
) {
return false;
}
return true;
};
/**
* fy.isHash
* Test if `value` is a hash - a plain object literal.
* 测试`value`是否为 Hash --纯对象文字。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a hash, false otherwise
* @api public
*/
fy.isHash = function (value) {
return (
fy.isObject(value) &&
value.constructor === Object &&
!value.nodeType &&
!value.setInterval
);
};
/**
* Test regexp.
*/
/**
* fy.isRegexp
* Test if `value` is a regular expression.
* 测试`value`是否为正则表达式。
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a regexp, false otherwise
* @api public
*/
fy.isRegexp = function (value) {
return toStr.call(value) === "[object RegExp]";
};
/**
* Test string.
*/
/**
* fy.isString
* Test if `value` is a string.
* 测试`value`是否为字符串。
*
* @param {*} value value to test
* @return {Boolean} true if 'value' is a string, false otherwise
* @api public
*/
fy.isString = function (value) {
return toStr.call(value) === "[object String]";
};
/**
* Test base64 string.
*/
/**
* fy.isBase64
* Test if `value` is a valid base64 encoded string.
* 测试`value`是否为有效的Base64编码字符串。
*
* @param {*} value value to test
* @return {Boolean} true if 'value' is a base64 encoded string, false otherwise
* @api public
*/
fy.isBase64 = function (value) {
return fy.isString(value) && (!value.length || base64Regex.test(value));
};
/**
* Test base64 string.
*/
/**
* fy.isHex
* Test if `value` is a valid hex encoded string.
* 测试`value`是否为有效的十六进制编码字符串。
*
* @param {*} value value to test
* @return {Boolean} true if 'value' is a hex encoded string, false otherwise
* @api public
*/
fy.isHex = function (value) {
return fy.isString(value) && (!value.length || hexRegex.test(value));
};
/**
* fy.isSymbol
* Test if `value` is an ES6 Symbol
* 测试`value`是否为ES6 Symbol
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a Symbol, false otherise
* @api public
*/
fy.isSymbol = function (value) {
return (
typeof Symbol === "function" &&
toStr.call(value) === "[object Symbol]" &&
typeof symbolValueOf.call(value) === "symbol"
);
};
/**
* fy.isBigint
* Test if `value` is an ES-proposed BigInt
* 测试`value`是否为 ES 建议的 BigInt
*
* @param {*} value value to test
* @return {Boolean} true if `value` is a BigInt, false otherise
* @api public
*/
fy.isBigint = function (value) {
return (
typeof BigInt === "function" &&
toStr.call(value) === "[object BigInt]" &&
typeof bigIntValueOf.call(value) === "bigint"
);
};
module.exports = fy;