2
2
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
3
3
4
4
import { Primitive } from '@sentry/types' ;
5
+
6
+ // eslint-disable-next-line @typescript-eslint/unbound-method
7
+ const objectToString = Object . prototype . toString ;
8
+
5
9
/**
6
10
* Checks whether given value's type is one of a few Error or Error-like
7
11
* {@link isError}.
@@ -10,18 +14,20 @@ import { Primitive } from '@sentry/types';
10
14
* @returns A boolean representing the result.
11
15
*/
12
16
export function isError ( wat : unknown ) : boolean {
13
- switch ( Object . prototype . toString . call ( wat ) ) {
17
+ switch ( objectToString . call ( wat ) ) {
14
18
case '[object Error]' :
15
- return true ;
16
19
case '[object Exception]' :
17
- return true ;
18
20
case '[object DOMException]' :
19
21
return true ;
20
22
default :
21
23
return isInstanceOf ( wat , Error ) ;
22
24
}
23
25
}
24
26
27
+ function isBuiltin ( wat : unknown , ty : string ) : boolean {
28
+ return objectToString . call ( wat ) === `[object ${ ty } ]` ;
29
+ }
30
+
25
31
/**
26
32
* Checks whether given value's type is ErrorEvent
27
33
* {@link isErrorEvent}.
@@ -30,7 +36,7 @@ export function isError(wat: unknown): boolean {
30
36
* @returns A boolean representing the result.
31
37
*/
32
38
export function isErrorEvent ( wat : unknown ) : boolean {
33
- return Object . prototype . toString . call ( wat ) === '[object ErrorEvent]' ;
39
+ return isBuiltin ( wat , ' ErrorEvent' ) ;
34
40
}
35
41
36
42
/**
@@ -41,7 +47,7 @@ export function isErrorEvent(wat: unknown): boolean {
41
47
* @returns A boolean representing the result.
42
48
*/
43
49
export function isDOMError ( wat : unknown ) : boolean {
44
- return Object . prototype . toString . call ( wat ) === '[object DOMError]' ;
50
+ return isBuiltin ( wat , ' DOMError' ) ;
45
51
}
46
52
47
53
/**
@@ -52,7 +58,7 @@ export function isDOMError(wat: unknown): boolean {
52
58
* @returns A boolean representing the result.
53
59
*/
54
60
export function isDOMException ( wat : unknown ) : boolean {
55
- return Object . prototype . toString . call ( wat ) === '[object DOMException]' ;
61
+ return isBuiltin ( wat , ' DOMException' ) ;
56
62
}
57
63
58
64
/**
@@ -63,7 +69,7 @@ export function isDOMException(wat: unknown): boolean {
63
69
* @returns A boolean representing the result.
64
70
*/
65
71
export function isString ( wat : unknown ) : boolean {
66
- return Object . prototype . toString . call ( wat ) === '[object String]' ;
72
+ return isBuiltin ( wat , ' String' ) ;
67
73
}
68
74
69
75
/**
@@ -85,7 +91,7 @@ export function isPrimitive(wat: unknown): wat is Primitive {
85
91
* @returns A boolean representing the result.
86
92
*/
87
93
export function isPlainObject ( wat : unknown ) : wat is Record < string , unknown > {
88
- return Object . prototype . toString . call ( wat ) === '[object Object]' ;
94
+ return isBuiltin ( wat , ' Object' ) ;
89
95
}
90
96
91
97
/**
@@ -118,7 +124,7 @@ export function isElement(wat: unknown): wat is Element {
118
124
* @returns A boolean representing the result.
119
125
*/
120
126
export function isRegExp ( wat : unknown ) : wat is RegExp {
121
- return Object . prototype . toString . call ( wat ) === '[object RegExp]' ;
127
+ return isBuiltin ( wat , ' RegExp' ) ;
122
128
}
123
129
124
130
/**
0 commit comments