Skip to content

Commit 023d9c4

Browse files
committed
Backport kindOf fix from reduxjs#4090
Former-commit-id: 4627a1a
1 parent 3d04f0b commit 023d9c4

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

src/utils/kindOf.ts

+52-52
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
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+
export function miniKindOf(val: any): string {
3+
if (val === void 0) return 'undefined'
4+
if (val === null) return 'null'
35

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+
}
916

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'
2020

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+
}
2431

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+
}
3535

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+
}
3939

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+
}
4348

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+
}
5257

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) {
59+
let typeOfVal: string = typeof val
6160

61+
if (process.env.NODE_ENV !== 'production') {
6262
typeOfVal = miniKindOf(val)
6363
}
6464

0 commit comments

Comments
 (0)