Skip to content
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

fix(NODE-6552): remove cache and use toStringTag in type helpers #740

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Changes from 4 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
78 changes: 34 additions & 44 deletions src/parser/utils.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,55 @@
const map = new WeakMap<object, string>();

const TYPES = {
ArrayBuffer: '[object ArrayBuffer]',
SharedArrayBuffer: '[object SharedArrayBuffer]',
Uint8Array: '[object Uint8Array]',
BigInt64Array: '[object BigInt64Array]',
BigUint64Array: '[object BigUint64Array]',
RegExp: '[object RegExp]',
Map: '[object Map]',
Date: '[object Date]'
};

/**
* Retrieves the prototype.toString() of a value.
* If the value is an object, it will cache the result in a WeakMap for future use.
*/
function getPrototypeString(value: unknown): string {
let str = map.get(value as object);

if (!str) {
str = Object.prototype.toString.call(value);
if (value !== null && typeof value === 'object') {
map.set(value, str);
}
}
return str;
}

export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
const type = getPrototypeString(value);
return type === TYPES.ArrayBuffer || type === TYPES.SharedArrayBuffer;
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
value[Symbol.toStringTag] === 'SharedArrayBuffer')
);
}

export function isUint8Array(value: unknown): value is Uint8Array {
const type = getPrototypeString(value);
return type === TYPES.Uint8Array;
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
value[Symbol.toStringTag] === 'Uint8Array'
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
);
}

export function isBigInt64Array(value: unknown): value is BigInt64Array {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
const type = getPrototypeString(value);
return type === TYPES.BigInt64Array;
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
value[Symbol.toStringTag] === 'BigInt64Array'
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
);
}

export function isBigUInt64Array(value: unknown): value is BigUint64Array {
const type = getPrototypeString(value);
return type === TYPES.BigUint64Array;
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
value[Symbol.toStringTag] === 'BigUint64Array'
);
}

export function isRegExp(d: unknown): d is RegExp {
const type = getPrototypeString(d);
return type === TYPES.RegExp;
return Object.prototype.toString.call(d) === '[object RegExp]';
}

export function isMap(d: unknown): d is Map<unknown, unknown> {
const type = getPrototypeString(d);
return type === TYPES.Map;
export function isMap(value: unknown): value is Map<unknown, unknown> {
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
value[Symbol.toStringTag] === 'Map'
);
}

export function isDate(d: unknown): d is Date {
const type = getPrototypeString(d);
return type === TYPES.Date;
return Object.prototype.toString.call(d) === '[object Date]';
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
}

export type InspectFn = (x: unknown, options?: unknown) => string;
Expand Down