1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -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!;
}
let hasPrimitives = false;
Object.keys(resultData).forEach((k, idx): void => {
const value: unknown = resultData[k]!;
if (value !== null && typeof value === "object") {
Object.entries(value as { [key: string]: unknown }).forEach(
([k, v]): void => {
if (properties && !properties.includes(k)) {
return;
}
if (objectValues[k]) {
objectValues[k].push(stringifyValue(v));
} else {
objectValues[k] = createColumn(v, idx);
}
}
);
values.push("");
} else {
const primitive =
value === null ||
(typeof value !== "function" && typeof value !== "object");
if (properties === undefined && primitive) {
hasPrimitives = true;
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]) {
// 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 {
objectValues[k] = createColumn(valueObj[k], idx);
}
}
}
values.push("");
}
indexKeys.push(k);
@ -872,10 +878,7 @@ export class Console {
const bodyValues = Object.values(objectValues);
const header = [
indexKey,
...(properties || [
...headerKeys,
!isMap && values.length > 0 && valuesKey,
]),
...(properties || [...headerKeys, !isMap && hasPrimitives && valuesKey]),
].filter(Boolean) as string[];
const body = [indexKeys, ...bodyValues, values];

View file

@ -1070,6 +1070,36 @@ unitTest(function consoleTable(): void {
1 "你好"
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
`
);
});