UNPKG

1.48 kBJavaScriptView Raw
1// Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of
2function miniKindOf(val) {
3 if (val === void 0) return 'undefined'
4 if (val === null) return 'null'
5
6 const type = typeof val
7 switch (type) {
8 case 'boolean':
9 case 'string':
10 case 'number':
11 case 'symbol':
12 case 'function': {
13 return type
14 }
15 default:
16 break
17 }
18
19 if (Array.isArray(val)) return 'array'
20 if (isDate(val)) return 'date'
21 if (isError(val)) return 'error'
22
23 const constructorName = ctorName(val)
24 switch (constructorName) {
25 case 'Symbol':
26 case 'Promise':
27 case 'WeakMap':
28 case 'WeakSet':
29 case 'Map':
30 case 'Set':
31 return constructorName
32 default:
33 break
34 }
35
36 // other
37 return type.slice(8, -1).toLowerCase().replace(/\s/g, '')
38}
39
40function ctorName(val) {
41 return typeof val.constructor === 'function' ? val.constructor.name : null
42}
43
44function isError(val) {
45 return (
46 val instanceof Error ||
47 (typeof val.message === 'string' &&
48 val.constructor &&
49 typeof val.constructor.stackTraceLimit === 'number')
50 )
51}
52
53function isDate(val) {
54 if (val instanceof Date) return true
55 return (
56 typeof val.toDateString === 'function' &&
57 typeof val.getDate === 'function' &&
58 typeof val.setDate === 'function'
59 )
60}
61
62export function kindOf(val) {
63 let typeOfVal = typeof val
64
65 if (process.env.NODE_ENV !== 'production') {
66 typeOfVal = miniKindOf(val)
67 }
68
69 return typeOfVal
70}