1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

feat(ext/console): Add string abbreviation size option for "Deno.inspect" (#14384)

This commit is contained in:
Ben Heidemann 2022-04-25 12:59:15 +01:00 committed by GitHub
parent 6dcf3a447c
commit ddbfa1418c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View file

@ -2491,6 +2491,8 @@ declare namespace Deno {
getters?: boolean; getters?: boolean;
/** Show an object's non-enumerable properties. Defaults to false. */ /** Show an object's non-enumerable properties. Defaults to false. */
showHidden?: boolean; 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 /** Converts the input into a string that has the same format as printed by

View file

@ -1938,3 +1938,22 @@ Deno.test(function inspectColors() {
assertEquals(Deno.inspect(1), "1"); assertEquals(Deno.inspect(1), "1");
assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b["); 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 ..." ]',
);
});

View file

@ -298,6 +298,7 @@
colors: false, colors: false,
getters: false, getters: false,
showHidden: false, showHidden: false,
strAbbreviateSize: 100,
}; };
const DEFAULT_INDENT = " "; // Default indent string const DEFAULT_INDENT = " "; // Default indent string
@ -786,11 +787,15 @@
level, level,
inspectOptions, inspectOptions,
) { ) {
const abbreviateSize =
typeof inspectOptions.strAbbreviateSize === "undefined"
? STR_ABBREVIATE_SIZE
: inspectOptions.strAbbreviateSize;
const green = maybeColor(colors.green, inspectOptions); const green = maybeColor(colors.green, inspectOptions);
switch (typeof value) { switch (typeof value) {
case "string": { case "string": {
const trunc = value.length > STR_ABBREVIATE_SIZE const trunc = value.length > abbreviateSize
? StringPrototypeSlice(value, 0, STR_ABBREVIATE_SIZE) + "..." ? StringPrototypeSlice(value, 0, abbreviateSize) + "..."
: value; : value;
return green(quoteString(trunc)); // Quoted strings are green return green(quoteString(trunc)); // Quoted strings are green
} }