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:
parent
c61a231ff4
commit
2e5e5fe393
4 changed files with 121 additions and 32 deletions
|
@ -109,7 +109,32 @@ unitTest(function consoleTestStringifyCircular(): void {
|
||||||
};
|
};
|
||||||
|
|
||||||
nestedObj.o = circularObj;
|
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(1), "1");
|
||||||
assertEquals(stringify(-0), "-0");
|
assertEquals(stringify(-0), "-0");
|
||||||
|
@ -166,7 +191,30 @@ unitTest(function consoleTestStringifyCircular(): void {
|
||||||
assertEquals(stringify(JSON), 'JSON { Symbol(Symbol.toStringTag): "JSON" }');
|
assertEquals(stringify(JSON), 'JSON { Symbol(Symbol.toStringTag): "JSON" }');
|
||||||
assertEquals(
|
assertEquals(
|
||||||
stringify(console),
|
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(
|
assertEquals(
|
||||||
stringify({ str: 1, [Symbol.for("sym")]: 2, [Symbol.toStringTag]: "TAG" }),
|
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 {
|
unitTest(function consoleTestWithCustomInspector(): void {
|
||||||
class A {
|
class A {
|
||||||
[customInspect](): string {
|
[customInspect](): string {
|
||||||
|
|
|
@ -16,9 +16,7 @@ type ConsoleOptions = Partial<{
|
||||||
// Default depth of logging nested objects
|
// Default depth of logging nested objects
|
||||||
const DEFAULT_MAX_DEPTH = 4;
|
const DEFAULT_MAX_DEPTH = 4;
|
||||||
|
|
||||||
// Number of elements an object must have before it's displayed in appreviated
|
const LINE_BREAKING_LENGTH = 80;
|
||||||
// form.
|
|
||||||
const OBJ_ABBREVIATE_SIZE = 5;
|
|
||||||
|
|
||||||
const STR_ABBREVIATE_SIZE = 100;
|
const STR_ABBREVIATE_SIZE = 100;
|
||||||
|
|
||||||
|
@ -299,37 +297,36 @@ function createRawObjectString(
|
||||||
const entries: string[] = [];
|
const entries: string[] = [];
|
||||||
const stringKeys = Object.keys(value);
|
const stringKeys = Object.keys(value);
|
||||||
const symbolKeys = Object.getOwnPropertySymbols(value);
|
const symbolKeys = Object.getOwnPropertySymbols(value);
|
||||||
const numKeys = stringKeys.length + symbolKeys.length;
|
|
||||||
if (numKeys > OBJ_ABBREVIATE_SIZE) {
|
for (const key of stringKeys) {
|
||||||
for (const key of stringKeys) {
|
entries.push(
|
||||||
entries.push(key);
|
`${key}: ${stringifyWithQuotes(value[key], ctx, level + 1, maxLevel)}`
|
||||||
}
|
);
|
||||||
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)}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
for (const key of symbolKeys) {
|
|
||||||
entries.push(
|
|
||||||
`${key.toString()}: ${stringifyWithQuotes(
|
|
||||||
// @ts-ignore
|
|
||||||
value[key],
|
|
||||||
ctx,
|
|
||||||
level + 1,
|
|
||||||
maxLevel
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
for (const key of symbolKeys) {
|
||||||
|
entries.push(
|
||||||
|
`${key.toString()}: ${stringifyWithQuotes(
|
||||||
|
// @ts-ignore
|
||||||
|
value[key],
|
||||||
|
ctx,
|
||||||
|
level + 1,
|
||||||
|
maxLevel
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalLength = entries.length + level + entries.join("").length;
|
||||||
|
|
||||||
ctx.delete(value);
|
ctx.delete(value);
|
||||||
|
|
||||||
if (entries.length === 0) {
|
if (entries.length === 0) {
|
||||||
baseString = "{}";
|
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 {
|
} else {
|
||||||
baseString = `{ ${entries.join(", ")} }`;
|
baseString = `{ ${entries.join(", ")} }`;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
{ a: 123, b: [ 1, 2, 3 ], c: null }
|
{ a: 123, b: [ 1, 2, 3 ], c: null }
|
||||||
123
|
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
|
invalid variable name
|
||||||
just a string
|
just a string
|
||||||
|
|
|
@ -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"
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue