mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00
pretty-print long strings
This commit is contained in:
parent
8f334ae568
commit
842627d6b9
2 changed files with 18 additions and 1 deletions
|
@ -23,6 +23,8 @@ const DEFAULT_MAX_DEPTH = 4;
|
||||||
// form.
|
// form.
|
||||||
const OBJ_ABBREVIATE_SIZE = 5;
|
const OBJ_ABBREVIATE_SIZE = 5;
|
||||||
|
|
||||||
|
const STR_ABBREVIATE_SIZE = 100;
|
||||||
|
|
||||||
// Char codes
|
// Char codes
|
||||||
const CHAR_PERCENT = 37; /* % */
|
const CHAR_PERCENT = 37; /* % */
|
||||||
const CHAR_LOWERCASE_S = 115; /* s */
|
const CHAR_LOWERCASE_S = 115; /* s */
|
||||||
|
@ -151,7 +153,11 @@ function stringifyWithQuotes(
|
||||||
): string {
|
): string {
|
||||||
switch (typeof value) {
|
switch (typeof value) {
|
||||||
case "string":
|
case "string":
|
||||||
return `"${value}"`;
|
const trunc =
|
||||||
|
value.length > STR_ABBREVIATE_SIZE
|
||||||
|
? value.slice(0, STR_ABBREVIATE_SIZE) + "..."
|
||||||
|
: value;
|
||||||
|
return JSON.stringify(trunc);
|
||||||
default:
|
default:
|
||||||
return stringify(value, ctx, level, maxLevel);
|
return stringify(value, ctx, level, maxLevel);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,17 @@ test(function consoleTestStringifyComplexObjects() {
|
||||||
assertEquals(stringify({ foo: "bar" }), `{ foo: "bar" }`);
|
assertEquals(stringify({ foo: "bar" }), `{ foo: "bar" }`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test(function consoleTestStringifyLongStrings() {
|
||||||
|
const veryLongString = "a".repeat(200);
|
||||||
|
// If we stringify an object containing the long string, it gets abbreviated.
|
||||||
|
let actual = stringify({ veryLongString });
|
||||||
|
assert(actual.includes("..."));
|
||||||
|
assert(actual.length < 200);
|
||||||
|
// However if we stringify the string itself, we get it exactly.
|
||||||
|
actual = stringify(veryLongString);
|
||||||
|
assertEquals(actual, veryLongString);
|
||||||
|
});
|
||||||
|
|
||||||
test(function consoleTestStringifyCircular() {
|
test(function consoleTestStringifyCircular() {
|
||||||
class Base {
|
class Base {
|
||||||
a = 1;
|
a = 1;
|
||||||
|
|
Loading…
Add table
Reference in a new issue