main
Last change
on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Line | |
---|
1 | // Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of
|
---|
2 | export function miniKindOf(val: any): string {
|
---|
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 | }
|
---|
16 |
|
---|
17 | if (Array.isArray(val)) return 'array'
|
---|
18 | if (isDate(val)) return 'date'
|
---|
19 | if (isError(val)) return 'error'
|
---|
20 |
|
---|
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 | }
|
---|
31 |
|
---|
32 | // other
|
---|
33 | return Object.prototype.toString
|
---|
34 | .call(val)
|
---|
35 | .slice(8, -1)
|
---|
36 | .toLowerCase()
|
---|
37 | .replace(/\s/g, '')
|
---|
38 | }
|
---|
39 |
|
---|
40 | function ctorName(val: any): string | null {
|
---|
41 | return typeof val.constructor === 'function' ? val.constructor.name : null
|
---|
42 | }
|
---|
43 |
|
---|
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 | }
|
---|
52 |
|
---|
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 | }
|
---|
61 |
|
---|
62 | export function kindOf(val: any) {
|
---|
63 | let typeOfVal: string = typeof val
|
---|
64 |
|
---|
65 | if (process.env.NODE_ENV !== 'production') {
|
---|
66 | typeOfVal = miniKindOf(val)
|
---|
67 | }
|
---|
68 |
|
---|
69 | return typeOfVal
|
---|
70 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.