22/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
33
44import { Primitive } from '@sentry/types' ;
5+
6+ // eslint-disable-next-line @typescript-eslint/unbound-method
7+ const objectToString = Object . prototype . toString ;
8+
59/**
610 * Checks whether given value's type is one of a few Error or Error-like
711 * {@link isError}.
@@ -10,18 +14,20 @@ import { Primitive } from '@sentry/types';
1014 * @returns A boolean representing the result.
1115 */
1216export function isError ( wat : unknown ) : boolean {
13- switch ( Object . prototype . toString . call ( wat ) ) {
17+ switch ( objectToString . call ( wat ) ) {
1418 case '[object Error]' :
15- return true ;
1619 case '[object Exception]' :
17- return true ;
1820 case '[object DOMException]' :
1921 return true ;
2022 default :
2123 return isInstanceOf ( wat , Error ) ;
2224 }
2325}
2426
27+ function isBuiltin ( wat : unknown , ty : string ) : boolean {
28+ return objectToString . call ( wat ) === `[object ${ ty } ]` ;
29+ }
30+
2531/**
2632 * Checks whether given value's type is ErrorEvent
2733 * {@link isErrorEvent}.
@@ -30,7 +36,7 @@ export function isError(wat: unknown): boolean {
3036 * @returns A boolean representing the result.
3137 */
3238export function isErrorEvent ( wat : unknown ) : boolean {
33- return Object . prototype . toString . call ( wat ) === '[object ErrorEvent]' ;
39+ return isBuiltin ( wat , ' ErrorEvent' ) ;
3440}
3541
3642/**
@@ -41,7 +47,7 @@ export function isErrorEvent(wat: unknown): boolean {
4147 * @returns A boolean representing the result.
4248 */
4349export function isDOMError ( wat : unknown ) : boolean {
44- return Object . prototype . toString . call ( wat ) === '[object DOMError]' ;
50+ return isBuiltin ( wat , ' DOMError' ) ;
4551}
4652
4753/**
@@ -52,7 +58,7 @@ export function isDOMError(wat: unknown): boolean {
5258 * @returns A boolean representing the result.
5359 */
5460export function isDOMException ( wat : unknown ) : boolean {
55- return Object . prototype . toString . call ( wat ) === '[object DOMException]' ;
61+ return isBuiltin ( wat , ' DOMException' ) ;
5662}
5763
5864/**
@@ -63,7 +69,7 @@ export function isDOMException(wat: unknown): boolean {
6369 * @returns A boolean representing the result.
6470 */
6571export function isString ( wat : unknown ) : boolean {
66- return Object . prototype . toString . call ( wat ) === '[object String]' ;
72+ return isBuiltin ( wat , ' String' ) ;
6773}
6874
6975/**
@@ -85,7 +91,7 @@ export function isPrimitive(wat: unknown): wat is Primitive {
8591 * @returns A boolean representing the result.
8692 */
8793export function isPlainObject ( wat : unknown ) : wat is Record < string , unknown > {
88- return Object . prototype . toString . call ( wat ) === '[object Object]' ;
94+ return isBuiltin ( wat , ' Object' ) ;
8995}
9096
9197/**
@@ -118,7 +124,7 @@ export function isElement(wat: unknown): wat is Element {
118124 * @returns A boolean representing the result.
119125 */
120126export function isRegExp ( wat : unknown ) : wat is RegExp {
121- return Object . prototype . toString . call ( wat ) === '[object RegExp]' ;
127+ return isBuiltin ( wat , ' RegExp' ) ;
122128}
123129
124130/**
0 commit comments