-
Notifications
You must be signed in to change notification settings - Fork 0
/
_type.js
50 lines (46 loc) · 1.12 KB
/
_type.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
import TYPES from './enum/types'
import OBJECTS from './enum/objects'
/**
* 检测数据类型,返回检测数据类型的字符串
* ========================================================================
* @method _type
* @param {*} val - 要检测的任意值
* @returns {String}
*/
const _type = (val) => {
const type = Object.prototype.toString.apply(val)
const _typeof = typeof val
let name
// HTMLElement
if (val?.tagName && val.nodeType === 1) {
name = TYPES.ELEMENT
} else {
/* ===== 原始值类型(Primitive data types) ===== */
switch (_typeof) {
case 'bigint':
name = TYPES.BIG_INT
break
case 'string':
name = TYPES.STRING
break
case 'number':
name = TYPES.NUMBER
break
case 'boolean':
name = TYPES.BOOLEAN
break
case 'undefined':
name = TYPES.UNDEFINED
break
case 'symbol':
name = TYPES.SYMBOL
break
// 对象(引用)类型的数据
default:
name = OBJECTS[type]
break
}
}
return name || type
}
export default _type