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

fix(ext/console): Only right-align integers in console.table() (#17389)

This commit is contained in:
Waldir Pimenta 2023-02-08 09:14:40 +00:00 committed by GitHub
parent 0597499e9e
commit 19543ffec3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 22 deletions

View file

@ -1395,7 +1395,8 @@ Deno.test(function consoleTable() {
console.table({ a: "test", b: 1 });
assertEquals(
stripColor(out.toString()),
`┌───────┬────────┐
`\
(idx) Values
a "test"
@ -1408,12 +1409,28 @@ Deno.test(function consoleTable() {
console.table({ a: { b: 10 }, b: { b: 20, c: 30 } }, ["c"]);
assertEquals(
stripColor(out.toString()),
`┌───────┬────┐
`\
(idx) c
a
b 30
`,
);
});
mockConsole((console, out) => {
console.table([[1, 1], [234, 2.34], [56789, 56.789]]);
assertEquals(
stripColor(out.toString()),
`\
(idx) 0 1
0 1 1
1 234 2.34
2 56789 56.789
`,
);
});
@ -1421,7 +1438,8 @@ Deno.test(function consoleTable() {
console.table([1, 2, [3, [4]], [5, 6], [[7], [8]]]);
assertEquals(
stripColor(out.toString()),
`┌───────┬───────┬───────┬────────┐
`\
(idx) 0 1 Values
0 1
@ -1437,7 +1455,8 @@ Deno.test(function consoleTable() {
console.table(new Set([1, 2, 3, "test"]));
assertEquals(
stripColor(out.toString()),
`┌────────────┬────────┐
`\
(iter idx) Values
0 1
@ -1457,7 +1476,8 @@ Deno.test(function consoleTable() {
);
assertEquals(
stripColor(out.toString()),
`┌────────────┬─────┬────────┐
`\
(iter idx) Key Values
0 1 "one"
@ -1476,7 +1496,8 @@ Deno.test(function consoleTable() {
});
assertEquals(
stripColor(out.toString()),
`┌───────┬───────────┬───────────────────┬────────┐
`\
(idx) c e Values
a true
@ -1498,7 +1519,8 @@ Deno.test(function consoleTable() {
]);
assertEquals(
stripColor(out.toString()),
`┌───────┬────────┬──────────────────────┬────┬────────┐
`\
(idx) 0 1 a Values
0 1
@ -1514,7 +1536,8 @@ Deno.test(function consoleTable() {
console.table([]);
assertEquals(
stripColor(out.toString()),
`┌───────┐
`\
(idx)
@ -1525,7 +1548,8 @@ Deno.test(function consoleTable() {
console.table({});
assertEquals(
stripColor(out.toString()),
`┌───────┐
`\
(idx)
@ -1536,7 +1560,8 @@ Deno.test(function consoleTable() {
console.table(new Set());
assertEquals(
stripColor(out.toString()),
`┌────────────┐
`\
(iter idx)
@ -1547,7 +1572,8 @@ Deno.test(function consoleTable() {
console.table(new Map());
assertEquals(
stripColor(out.toString()),
`┌────────────┐
`\
(iter idx)
@ -1562,7 +1588,8 @@ Deno.test(function consoleTable() {
console.table(["Hello", "你好", "Amapá"]);
assertEquals(
stripColor(out.toString()),
`┌───────┬─────────┐
`\
(idx) Values
0 "Hello"
@ -1579,7 +1606,8 @@ Deno.test(function consoleTable() {
]);
assertEquals(
stripColor(out.toString()),
`┌───────┬───┬───┐
`\
(idx) 0 1
0 1 2
@ -1592,7 +1620,8 @@ Deno.test(function consoleTable() {
console.table({ 1: { a: 4, b: 5 }, 2: null, 3: { b: 6, c: 7 } }, ["b"]);
assertEquals(
stripColor(out.toString()),
`┌───────┬───┐
`\
(idx) b
1 5
@ -1606,7 +1635,8 @@ Deno.test(function consoleTable() {
console.table([{ a: 0 }, { a: 1, b: 1 }, { a: 2 }, { a: 3, b: 3 }]);
assertEquals(
stripColor(out.toString()),
`┌───────┬───┬───┐
`\
(idx) a b
0 0
@ -1624,7 +1654,8 @@ Deno.test(function consoleTable() {
);
assertEquals(
stripColor(out.toString()),
`┌───────┬───┬───┬───┐
`\
(idx) a b c
0 0

View file

@ -47,6 +47,7 @@ const {
StringPrototypeIncludes,
StringPrototypeStartsWith,
TypeError,
NumberIsInteger,
NumberParseInt,
RegExp,
RegExpPrototype,
@ -232,11 +233,6 @@ function renderRow(row, columnWidths, columnRightAlign) {
return out;
}
function canRightAlign(value) {
const isNumber = !isNaN(value);
return isNumber;
}
function cliTable(head, columns) {
const rows = [];
const columnWidths = ArrayPrototypeMap(head, (h) => getStringWidth(h));
@ -257,7 +253,7 @@ function cliTable(head, columns) {
const width = columnWidths[i] || 0;
const counted = getStringWidth(value);
columnWidths[i] = MathMax(width, counted);
columnRightAlign[i] &= canRightAlign(value);
columnRightAlign[i] &= NumberIsInteger(+value);
}
}