Skip to content

Commit

Permalink
Fix Browser Polyfill, fix #216
Browse files Browse the repository at this point in the history
  • Loading branch information
terehov committed Feb 23, 2023
1 parent b25d322 commit 6352c32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions examples/nodejs/index2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Logger, BaseLogger } from "../../src/index.js";

import { formatValue } from "../../src/runtime/browser/util.inspect.polyfil";

const defaultLogObject: {
name: string;
} = {
Expand Down Expand Up @@ -90,3 +92,5 @@ const log = new Logger({
});

log.info(error);

////////////////////////////
33 changes: 20 additions & 13 deletions src/runtime/browser/util.inspect.polyfil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function formatError(value: Error): string {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {
export function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (
Expand All @@ -162,6 +162,9 @@ function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {
// Also filter out any prototype objects using the circular check.
!(value?.constructor && value?.constructor.prototype === value)
) {
if (typeof value.inspect !== "function" && value.toString != null) {
return value.toString();
}
let ret = value?.inspect(recurseTimes, ctx);
if (!isString(ret)) {
ret = formatValue(ctx, ret, recurseTimes);
Expand Down Expand Up @@ -194,18 +197,22 @@ function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {

// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (isFunction(value)) {
const name = value.name ? ": " + value.name : "";
return ctx.stylize("[Function" + name + "]", "special");
}
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
}
if (isDate(value)) {
return ctx.stylize(Date.prototype.toString.call(value), "date");
}
if (isError(value)) {
return formatError(value as Error);
if (isFunction(ctx.stylize)) {
if (isFunction(value)) {
const name = value.name ? ": " + value.name : "";
return ctx.stylize("[Function" + name + "]", "special");
}
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
}
if (isDate(value)) {
return ctx.stylize(Date.prototype.toString.call(value), "date");
}
if (isError(value)) {
return formatError(value as Error);
}
} else {
return value;
}
}

Expand Down

0 comments on commit 6352c32

Please sign in to comment.