Skip to content

Commit fd0894b

Browse files
rubennortefacebook-github-bot
authored andcommitted
Add support for the columns option in console.table (facebook#48592)
Summary: Pull Request resolved: facebook#48592 Changelog: [General][Added] Add support for the second parameter of `console.table` to specify a list of columns to print in the table. Reviewed By: javache Differential Revision: D67803665 fbshipit-source-id: 354476404bad7cd2d280c8b3d963d5acba41f86b
1 parent 7154c62 commit fd0894b

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

packages/polyfills/__tests__/console-itest.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,67 @@ describe('console', () => {
284284
global.nativeLoggingHook = originalNativeLoggingHook;
285285
}
286286
});
287+
288+
it('should only print the selected columns, if specified (arrays)', () => {
289+
const originalNativeLoggingHook = global.nativeLoggingHook;
290+
const logFn = (global.nativeLoggingHook = jest.fn());
291+
292+
try {
293+
console.table(
294+
[
295+
{first: 1, second: 2, third: 3},
296+
{first: 4, second: 5},
297+
{third: 7, fourth: 8},
298+
{fifth: 9},
299+
],
300+
// $FlowExpectedError[extra-arg]
301+
['first', 'fifth'],
302+
);
303+
expect(logFn).toHaveBeenCalledTimes(1);
304+
expect(logFn.mock.lastCall).toEqual([
305+
`
306+
| (index) | first | fifth |
307+
| ------- | ----- | ----- |
308+
| 0 | 1 | |
309+
| 1 | 4 | |
310+
| 2 | | |
311+
| 3 | | 9 |`,
312+
LOG_LEVELS.info,
313+
]);
314+
} finally {
315+
global.nativeLoggingHook = originalNativeLoggingHook;
316+
}
317+
});
318+
319+
it('should only print the selected columns, if specified (dictionaries)', () => {
320+
const originalNativeLoggingHook = global.nativeLoggingHook;
321+
const logFn = (global.nativeLoggingHook = jest.fn());
322+
323+
try {
324+
console.table(
325+
{
326+
a: {first: 1, second: 2, third: 3},
327+
b: {first: 4, second: 5},
328+
c: {third: 7, fourth: 8},
329+
d: {fifth: 9},
330+
},
331+
// $FlowExpectedError[extra-arg]
332+
['first', 'fifth'],
333+
);
334+
expect(logFn).toHaveBeenCalledTimes(1);
335+
expect(logFn.mock.lastCall).toEqual([
336+
`
337+
| (index) | first | fifth |
338+
| ------- | ----- | ----- |
339+
| a | 1 | |
340+
| b | 4 | |
341+
| c | | |
342+
| d | | 9 |`,
343+
LOG_LEVELS.info,
344+
]);
345+
} finally {
346+
global.nativeLoggingHook = originalNativeLoggingHook;
347+
}
348+
});
287349
});
288350
});

packages/polyfills/console.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,18 @@ function formatCellValue(cell, key) {
455455
return '';
456456
}
457457

458-
function consoleTablePolyfill(rows) {
458+
function consoleTablePolyfill(data, columns) {
459+
var rows;
460+
459461
// convert object -> array
460-
if (Array.isArray(rows)) {
461-
rows = rows.map((row, index) => {
462+
if (Array.isArray(data)) {
463+
rows = data.map((row, index) => {
462464
var processedRow = {};
463465
processedRow[INDEX_COLUMN_NAME] = String(index);
464466
Object.assign(processedRow, row);
465467
return processedRow;
466468
});
467469
} else {
468-
var data = rows;
469470
rows = [];
470471
for (var key in data) {
471472
if (data.hasOwnProperty(key)) {
@@ -481,12 +482,16 @@ function consoleTablePolyfill(rows) {
481482
return;
482483
}
483484

484-
var columns = Array.from(
485-
rows.reduce((columnSet, row) => {
486-
Object.keys(row).forEach(key => columnSet.add(key));
487-
return columnSet;
488-
}, new Set()),
489-
);
485+
if (Array.isArray(columns)) {
486+
columns = [INDEX_COLUMN_NAME].concat(columns);
487+
} else {
488+
columns = Array.from(
489+
rows.reduce((columnSet, row) => {
490+
Object.keys(row).forEach(key => columnSet.add(key));
491+
return columnSet;
492+
}, new Set()),
493+
);
494+
}
490495
var stringRows = [];
491496
var columnWidths = [];
492497

0 commit comments

Comments
 (0)