From f0a235563e1eb748f4030d19af3f9a5ac59d2550 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Tue, 20 Aug 2019 01:35:43 +1000 Subject: Support custom inspection of objects (#2791) --- js/console.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'js/console.ts') diff --git a/js/console.ts b/js/console.ts index 002be6e2f..ed2543a9d 100644 --- a/js/console.ts +++ b/js/console.ts @@ -326,7 +326,9 @@ function createObjectString( value: {}, ...args: [ConsoleContext, number, number] ): string { - if (value instanceof Error) { + if (customInspect in value && typeof value[customInspect] === "function") { + return String(value[customInspect]!()); + } else if (value instanceof Error) { return String(value.stack); } else if (Array.isArray(value)) { return createArrayString(value, ...args); @@ -752,6 +754,11 @@ export class Console { } } +/** A symbol which can be used as a key for a custom method which will be called + * when `Deno.inspect()` is called, or when the object is logged to the console. + */ +export const customInspect = Symbol.for("Deno.customInspect"); + /** * `inspect()` converts input into string that has the same format * as printed by `console.log(...)`; -- cgit v1.2.3