-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisType.js
32 lines (28 loc) · 961 Bytes
/
isType.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
const isType = function (data, type) {
return new Promise((resolve, reject) => {
if (arguments.length === 2) {
// 数组,这里使用isArray,有的浏览器不支持Array.isArray,则用prototype.toString替代
let t = type.toLowerCase()
if (t === 'array') {
if (!Array.isArray) {
Array.isArray = function (arg) {
return Object.prototype.toString.call(arg) === '[object Array]'
}
}
resolve(Array.isArray(data))
}
if (t === 'string') {
resolve(Object.prototype.toString.call(data) === '[object String]')
}
if (t === 'object') {
resolve(Object.prototype.toString.call(data) === '[object Object]')
}
if (t === 'number') {
resolve(Object.prototype.toString.call(data) === '[object Number]')
}
} else {
reject(new Error('miss arguments; arguments: (data, type)'))
}
})
}
module.exports = isType