1- import Utils from "../Utils.js" ;
2-
31/**
42 * ToTable operations.
53 *
64 * @author Mark Jones [github.com/justanothermark]
75 * @namespace
86 */
9- const ToTable = {
7+ const ToTable = {
108 /**
119 * @constant
1210 * @default
1311 */
1412 SEPARATORS : [
15- { name : "Comma" , value :"," } ,
16- { name : "Tab" , value : escape ( "\t" ) } ,
13+ { name : "Comma" , value : "," } ,
14+ { name : "Tab" , value : "\\t" } ,
1715 { name : "Pipe" , value : "|" } ,
1816 { name : "Custom" , value : "" }
1917 ] ,
@@ -23,8 +21,8 @@ import Utils from "../Utils.js";
2321 * @default
2422 */
2523 FORMATS : [
26- ' ASCII' ,
27- ' HTML'
24+ " ASCII" ,
25+ " HTML"
2826 ] ,
2927
3028 /**
@@ -41,23 +39,22 @@ import Utils from "../Utils.js";
4139 let tableData = [ ] ;
4240
4341 // If the separator contains any tabs, convert them to tab characters.
44- separator = separator . replace ( ' \\t' , '\t' ) ;
42+ separator = separator . replace ( " \\t" , "\t" ) ;
4543
4644 // Process the input into a nested array of elements.
47- let rows = input . split ( '\n' ) ;
45+ let rows = input . split ( "\n" ) ;
4846 rows . forEach ( function ( element ) {
49- if ( separator == '' ) {
47+ if ( separator === "" ) {
5048 tableData . push ( [ element ] ) ;
51- }
52- else {
49+ } else {
5350 tableData . push ( element . split ( separator ) ) ;
5451 }
5552 } ) ;
5653
5754 // Render the data in the requested format.
58- let output = '' ;
55+ let output = "" ;
5956 switch ( format ) {
60- case ' ASCII' :
57+ case " ASCII" :
6158 output = asciiOutput ( tableData ) ;
6259 break ;
6360
@@ -75,31 +72,22 @@ import Utils from "../Utils.js";
7572 * @returns {string }
7673 */
7774 function asciiOutput ( tableData ) {
78- const horizontalBorder = '-' ;
79- const verticalBorder = '|' ;
80- const crossBorder = '+' ;
75+ const horizontalBorder = "-" ;
76+ const verticalBorder = "|" ;
77+ const crossBorder = "+" ;
8178
82- let output = '' ;
79+ let output = "" ;
8380 let longestCells = [ ] ;
8481
8582 // Find longestCells value per column to pad cells equally.
8683 tableData . forEach ( function ( row , index ) {
8784 row . forEach ( function ( cell , cellIndex ) {
88- if ( longestCells [ cellIndex ] == undefined || cell . length > longestCells [ cellIndex ] ) {
85+ if ( longestCells [ cellIndex ] === undefined || cell . length > longestCells [ cellIndex ] ) {
8986 longestCells [ cellIndex ] = cell . length ;
9087 }
9188 } ) ;
9289 } ) ;
9390
94- // Calculate the complete row length. This is the length of the
95- // longest cell for each column plus 3 characters per cell
96- // (1 padding each side of the value and 1 for the cell border)
97- // plus 1 for the final cell border.
98- let rowLength = ( longestCells . length * 3 ) + 1 ;
99- longestCells . forEach ( function ( celllongestCells ) {
100- rowLength += celllongestCells ;
101- } ) ;
102-
10391 // Add the top border of the table to the output.
10492 output += outputHorizontalBorder ( longestCells ) ;
10593
@@ -127,9 +115,9 @@ import Utils from "../Utils.js";
127115 function outputRow ( row , longestCells ) {
128116 let rowOutput = verticalBorder ;
129117 row . forEach ( function ( cell , index ) {
130- rowOutput += ' ' + cell + ' ' . repeat ( longestCells [ index ] - cell . length ) + ' ' + verticalBorder ;
118+ rowOutput += " " + cell + " " . repeat ( longestCells [ index ] - cell . length ) + " " + verticalBorder ;
131119 } ) ;
132- rowOutput += '\n' ;
120+ rowOutput += "\n" ;
133121 return rowOutput ;
134122 }
135123
@@ -142,7 +130,7 @@ import Utils from "../Utils.js";
142130 longestCells . forEach ( function ( cellLength ) {
143131 rowOutput += horizontalBorder . repeat ( cellLength + 2 ) + crossBorder ;
144132 } ) ;
145- rowOutput += '\n' ;
133+ rowOutput += "\n" ;
146134 return rowOutput ;
147135 }
148136 }
@@ -158,20 +146,26 @@ import Utils from "../Utils.js";
158146 if ( firstRowHeader ) {
159147 let row = tableData . shift ( ) ;
160148 output += "<thead>" ;
161- output += outputRow ( row , 'th' ) ;
149+ output += outputRow ( row , "th" ) ;
162150 output += "</thead>" ;
163151 }
164152
165153 // Output the rest of the rows in the <tbody>.
166154 output += "<tbody>" ;
167155 tableData . forEach ( function ( row , index ) {
168- output += outputRow ( row , 'td' ) ;
156+ output += outputRow ( row , "td" ) ;
169157 } ) ;
170158
171159 // Close the body and table elements.
172160 output += "</tbody></table>" ;
173161 return output ;
174162
163+ /**
164+ * Outputs a table row.
165+ *
166+ * @param {string[] } row
167+ * @param {string } cellType
168+ */
175169 function outputRow ( row , cellType ) {
176170 let output = "<tr>" ;
177171 row . forEach ( function ( cell ) {
@@ -181,9 +175,7 @@ import Utils from "../Utils.js";
181175 return output ;
182176 }
183177 }
184-
185- return output ;
186178 }
187179} ;
188180
189- export default ToTable ;
181+ export default ToTable ;
0 commit comments