Skip to content

Commit

Permalink
Add support for the columns option in console.table
Browse files Browse the repository at this point in the history
Summary: Changelog: [General][Added] Add support for the second parameter of `console.table` to specify a list of columns to print in the table.

Differential Revision: D67803665
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 10, 2025
1 parent 00aa755 commit e680d52
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
62 changes: 62 additions & 0 deletions packages/polyfills/__tests__/console-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,67 @@ describe('console', () => {
global.nativeLoggingHook = originalNativeLoggingHook;
}
});

it('should only print the selected columns, if specified (arrays)', () => {
const originalNativeLoggingHook = global.nativeLoggingHook;
const logFn = (global.nativeLoggingHook = jest.fn());

try {
console.table(
[
{first: 1, second: 2, third: 3},
{first: 4, second: 5},
{third: 7, fourth: 8},
{fifth: 9},
],
// $FlowExpectedError[extra-arg]
['first', 'fifth'],
);
expect(logFn).toHaveBeenCalledTimes(1);
expect(logFn.mock.lastCall).toEqual([
`
| (index) | first | fifth |
| ------- | ----- | ----- |
| 0 | 1 | |
| 1 | 4 | |
| 2 | | |
| 3 | | 9 |`,
LOG_LEVELS.info,
]);
} finally {
global.nativeLoggingHook = originalNativeLoggingHook;
}
});

it('should only print the selected columns, if specified (dictionaries)', () => {
const originalNativeLoggingHook = global.nativeLoggingHook;
const logFn = (global.nativeLoggingHook = jest.fn());

try {
console.table(
{
a: {first: 1, second: 2, third: 3},
b: {first: 4, second: 5},
c: {third: 7, fourth: 8},
d: {fifth: 9},
},
// $FlowExpectedError[extra-arg]
['first', 'fifth'],
);
expect(logFn).toHaveBeenCalledTimes(1);
expect(logFn.mock.lastCall).toEqual([
`
| (index) | first | fifth |
| ------- | ----- | ----- |
| a | 1 | |
| b | 4 | |
| c | | |
| d | | 9 |`,
LOG_LEVELS.info,
]);
} finally {
global.nativeLoggingHook = originalNativeLoggingHook;
}
});
});
});
25 changes: 15 additions & 10 deletions packages/polyfills/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,18 @@ function formatCellValue(cell, key) {
return '';
}

function consoleTablePolyfill(rows) {
function consoleTablePolyfill(data, columns) {
var rows;

// convert object -> array
if (Array.isArray(rows)) {
rows = rows.map((row, index) => {
if (Array.isArray(data)) {
rows = data.map((row, index) => {
var processedRow = {};
processedRow[INDEX_COLUMN_NAME] = String(index);
Object.assign(processedRow, row);
return processedRow;
});
} else {
var data = rows;
rows = [];
for (var key in data) {
if (data.hasOwnProperty(key)) {
Expand All @@ -481,12 +482,16 @@ function consoleTablePolyfill(rows) {
return;
}

var columns = Array.from(
rows.reduce((columnSet, row) => {
Object.keys(row).forEach(key => columnSet.add(key));
return columnSet;
}, new Set()),
);
if (Array.isArray(columns)) {
columns = [INDEX_COLUMN_NAME].concat(columns);
} else {
columns = Array.from(
rows.reduce((columnSet, row) => {
Object.keys(row).forEach(key => columnSet.add(key));
return columnSet;
}, new Set()),
);
}
var stringRows = [];
var columnWidths = [];

Expand Down

0 comments on commit e680d52

Please sign in to comment.