From 3a0d2e25e2e8533cb5df191c56378832b11b84e3 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Thu, 3 Nov 2022 21:45:50 -0700 Subject: print: Refactor check for nonstandard toString in pretty-print In the pretty-printer, if an object or a function has has its toString() method overridden, the overridden toString() is preferred. Refactor this check slightly so that we can put more cases in the switch statement. --- modules/script/_bootstrap/default.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'modules') diff --git a/modules/script/_bootstrap/default.js b/modules/script/_bootstrap/default.js index 91b5eca1..161418c0 100644 --- a/modules/script/_bootstrap/default.js +++ b/modules/script/_bootstrap/default.js @@ -24,19 +24,23 @@ } function prettyPrint(value) { - if (value.toString === Object.prototype.toString || value.toString === Array.prototype.toString || value.toString === Function.prototype.toString || value.toString === Date.prototype.toString) { - const printedObjects = new WeakSet(); - switch (typeof value) { - case 'object': + switch (typeof value) { + case 'object': + if (value.toString === Object.prototype.toString || + value.toString === Array.prototype.toString || + value.toString === Date.prototype.toString) { + const printedObjects = new WeakSet(); return formatObject(value, printedObjects); - case 'function': - return formatFunction(value); - default: - return value.toString(); } - } else { - if (typeof value === 'string') - return JSON.stringify(value); + // If the object has a nonstandard toString, prefer that + return value.toString(); + case 'function': + if (value.toString === Function.prototype.toString) + return formatFunction(value); + return value.toString(); + case 'string': + return JSON.stringify(value); + default: return value.toString(); } } -- cgit v1.2.1