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

console: Hide values for console.table if display not necessary (#5914)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2020-05-28 05:30:32 -07:00 committed by GitHub
parent f6e58b076c
commit 3cbcdd4250
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 19 deletions

View file

@ -842,27 +842,33 @@ export class Console {
resultData = data!; resultData = data!;
} }
let hasPrimitives = false;
Object.keys(resultData).forEach((k, idx): void => { Object.keys(resultData).forEach((k, idx): void => {
const value: unknown = resultData[k]!; const value: unknown = resultData[k]!;
const primitive =
if (value !== null && typeof value === "object") { value === null ||
Object.entries(value as { [key: string]: unknown }).forEach( (typeof value !== "function" && typeof value !== "object");
([k, v]): void => { if (properties === undefined && primitive) {
if (properties && !properties.includes(k)) { hasPrimitives = true;
return; values.push(stringifyValue(value));
} } else {
const valueObj = (value as { [key: string]: unknown }) || {};
const keys = properties || Object.keys(valueObj);
for (const k of keys) {
if (primitive || !valueObj.hasOwnProperty(k)) {
if (objectValues[k]) { if (objectValues[k]) {
objectValues[k].push(stringifyValue(v)); // fill with blanks for idx to avoid misplacing from later values
objectValues[k].push("");
}
} else {
if (objectValues[k]) {
objectValues[k].push(stringifyValue(valueObj[k]));
} else { } else {
objectValues[k] = createColumn(v, idx); objectValues[k] = createColumn(valueObj[k], idx);
} }
} }
); }
values.push(""); values.push("");
} else {
values.push(stringifyValue(value));
} }
indexKeys.push(k); indexKeys.push(k);
@ -872,10 +878,7 @@ export class Console {
const bodyValues = Object.values(objectValues); const bodyValues = Object.values(objectValues);
const header = [ const header = [
indexKey, indexKey,
...(properties || [ ...(properties || [...headerKeys, !isMap && hasPrimitives && valuesKey]),
...headerKeys,
!isMap && values.length > 0 && valuesKey,
]),
].filter(Boolean) as string[]; ].filter(Boolean) as string[];
const body = [indexKeys, ...bodyValues, values]; const body = [indexKeys, ...bodyValues, values];

View file

@ -571,7 +571,7 @@ unitTest(function consoleTestStringifyIterable() {
`[ <4 empty items>, 0, 0, <4 empty items> ]` `[ <4 empty items>, 0, 0, <4 empty items> ]`
); );
/* TODO(ry) Fix this test /* TODO(ry) Fix this test
const lWithEmptyEl = Array(200); const lWithEmptyEl = Array(200);
lWithEmptyEl.fill(0, 50, 80); lWithEmptyEl.fill(0, 50, 80);
assertEquals( assertEquals(
@ -1070,6 +1070,36 @@ unitTest(function consoleTable(): void {
1 "你好" 1 "你好"
2 "Amapá" 2 "Amapá"
`
);
});
mockConsole((console, out): void => {
console.table([
[1, 2],
[3, 4],
]);
assertEquals(
stripColor(out.toString()),
`┌───────┬───┬───┐
(idx) 0 1
0 1 2
1 3 4
`
);
});
mockConsole((console, out): void => {
console.table({ 1: { a: 4, b: 5 }, 2: null, 3: { b: 6, c: 7 } }, ["b"]);
assertEquals(
stripColor(out.toString()),
`┌───────┬───┐
(idx) b
1 5
2
3 6
` `
); );
}); });