-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgetDataType.js
executable file
·35 lines (35 loc) · 1.08 KB
/
getDataType.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
/**
* @description 获取数据的具体类型
* @param {any} o - 要判断的数据
* @returns {string} - 返回该数据的具体类型
*/
export const getDataType = (o) => {
// 映射数据类型
const map2DataType = {
'[object String]': 'String',
'[object Number]': 'Number',
'[object Undefined]': 'Undefined',
'[object Boolean]': 'Boolean',
'[object Array]': 'Array',
'[object Function]': 'Function',
'[object Object]': 'Object',
'[object Symbol]': 'Symbol',
'[object Set]': 'Set',
'[object Map]': 'Map',
'[object WeakSet]': 'WeakSet',
'[object WeakMap]': 'WeakMap',
'[object Null]': 'Null',
'[object Promise]': 'Promise',
'[object NodeList]': 'NodeList',
'[object Date]': 'Date',
'[object FormData]': 'FormData'
};
o = Object.prototype.toString.call(o);
if (map2DataType[o]) {
return map2DataType[o];
} else {
// return o.replace(/^\[object\s(.*)\]$/, '$1');
// FIXED: slice instated of regExp to improve performance,look at [jsperf](https://jsperf.com/slice-replace-reg)
return o.slice(8, -1);
}
};