Spaces:
Runtime error
Runtime error
File size: 2,262 Bytes
1fa8efd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | const TypedArrayPrototypeGetSymbolToStringTag = (() => {
// Type check system lovingly referenced from:
// https://github.com/nodejs/node/blob/7450332339ed40481f470df2a3014e2ec355d8d8/lib/internal/util/types.js#L13-L15
// eslint-disable-next-line @typescript-eslint/unbound-method -- the intention is to call this method with a bound value
const g = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(Uint8Array.prototype),
Symbol.toStringTag
)!.get!;
return (value: unknown) => g.call(value);
})();
export function isUint8Array(value: unknown): value is Uint8Array {
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
}
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
return (
typeof value === 'object' &&
value != null &&
Symbol.toStringTag in value &&
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
value[Symbol.toStringTag] === 'SharedArrayBuffer')
);
}
export function isRegExp(regexp: unknown): regexp is RegExp {
return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]';
}
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(date: unknown): date is Date {
return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]';
}
export type InspectFn = (x: unknown, options?: unknown) => string;
export function defaultInspect(x: unknown, _options?: unknown): string {
return JSON.stringify(x, (k: string, v: unknown) => {
if (typeof v === 'bigint') {
return { $numberLong: `${v}` };
} else if (isMap(v)) {
return Object.fromEntries(v);
}
return v;
});
}
/** @internal */
type StylizeFunction = (x: string, style: string) => string;
/** @internal */
export function getStylizeFunction(options?: unknown): StylizeFunction | undefined {
const stylizeExists =
options != null &&
typeof options === 'object' &&
'stylize' in options &&
typeof options.stylize === 'function';
if (stylizeExists) {
return options.stylize as StylizeFunction;
}
}
|