UNPKG

1.68 kBJavaScriptView Raw
1var toString = Object.prototype.toString;
2
3/**
4 * Get the native `typeof` a value.
5 *
6 * @param {*} `val`
7 * @return {*} Native javascript type
8 */
9
10module.exports = function kindOf(val) {
11 // primitivies
12 if (typeof val === 'undefined') {
13 return 'undefined';
14 }
15 if (val === null) {
16 return 'null';
17 }
18 if (val === true || val === false || val instanceof Boolean) {
19 return 'boolean';
20 }
21 if (typeof val === 'string' || val instanceof String) {
22 return 'string'
23 }
24 if (typeof val === 'number' || val instanceof Number) {
25 return 'number'
26 }
27
28 // functions
29 if (typeof val === 'function' || val instanceof Function) {
30 return 'function'
31 }
32
33 // array
34 if (typeof Array.isArray !== 'undefined' && Array.isArray(val)) {
35 return 'array';
36 }
37
38 // check for instances of RegExp and Date before calling `toString`
39 if (val instanceof RegExp) {
40 return 'regexp';
41 }
42 if (val instanceof Date) {
43 return 'date';
44 }
45
46 // other objects
47 var type = toString.call(val);
48
49 if (type === '[object RegExp]') {
50 return 'regexp';
51 }
52 if (type === '[object Date]') {
53 return 'date';
54 }
55 if (type === '[object Arguments]') {
56 return 'arguments';
57 }
58
59 // buffer
60 if (typeof Buffer !== 'undefined' && Buffer.isBuffer(val)) {
61 return 'buffer';
62 }
63
64 // es6: Map, WeakMap, Set, WeakSet
65 if (type === '[object Set]') {
66 return 'set';
67 }
68 if (type === '[object WeakSet]') {
69 return 'weakset';
70 }
71 if (type === '[object Map]') {
72 return 'map';
73 }
74 if (type === '[object WeakMap]') {
75 return 'weakmap';
76 }
77 if (type === '[object Symbol]') {
78 return 'symbol';
79 }
80
81 // must be a plain object
82 return 'object';
83};