Skip to content

Commit 6352c32

Browse files
committed
Fix Browser Polyfill, fix #216
1 parent b25d322 commit 6352c32

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

examples/nodejs/index2.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Logger, BaseLogger } from "../../src/index.js";
22

3+
import { formatValue } from "../../src/runtime/browser/util.inspect.polyfil";
4+
35
const defaultLogObject: {
46
name: string;
57
} = {
@@ -90,3 +92,5 @@ const log = new Logger({
9092
});
9193

9294
log.info(error);
95+
96+
////////////////////////////

src/runtime/browser/util.inspect.polyfil.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function formatError(value: Error): string {
150150
}
151151

152152
// eslint-disable-next-line @typescript-eslint/no-explicit-any
153-
function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {
153+
export function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {
154154
// Provide a hook for user-specified inspect functions.
155155
// Check that value is an object with an inspect function on it
156156
if (
@@ -162,6 +162,9 @@ function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {
162162
// Also filter out any prototype objects using the circular check.
163163
!(value?.constructor && value?.constructor.prototype === value)
164164
) {
165+
if (typeof value.inspect !== "function" && value.toString != null) {
166+
return value.toString();
167+
}
165168
let ret = value?.inspect(recurseTimes, ctx);
166169
if (!isString(ret)) {
167170
ret = formatValue(ctx, ret, recurseTimes);
@@ -194,18 +197,22 @@ function formatValue(ctx: ICtx, value: any, recurseTimes = 0): string {
194197

195198
// Some type of object without properties can be shortcutted.
196199
if (keys.length === 0) {
197-
if (isFunction(value)) {
198-
const name = value.name ? ": " + value.name : "";
199-
return ctx.stylize("[Function" + name + "]", "special");
200-
}
201-
if (isRegExp(value)) {
202-
return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
203-
}
204-
if (isDate(value)) {
205-
return ctx.stylize(Date.prototype.toString.call(value), "date");
206-
}
207-
if (isError(value)) {
208-
return formatError(value as Error);
200+
if (isFunction(ctx.stylize)) {
201+
if (isFunction(value)) {
202+
const name = value.name ? ": " + value.name : "";
203+
return ctx.stylize("[Function" + name + "]", "special");
204+
}
205+
if (isRegExp(value)) {
206+
return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
207+
}
208+
if (isDate(value)) {
209+
return ctx.stylize(Date.prototype.toString.call(value), "date");
210+
}
211+
if (isError(value)) {
212+
return formatError(value as Error);
213+
}
214+
} else {
215+
return value;
209216
}
210217
}
211218

0 commit comments

Comments
 (0)