diff --git a/lib/internal/cli_table.js b/lib/internal/cli_table.js index 2a5f0acd471909..79b15a38f49bb5 100644 --- a/lib/internal/cli_table.js +++ b/lib/internal/cli_table.js @@ -1,9 +1,13 @@ 'use strict'; const { + ArrayPrototypeJoin, + ArrayPrototypeMap, MathCeil, MathMax, + MathMaxApply, ObjectPrototypeHasOwnProperty, + StringPrototypeRepeat, } = primordials; const { getStringWidth } = require('internal/util/inspect'); @@ -39,7 +43,8 @@ const renderRow = (row, columnWidths) => { const needed = (columnWidths[i] - len) / 2; // round(needed) + ceil(needed) will always add up to the amount // of spaces we need while also left justifying the output. - out += `${' '.repeat(needed)}${cell}${' '.repeat(MathCeil(needed))}`; + out += StringPrototypeRepeat(' ', needed) + cell + + StringPrototypeRepeat(' ', MathCeil(needed)); if (i !== row.length - 1) out += tableChars.middle; } @@ -49,8 +54,9 @@ const renderRow = (row, columnWidths) => { const table = (head, columns) => { const rows = []; - const columnWidths = head.map((h) => getStringWidth(h)); - const longestColumn = columns.reduce((n, a) => MathMax(n, a.length), 0); + const columnWidths = ArrayPrototypeMap(head, (h) => getStringWidth(h)); + const longestColumn = MathMaxApply(ArrayPrototypeMap(columns, (a) => + a.length)); for (let i = 0; i < head.length; i++) { const column = columns[i]; @@ -65,18 +71,22 @@ const table = (head, columns) => { } } - const divider = columnWidths.map((i) => - tableChars.middleMiddle.repeat(i + 2)); + const divider = ArrayPrototypeMap(columnWidths, (i) => + StringPrototypeRepeat(tableChars.middleMiddle, i + 2)); - let result = `${tableChars.topLeft}${divider.join(tableChars.topMiddle)}` + - `${tableChars.topRight}\n${renderRow(head, columnWidths)}\n` + - `${tableChars.leftMiddle}${divider.join(tableChars.rowMiddle)}` + - `${tableChars.rightMiddle}\n`; + let result = tableChars.topLeft + + ArrayPrototypeJoin(divider, tableChars.topMiddle) + + tableChars.topRight + '\n' + + renderRow(head, columnWidths) + '\n' + + tableChars.leftMiddle + + ArrayPrototypeJoin(divider, tableChars.rowMiddle) + + tableChars.rightMiddle + '\n'; for (const row of rows) result += `${renderRow(row, columnWidths)}\n`; - result += `${tableChars.bottomLeft}${divider.join(tableChars.bottomMiddle)}` + + result += tableChars.bottomLeft + + ArrayPrototypeJoin(divider, tableChars.bottomMiddle) + tableChars.bottomRight; return result;