From fd0894b1c7fcb20dd213ec1e93aafef25935d709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Fri, 10 Jan 2025 05:41:37 -0800 Subject: [PATCH] Add support for the columns option in console.table (#48592) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- packages/polyfills/__tests__/console-itest.js | 62 +++++++++++++++++++ packages/polyfills/console.js | 25 +++++--- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/packages/polyfills/__tests__/console-itest.js b/packages/polyfills/__tests__/console-itest.js index a02e8fe520e568..a58937f6a156d0 100644 --- a/packages/polyfills/__tests__/console-itest.js +++ b/packages/polyfills/__tests__/console-itest.js @@ -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; + } + }); }); }); diff --git a/packages/polyfills/console.js b/packages/polyfills/console.js index f397228ee92bd2..4f0b9cad52e24b 100644 --- a/packages/polyfills/console.js +++ b/packages/polyfills/console.js @@ -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)) { @@ -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 = [];