-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (119 loc) · 2.95 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
const toString = Object.prototype.toString;
/**
* get `toString.call()` result
* @param value {*} The value to call `toString.call()`
* @returns {string|string} Return `toString.call()` result
*/
function getTag(value) {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]';
}
return toString.call(value);
}
/**
* get [[Class]] name
*
* getClass('')
* // => 'String'
*
* getClass(new Date())
* // => 'Date'
*
* @param value {*} The value to get class name
* @return {string} Return [[Class]] name
*/
export function getClass(value) {
return /^\[object (.*)]$/.exec(getTag(value))[1];
}
/**
* generator isType function
* @param type {string} The type name
* @returns {function(*): boolean}
*/
function isType(type) {
const cls = '[object ' + type + ']';
/**
* Check if value is a type
*/
return function (value) {
return getTag(value) === cls;
}
}
/**
* Check if value is a Date object
* @param value {*} The value to check
* @return {boolean} Return true if value is a date
*/
export const isDate = isType('Date');
/**
* Check if value is a regexp
* @param value {*} The value to check
* @return {boolean} Return true if value is a regexp
*/
export const isRegExp = isType('RegExp');
/**
* Check if value is a bool value
* @param value {*} The value to check
* @return {boolean} Return true if value is a bool
*/
export const isBoolean = function (value) {
return value === true || value === false || isType('Boolean');
}
/**
* Check if value is a Number
* @param value {*} The value to check
* @return {boolean} Return true if value is a number
*/
export const isNumber = function (value) {
return typeof value === 'number' || isType('Number');
}
/**
* Check if value is a string
* @param value {*} The value to check
* @return {boolean} Return true if value is a string
*/
export const isString = function (value) {
return typeof value === 'string' || isType('String');
}
/**
* Check if value is a Function object
* @param value {*} The value to check
* @return {boolean} Return true if value is a function
*/
export const isFunction = function (value) {
return typeof value === 'function' || isType('Function');
}
/**
* Check if value is an Array object
* @param value {*} The value to check
* @return {boolean} Return true if value is a array
*/
export const isArray = Array.isArray || isType('Array');
/**
* Check if value is NaN
* @param value {*} The value of check
* @return {boolean} Return true if value is NaN
*/
export const isNaN = Number.isNaN || function(value) {
return typeof value === 'number' || isNaN(value);
}
/**
* Check if value is not null or undefined
* @param value {*} The value to check
* @return {boolean} Return true if value is not null or undefined
*/
export function isDefined(value) {
return value != null;
}
export default {
getClass,
isDate,
isRegExp,
isBoolean,
isNumber,
isString,
isFunction,
isArray,
isNaN,
isDefined
}