0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -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 }); console.table({ a: "test", b: 1 });
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬────────┐ `\
(idx) Values (idx) Values
a "test" a "test"
@ -1408,12 +1409,28 @@ Deno.test(function consoleTable() {
console.table({ a: { b: 10 }, b: { b: 20, c: 30 } }, ["c"]); console.table({ a: { b: 10 }, b: { b: 20, c: 30 } }, ["c"]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬────┐ `\
(idx) c (idx) c
a a
b 30 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]]]); console.table([1, 2, [3, [4]], [5, 6], [[7], [8]]]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬───────┬───────┬────────┐ `\
(idx) 0 1 Values (idx) 0 1 Values
0 1 0 1
@ -1437,7 +1455,8 @@ Deno.test(function consoleTable() {
console.table(new Set([1, 2, 3, "test"])); console.table(new Set([1, 2, 3, "test"]));
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌────────────┬────────┐ `\
(iter idx) Values (iter idx) Values
0 1 0 1
@ -1457,7 +1476,8 @@ Deno.test(function consoleTable() {
); );
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌────────────┬─────┬────────┐ `\
(iter idx) Key Values (iter idx) Key Values
0 1 "one" 0 1 "one"
@ -1476,7 +1496,8 @@ Deno.test(function consoleTable() {
}); });
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬───────────┬───────────────────┬────────┐ `\
(idx) c e Values (idx) c e Values
a true a true
@ -1498,7 +1519,8 @@ Deno.test(function consoleTable() {
]); ]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬────────┬──────────────────────┬────┬────────┐ `\
(idx) 0 1 a Values (idx) 0 1 a Values
0 1 0 1
@ -1514,7 +1536,8 @@ Deno.test(function consoleTable() {
console.table([]); console.table([]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┐ `\
(idx) (idx)
@ -1525,7 +1548,8 @@ Deno.test(function consoleTable() {
console.table({}); console.table({});
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┐ `\
(idx) (idx)
@ -1536,7 +1560,8 @@ Deno.test(function consoleTable() {
console.table(new Set()); console.table(new Set());
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌────────────┐ `\
(iter idx) (iter idx)
@ -1547,7 +1572,8 @@ Deno.test(function consoleTable() {
console.table(new Map()); console.table(new Map());
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌────────────┐ `\
(iter idx) (iter idx)
@ -1562,7 +1588,8 @@ Deno.test(function consoleTable() {
console.table(["Hello", "你好", "Amapá"]); console.table(["Hello", "你好", "Amapá"]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬─────────┐ `\
(idx) Values (idx) Values
0 "Hello" 0 "Hello"
@ -1579,7 +1606,8 @@ Deno.test(function consoleTable() {
]); ]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬───┬───┐ `\
(idx) 0 1 (idx) 0 1
0 1 2 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"]); console.table({ 1: { a: 4, b: 5 }, 2: null, 3: { b: 6, c: 7 } }, ["b"]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬───┐ `\
(idx) b (idx) b
1 5 1 5
@ -1606,7 +1635,8 @@ Deno.test(function consoleTable() {
console.table([{ a: 0 }, { a: 1, b: 1 }, { a: 2 }, { a: 3, b: 3 }]); console.table([{ a: 0 }, { a: 1, b: 1 }, { a: 2 }, { a: 3, b: 3 }]);
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬───┬───┐ `\
(idx) a b (idx) a b
0 0 0 0
@ -1624,7 +1654,8 @@ Deno.test(function consoleTable() {
); );
assertEquals( assertEquals(
stripColor(out.toString()), stripColor(out.toString()),
`┌───────┬───┬───┬───┐ `\
(idx) a b c (idx) a b c
0 0 0 0

View file

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