1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

console: replace object abbreviation with line breaking (#4425)

This commit is contained in:
Michał Sabiniarz 2020-03-24 05:57:05 +01:00 committed by GitHub
parent c61a231ff4
commit 2e5e5fe393
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 32 deletions

View file

@ -109,7 +109,32 @@ unitTest(function consoleTestStringifyCircular(): void {
};
nestedObj.o = circularObj;
const nestedObjExpected = `{ num, bool, str, method, asyncMethod, generatorMethod, un, nu, arrowFunc, extendedClass, nFunc, extendedCstr, o }`;
const nestedObjExpected = `{
num: 1,
bool: true,
str: "a",
method: [Function: method],
asyncMethod: [AsyncFunction: asyncMethod],
generatorMethod: [GeneratorFunction: generatorMethod],
un: undefined,
nu: null,
arrowFunc: [Function: arrowFunc],
extendedClass: Extended { a: 1, b: 2 },
nFunc: [Function],
extendedCstr: [Function: Extended],
o: {
num: 2,
bool: false,
str: "b",
method: [Function: method],
un: undefined,
nu: null,
nested: [Circular],
emptyObj: {},
arr: [ 1, "s", false, null, [Circular] ],
baseClass: Base { a: 1 }
}
}`;
assertEquals(stringify(1), "1");
assertEquals(stringify(-0), "-0");
@ -166,7 +191,30 @@ unitTest(function consoleTestStringifyCircular(): void {
assertEquals(stringify(JSON), 'JSON { Symbol(Symbol.toStringTag): "JSON" }');
assertEquals(
stringify(console),
"{ printFunc, log, debug, info, dir, dirxml, warn, error, assert, count, countReset, table, time, timeLog, timeEnd, group, groupCollapsed, groupEnd, clear, trace, indentLevel, Symbol(isConsoleInstance) }"
`{
printFunc: [Function],
log: [Function],
debug: [Function],
info: [Function],
dir: [Function],
dirxml: [Function],
warn: [Function],
error: [Function],
assert: [Function],
count: [Function],
countReset: [Function],
table: [Function],
time: [Function],
timeLog: [Function],
timeEnd: [Function],
group: [Function],
groupCollapsed: [Function],
groupEnd: [Function],
clear: [Function],
trace: [Function],
indentLevel: 0,
Symbol(isConsoleInstance): true
}`
);
assertEquals(
stringify({ str: 1, [Symbol.for("sym")]: 2, [Symbol.toStringTag]: "TAG" }),
@ -200,6 +248,42 @@ unitTest(function consoleTestStringifyWithDepth(): void {
);
});
unitTest(function consoleTestStringifyLargeObject(): void {
const obj = {
a: 2,
o: {
a: "1",
b: "2",
c: "3",
d: "4",
e: "5",
f: "6",
g: 10,
asd: 2,
asda: 3,
x: { a: "asd", x: 3 }
}
};
assertEquals(
stringify(obj),
`{
a: 2,
o: {
a: "1",
b: "2",
c: "3",
d: "4",
e: "5",
f: "6",
g: 10,
asd: 2,
asda: 3,
x: { a: "asd", x: 3 }
}
}`
);
});
unitTest(function consoleTestWithCustomInspector(): void {
class A {
[customInspect](): string {

View file

@ -16,9 +16,7 @@ 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;
const LINE_BREAKING_LENGTH = 80;
const STR_ABBREVIATE_SIZE = 100;
@ -299,15 +297,7 @@ function createRawObjectString(
const entries: string[] = [];
const stringKeys = Object.keys(value);
const symbolKeys = Object.getOwnPropertySymbols(value);
const numKeys = stringKeys.length + symbolKeys.length;
if (numKeys > OBJ_ABBREVIATE_SIZE) {
for (const key of stringKeys) {
entries.push(key);
}
for (const key of symbolKeys) {
entries.push(key.toString());
}
} else {
for (const key of stringKeys) {
entries.push(
`${key}: ${stringifyWithQuotes(value[key], ctx, level + 1, maxLevel)}`
@ -324,12 +314,19 @@ function createRawObjectString(
)}`
);
}
}
const totalLength = entries.length + level + entries.join("").length;
ctx.delete(value);
if (entries.length === 0) {
baseString = "{}";
} else if (totalLength > LINE_BREAKING_LENGTH) {
const entryIndent = " ".repeat(level + 1);
const closingIndent = " ".repeat(level);
baseString = `{\n${entryIndent}${entries.join(
`,\n${entryIndent}`
)}\n${closingIndent}}`;
} else {
baseString = `{ ${entries.join(", ")} }`;
}

View file

@ -1,5 +1,9 @@
{ a: 123, b: [ 1, 2, 3 ], c: null }
123
{ $var: { a: 123, b: [ 1, 2, 3 ], c: null }, with space: "invalid variable name", function: "reserved word" }
{
$var: { a: 123, b: [ 1, 2, 3 ], c: null },
with space: "invalid variable name",
function: "reserved word"
}
invalid variable name
just a string

View file

@ -1 +1,5 @@
Module { add_one: [Function: 0], memory: WebAssembly.Memory {}, Symbol(Symbol.toStringTag): "Module" }
Module {
add_one: [Function: 0],
memory: WebAssembly.Memory {},
Symbol(Symbol.toStringTag): "Module"
}