lib: refactor to use primordials in lib/internal/cli_table
PR-URL: https://github.com/nodejs/node/pull/38046 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
66c8f76c2c
commit
a9332e84bf
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user