diff options
author | Philip Chimento <philip.chimento@gmail.com> | 2022-11-03 21:45:50 -0700 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2023-01-01 17:13:51 -0700 |
commit | 3a0d2e25e2e8533cb5df191c56378832b11b84e3 (patch) | |
tree | 157a51ec5a02270032e4ec9a43a2554fd26ef802 /modules | |
parent | 7170ad6938a448682c525451d79c86d07509d0ca (diff) | |
download | gjs-3a0d2e25e2e8533cb5df191c56378832b11b84e3.tar.gz |
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.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/script/_bootstrap/default.js | 26 |
1 files changed, 15 insertions, 11 deletions
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(); } } |