Skip to content

feat(utils): Try to optimize bundle size around type checks #4300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions packages/utils/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */

import { Primitive } from '@sentry/types';

// eslint-disable-next-line @typescript-eslint/unbound-method
const objectToString = Object.prototype.toString;

/**
* Checks whether given value's type is one of a few Error or Error-like
* {@link isError}.
Expand All @@ -10,18 +14,20 @@ import { Primitive } from '@sentry/types';
* @returns A boolean representing the result.
*/
export function isError(wat: unknown): boolean {
switch (Object.prototype.toString.call(wat)) {
switch (objectToString.call(wat)) {
case '[object Error]':
return true;
case '[object Exception]':
return true;
case '[object DOMException]':
return true;
default:
return isInstanceOf(wat, Error);
}
}

function isBuiltin(wat: unknown, ty: string): boolean {
return objectToString.call(wat) === `[object ${ty}]`;
}

/**
* Checks whether given value's type is ErrorEvent
* {@link isErrorEvent}.
Expand All @@ -30,7 +36,7 @@ export function isError(wat: unknown): boolean {
* @returns A boolean representing the result.
*/
export function isErrorEvent(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object ErrorEvent]';
return isBuiltin(wat, 'ErrorEvent');
}

/**
Expand All @@ -41,7 +47,7 @@ export function isErrorEvent(wat: unknown): boolean {
* @returns A boolean representing the result.
*/
export function isDOMError(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object DOMError]';
return isBuiltin(wat, 'DOMError');
}

/**
Expand All @@ -52,7 +58,7 @@ export function isDOMError(wat: unknown): boolean {
* @returns A boolean representing the result.
*/
export function isDOMException(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object DOMException]';
return isBuiltin(wat, 'DOMException');
}

/**
Expand All @@ -63,7 +69,7 @@ export function isDOMException(wat: unknown): boolean {
* @returns A boolean representing the result.
*/
export function isString(wat: unknown): boolean {
return Object.prototype.toString.call(wat) === '[object String]';
return isBuiltin(wat, 'String');
}

/**
Expand All @@ -85,7 +91,7 @@ export function isPrimitive(wat: unknown): wat is Primitive {
* @returns A boolean representing the result.
*/
export function isPlainObject(wat: unknown): wat is Record<string, unknown> {
return Object.prototype.toString.call(wat) === '[object Object]';
return isBuiltin(wat, 'Object');
}

/**
Expand Down Expand Up @@ -118,7 +124,7 @@ export function isElement(wat: unknown): wat is Element {
* @returns A boolean representing the result.
*/
export function isRegExp(wat: unknown): wat is RegExp {
return Object.prototype.toString.call(wat) === '[object RegExp]';
return isBuiltin(wat, 'RegExp');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ export function normalizeToSize<T>(
* unchanged.
*/
function serializeValue(value: any): any {
const type = Object.prototype.toString.call(value);

// Node.js REPL notation
if (typeof value === 'string') {
return value;
}

const type = Object.prototype.toString.call(value);
if (type === '[object Object]') {
return '[Object]';
}
Expand Down