From ddbfa1418c5ec2805a565caa07f7eec6af1deb39 Mon Sep 17 00:00:00 2001 From: Ben Heidemann <56122437+bcheidemann@users.noreply.github.com> Date: Mon, 25 Apr 2022 12:59:15 +0100 Subject: [PATCH] feat(ext/console): Add string abbreviation size option for "Deno.inspect" (#14384) --- cli/dts/lib.deno.ns.d.ts | 2 ++ cli/tests/unit/console_test.ts | 19 +++++++++++++++++++ ext/console/02_console.js | 9 +++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index 485b3d4905..2caf05085e 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -2491,6 +2491,8 @@ declare namespace Deno { getters?: boolean; /** Show an object's non-enumerable properties. Defaults to false. */ showHidden?: boolean; + /** The maximum length of a string before it is truncated with an ellipsis */ + strAbbreviateSize?: number; } /** Converts the input into a string that has the same format as printed by diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index d1c570e1b2..39baaf1a36 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -1938,3 +1938,22 @@ Deno.test(function inspectColors() { assertEquals(Deno.inspect(1), "1"); assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b["); }); + +Deno.test(function inspectStringAbbreviation() { + const LONG_STRING = + "This is a really long string which will be abbreviated with ellipsis."; + const obj = { + str: LONG_STRING, + }; + const arr = [LONG_STRING]; + + assertEquals( + Deno.inspect(obj, { strAbbreviateSize: 10 }), + '{ str: "This is a ..." }', + ); + + assertEquals( + Deno.inspect(arr, { strAbbreviateSize: 10 }), + '[ "This is a ..." ]', + ); +}); diff --git a/ext/console/02_console.js b/ext/console/02_console.js index 0f51bded83..607da2db67 100644 --- a/ext/console/02_console.js +++ b/ext/console/02_console.js @@ -298,6 +298,7 @@ colors: false, getters: false, showHidden: false, + strAbbreviateSize: 100, }; const DEFAULT_INDENT = " "; // Default indent string @@ -786,11 +787,15 @@ level, inspectOptions, ) { + const abbreviateSize = + typeof inspectOptions.strAbbreviateSize === "undefined" + ? STR_ABBREVIATE_SIZE + : inspectOptions.strAbbreviateSize; const green = maybeColor(colors.green, inspectOptions); switch (typeof value) { case "string": { - const trunc = value.length > STR_ABBREVIATE_SIZE - ? StringPrototypeSlice(value, 0, STR_ABBREVIATE_SIZE) + "..." + const trunc = value.length > abbreviateSize + ? StringPrototypeSlice(value, 0, abbreviateSize) + "..." : value; return green(quoteString(trunc)); // Quoted strings are green }