Skip to content

Commit cecf124

Browse files
authored
Merge pull request reduxjs#4090 from embeddedt/fix/es5_compat
Move miniKindOf out of if scope to fix ES5 compatibility issue
2 parents 5e33fd1 + 674ef72 commit cecf124

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

src/utils/kindOf.js

+56-56
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
1-
export function kindOf(val) {
2-
let typeOfVal = typeof val
1+
// Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of
2+
function miniKindOf(val) {
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) {
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+
default:
16+
break
17+
}
918

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-
default:
20-
break
21-
}
19+
if (Array.isArray(val)) return 'array'
20+
if (isDate(val)) return 'date'
21+
if (isError(val)) return 'error'
2222

23-
if (Array.isArray(val)) return 'array'
24-
if (isDate(val)) return 'date'
25-
if (isError(val)) return 'error'
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+
}
2635

27-
const constructorName = ctorName(val)
28-
switch (constructorName) {
29-
case 'Symbol':
30-
case 'Promise':
31-
case 'WeakMap':
32-
case 'WeakSet':
33-
case 'Map':
34-
case 'Set':
35-
return constructorName
36-
default:
37-
break
38-
}
36+
// other
37+
return type.slice(8, -1).toLowerCase().replace(/\s/g, '')
38+
}
3939

40-
// other
41-
return type.slice(8, -1).toLowerCase().replace(/\s/g, '')
42-
}
40+
function ctorName(val) {
41+
return typeof val.constructor === 'function' ? val.constructor.name : null
42+
}
4343

44-
function ctorName(val) {
45-
return typeof val.constructor === 'function' ? val.constructor.name : null
46-
}
44+
function isError(val) {
45+
return (
46+
val instanceof Error ||
47+
(typeof val.message === 'string' &&
48+
val.constructor &&
49+
typeof val.constructor.stackTraceLimit === 'number')
50+
)
51+
}
4752

48-
function isError(val) {
49-
return (
50-
val instanceof Error ||
51-
(typeof val.message === 'string' &&
52-
val.constructor &&
53-
typeof val.constructor.stackTraceLimit === 'number')
54-
)
55-
}
53+
function 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+
}
5661

57-
function isDate(val) {
58-
if (val instanceof Date) return true
59-
return (
60-
typeof val.toDateString === 'function' &&
61-
typeof val.getDate === 'function' &&
62-
typeof val.setDate === 'function'
63-
)
64-
}
62+
export function kindOf(val) {
63+
let typeOfVal = typeof val
6564

65+
if (process.env.NODE_ENV !== 'production') {
6666
typeOfVal = miniKindOf(val)
6767
}
6868

0 commit comments

Comments
 (0)