summaryrefslogtreecommitdiff
path: root/js/console.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-03-19 12:15:01 -0400
committerRyan Dahl <ry@tinyclouds.org>2019-03-20 12:38:59 -0400
commit8f334ae56887cc5291be20da8b9288fe7054410e (patch)
tree1261516ff20ef90bace9d74b9ceea73af79cda3b /js/console.ts
parent4c831f1eb5e5eae4d56c82c941260f5b18940369 (diff)
Improve pretty printing of objects
If an object has more than 5 elements, it is printed in abbeviated form displaying only the keys. This is useful in the REPL when inspecting large objects like the Deno namespace: > Deno { args, noColor, pid, env, exit, isTTY, execPath, chdir, cwd, File, open, stdin, stdout, stderr, read, write, seek, close, copy, toAsyncIterator, SeekMode, Buffer, readAll, mkdirSync, mkdir, makeTempDirSync, makeTempDir, chmodSync, chmod, removeSync, remove, renameSync, rename, readFileSync, readFile, readDirSync, readDir, copyFileSync, copyFile, readlinkSync, readlink, statSync, lstatSync, stat, lstat, symlinkSync, symlink, writeFileSync, writeFile, ErrorKind, DenoError, libdeno, permissions, revokePermission, truncateSync, truncate, connect, dial, listen, metrics, resources, run, Process, inspect, build, platform, version, Console, stringifyArgs, DomIterableMixin }
Diffstat (limited to 'js/console.ts')
-rw-r--r--js/console.ts24
1 files changed, 17 insertions, 7 deletions
diff --git a/js/console.ts b/js/console.ts
index 20eafbacf..bde912dd6 100644
--- a/js/console.ts
+++ b/js/console.ts
@@ -19,6 +19,10 @@ type ConsoleOptions = Partial<{
// Default depth of logging nested objects
const DEFAULT_MAX_DEPTH = 4;
+// Number of elements an object must have before it's displayed in appreviated
+// form.
+const OBJ_ABBREVIATE_SIZE = 5;
+
// Char codes
const CHAR_PERCENT = 37; /* % */
const CHAR_LOWERCASE_S = 115; /* s */
@@ -272,7 +276,6 @@ function createRawObjectString(
}
ctx.add(value);
- const entries: string[] = [];
let baseString = "";
const className = getClassInstanceName(value);
@@ -280,12 +283,19 @@ function createRawObjectString(
if (className && className !== "Object" && className !== "anonymous") {
shouldShowClassName = true;
}
-
- for (const key of Object.keys(value)) {
- entries.push(
- `${key}: ${stringifyWithQuotes(value[key], ctx, level + 1, maxLevel)}`
- );
- }
+ const keys = Object.keys(value);
+ const entries: string[] = keys.map(key => {
+ if (keys.length > OBJ_ABBREVIATE_SIZE) {
+ return key;
+ } else {
+ return `${key}: ${stringifyWithQuotes(
+ value[key],
+ ctx,
+ level + 1,
+ maxLevel
+ )}`;
+ }
+ });
ctx.delete(value);