console: fix console.table() display edge case

If the properties are not specified in `console.table()`, then we should
make a best effort to determine them rather than put all values into a
"Values" column.

PR-URL: https://github.com/nodejs/node/pull/20323
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Rich Trott 2018-04-25 22:47:56 -07:00
parent 65d97c96aa
commit 99d56a4749
2 changed files with 21 additions and 14 deletions

View File

@ -363,9 +363,7 @@ Console.prototype.table = function(tabularData, properties) {
tabularData = previewSetIterator(tabularData); tabularData = previewSetIterator(tabularData);
const setlike = setIter || isSet(tabularData); const setlike = setIter || isSet(tabularData);
if (setlike || if (setlike) {
(properties === undefined &&
(isArray(tabularData) || isTypedArray(tabularData)))) {
const values = []; const values = [];
let length = 0; let length = 0;
for (const v of tabularData) { for (const v of tabularData) {

View File

@ -41,13 +41,13 @@ test([1, 2, 3], `
`); `);
test([Symbol(), 5, [10]], ` test([Symbol(), 5, [10]], `
(index) Values (index) 0 Values
0 Symbol() 0 Symbol()
1 5 1 5
2 [ 10 ] 2 10
`); `);
test([undefined, 5], ` test([undefined, 5], `
@ -182,10 +182,10 @@ test({ a: undefined }, ['x'], `
`); `);
test([], ` test([], `
(index) Values (index)
`); `);
test(new Map(), ` test(new Map(), `
@ -194,3 +194,12 @@ test(new Map(), `
`); `);
test([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], `
(index) a b
0 1 'Y'
1 'Z' 2
`);