Skip to content

Commit a9332e8

Browse files
marsonyaTrott
authored andcommitted
lib: refactor to use primordials in lib/internal/cli_table
PR-URL: #38046 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 66c8f76 commit a9332e8

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

‎lib/internal/cli_table.js‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
'use strict';
22

33
const{
4+
ArrayPrototypeJoin,
5+
ArrayPrototypeMap,
46
MathCeil,
57
MathMax,
8+
MathMaxApply,
69
ObjectPrototypeHasOwnProperty,
10+
StringPrototypeRepeat,
711
}=primordials;
812

913
const{ getStringWidth }=require('internal/util/inspect');
@@ -39,7 +43,8 @@ const renderRow = (row, columnWidths) =>{
3943
constneeded=(columnWidths[i]-len)/2;
4044
// round(needed) + ceil(needed) will always add up to the amount
4145
// of spaces we need while also left justifying the output.
42-
out+=`${' '.repeat(needed)}${cell}${' '.repeat(MathCeil(needed))}`;
46+
out+=StringPrototypeRepeat(' ',needed)+cell+
47+
StringPrototypeRepeat(' ',MathCeil(needed));
4348
if(i!==row.length-1)
4449
out+=tableChars.middle;
4550
}
@@ -49,8 +54,9 @@ const renderRow = (row, columnWidths) =>{
4954

5055
consttable=(head,columns)=>{
5156
constrows=[];
52-
constcolumnWidths=head.map((h)=>getStringWidth(h));
53-
constlongestColumn=columns.reduce((n,a)=>MathMax(n,a.length),0);
57+
constcolumnWidths=ArrayPrototypeMap(head,(h)=>getStringWidth(h));
58+
constlongestColumn=MathMaxApply(ArrayPrototypeMap(columns,(a)=>
59+
a.length));
5460

5561
for(leti=0;i<head.length;i++){
5662
constcolumn=columns[i];
@@ -65,18 +71,22 @@ const table = (head, columns) =>{
6571
}
6672
}
6773

68-
constdivider=columnWidths.map((i)=>
69-
tableChars.middleMiddle.repeat(i+2));
74+
constdivider=ArrayPrototypeMap(columnWidths,(i)=>
75+
StringPrototypeRepeat(tableChars.middleMiddle,i+2));
7076

71-
letresult=`${tableChars.topLeft}${divider.join(tableChars.topMiddle)}`+
72-
`${tableChars.topRight}\n${renderRow(head,columnWidths)}\n`+
73-
`${tableChars.leftMiddle}${divider.join(tableChars.rowMiddle)}`+
74-
`${tableChars.rightMiddle}\n`;
77+
letresult=tableChars.topLeft+
78+
ArrayPrototypeJoin(divider,tableChars.topMiddle)+
79+
tableChars.topRight+'\n'+
80+
renderRow(head,columnWidths)+'\n'+
81+
tableChars.leftMiddle+
82+
ArrayPrototypeJoin(divider,tableChars.rowMiddle)+
83+
tableChars.rightMiddle+'\n';
7584

7685
for(constrowofrows)
7786
result+=`${renderRow(row,columnWidths)}\n`;
7887

79-
result+=`${tableChars.bottomLeft}${divider.join(tableChars.bottomMiddle)}`+
88+
result+=tableChars.bottomLeft+
89+
ArrayPrototypeJoin(divider,tableChars.bottomMiddle)+
8090
tableChars.bottomRight;
8191

8292
returnresult;

0 commit comments

Comments
(0)