summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Heidemann <56122437+bcheidemann@users.noreply.github.com>2022-04-25 12:59:15 +0100
committerGitHub <noreply@github.com>2022-04-25 13:59:15 +0200
commitddbfa1418c5ec2805a565caa07f7eec6af1deb39 (patch)
tree26daa26b991a0e3a465205380af22b73cb89c973
parent6dcf3a447c688130d79751ad7e6e508631f7032d (diff)
feat(ext/console): Add string abbreviation size option for "Deno.inspect" (#14384)
-rw-r--r--cli/dts/lib.deno.ns.d.ts2
-rw-r--r--cli/tests/unit/console_test.ts19
-rw-r--r--ext/console/02_console.js9
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 485b3d490..2caf05085 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 d1c570e1b..39baaf1a3 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 0f51bded8..607da2db6 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
}