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