1
1
/**
2
- * @typedef MarkdownTableOptions
3
- * @property {string|null|Array. <string|null|undefined> } [align]
2
+ * @typedef Options
3
+ * @property {string|null|Array<string|null|undefined> } [align]
4
4
* @property {boolean } [padding=true]
5
5
* @property {boolean } [delimiterStart=true]
6
6
* @property {boolean } [delimiterStart=true]
9
9
* @property {(value: string) => number } [stringLength]
10
10
*/
11
11
12
+ /**
13
+ * @typedef {Options } MarkdownTableOptions
14
+ * @todo
15
+ * Remove next major.
16
+ */
17
+
12
18
/**
13
19
* Create a table from a matrix of strings.
14
20
*
15
- * @param {Array. <Array. <string|null|undefined>> } table
16
- * @param {MarkdownTableOptions } [options]
21
+ * @param {Array<Array<string|null|undefined>> } table
22
+ * @param {Options } [options]
17
23
* @returns {string }
18
24
*/
19
- export function markdownTable ( table , options ) {
20
- const settings = options || { }
21
- const align = ( settings . align || [ ] ) . concat ( )
22
- const stringLength = settings . stringLength || defaultStringLength
23
- /** @type {number[] } Character codes as symbols for alignment per column. */
25
+ export function markdownTable ( table , options = { } ) {
26
+ const align = ( options . align || [ ] ) . concat ( )
27
+ const stringLength = options . stringLength || defaultStringLength
28
+ /** @type {Array<number> } Character codes as symbols for alignment per column. */
24
29
const alignments = [ ]
25
- let rowIndex = - 1
26
- /** @type {string[][] } Cells per row. */
30
+ /** @type {Array<Array<string>> } Cells per row. */
27
31
const cellMatrix = [ ]
28
- /** @type {number[][] } Sizes of each cell per row. */
32
+ /** @type {Array<Array< number>> } Sizes of each cell per row. */
29
33
const sizeMatrix = [ ]
30
- /** @type {number[] } */
34
+ /** @type {Array< number> } */
31
35
const longestCellByColumn = [ ]
32
36
let mostCellsPerRow = 0
33
- /** @type {number } */
34
- let columnIndex
35
- /** @type {string[] } Cells of current row */
36
- let row
37
- /** @type {number[] } Sizes of current row */
38
- let sizes
39
- /** @type {number } Sizes of current cell */
40
- let size
41
- /** @type {string } Current cell */
42
- let cell
43
- /** @type {string[] } Chunks of current line. */
44
- let line
45
- /** @type {string } */
46
- let before
47
- /** @type {string } */
48
- let after
49
- /** @type {number } */
50
- let code
37
+ let rowIndex = - 1
51
38
52
39
// This is a superfluous loop if we don’t align delimiters, but otherwise we’d
53
40
// do superfluous work when aligning, so optimize for aligning.
54
41
while ( ++ rowIndex < table . length ) {
55
- columnIndex = - 1
56
- row = [ ]
57
- sizes = [ ]
42
+ /** @type {Array<string> } */
43
+ const row = [ ]
44
+ /** @type {Array<number> } */
45
+ const sizes = [ ]
46
+ let columnIndex = - 1
58
47
59
48
if ( table [ rowIndex ] . length > mostCellsPerRow ) {
60
49
mostCellsPerRow = table [ rowIndex ] . length
61
50
}
62
51
63
52
while ( ++ columnIndex < table [ rowIndex ] . length ) {
64
- cell = serialize ( table [ rowIndex ] [ columnIndex ] )
53
+ const cell = serialize ( table [ rowIndex ] [ columnIndex ] )
65
54
66
- if ( settings . alignDelimiters !== false ) {
67
- size = stringLength ( cell )
55
+ if ( options . alignDelimiters !== false ) {
56
+ const size = stringLength ( cell )
68
57
sizes [ columnIndex ] = size
69
58
70
59
if (
@@ -83,14 +72,14 @@ export function markdownTable(table, options) {
83
72
}
84
73
85
74
// Figure out which alignments to use.
86
- columnIndex = - 1
75
+ let columnIndex = - 1
87
76
88
77
if ( typeof align === 'object' && 'length' in align ) {
89
78
while ( ++ columnIndex < mostCellsPerRow ) {
90
79
alignments [ columnIndex ] = toAlignment ( align [ columnIndex ] )
91
80
}
92
81
} else {
93
- code = toAlignment ( align )
82
+ const code = toAlignment ( align )
94
83
95
84
while ( ++ columnIndex < mostCellsPerRow ) {
96
85
alignments [ columnIndex ] = code
@@ -99,13 +88,15 @@ export function markdownTable(table, options) {
99
88
100
89
// Inject the alignment row.
101
90
columnIndex = - 1
102
- row = [ ]
103
- sizes = [ ]
91
+ /** @type {Array<string> } */
92
+ const row = [ ]
93
+ /** @type {Array<number> } */
94
+ const sizes = [ ]
104
95
105
96
while ( ++ columnIndex < mostCellsPerRow ) {
106
- code = alignments [ columnIndex ]
107
- before = ''
108
- after = ''
97
+ const code = alignments [ columnIndex ]
98
+ let before = ''
99
+ let after = ''
109
100
110
101
if ( code === 99 /* `c` */ ) {
111
102
before = ':'
@@ -117,17 +108,17 @@ export function markdownTable(table, options) {
117
108
}
118
109
119
110
// There *must* be at least one hyphen-minus in each alignment cell.
120
- size =
121
- settings . alignDelimiters === false
111
+ let size =
112
+ options . alignDelimiters === false
122
113
? 1
123
114
: Math . max (
124
115
1 ,
125
116
longestCellByColumn [ columnIndex ] - before . length - after . length
126
117
)
127
118
128
- cell = before + '-' . repeat ( size ) + after
119
+ const cell = before + '-' . repeat ( size ) + after
129
120
130
- if ( settings . alignDelimiters !== false ) {
121
+ if ( options . alignDelimiters !== false ) {
131
122
size = before . length + size + after . length
132
123
133
124
if ( size > longestCellByColumn [ columnIndex ] ) {
@@ -145,23 +136,25 @@ export function markdownTable(table, options) {
145
136
sizeMatrix . splice ( 1 , 0 , sizes )
146
137
147
138
rowIndex = - 1
148
- /** @type {string[] } */
139
+ /** @type {Array< string> } */
149
140
const lines = [ ]
150
141
151
142
while ( ++ rowIndex < cellMatrix . length ) {
152
- row = cellMatrix [ rowIndex ]
153
- sizes = sizeMatrix [ rowIndex ]
143
+ const row = cellMatrix [ rowIndex ]
144
+ const sizes = sizeMatrix [ rowIndex ]
154
145
columnIndex = - 1
155
- line = [ ]
146
+ /** @type {Array<string> } */
147
+ const line = [ ]
156
148
157
149
while ( ++ columnIndex < mostCellsPerRow ) {
158
- cell = row [ columnIndex ] || ''
159
- before = ''
160
- after = ''
150
+ const cell = row [ columnIndex ] || ''
151
+ let before = ''
152
+ let after = ''
161
153
162
- if ( settings . alignDelimiters !== false ) {
163
- size = longestCellByColumn [ columnIndex ] - ( sizes [ columnIndex ] || 0 )
164
- code = alignments [ columnIndex ]
154
+ if ( options . alignDelimiters !== false ) {
155
+ const size =
156
+ longestCellByColumn [ columnIndex ] - ( sizes [ columnIndex ] || 0 )
157
+ const code = alignments [ columnIndex ]
165
158
166
159
if ( code === 114 /* `r` */ ) {
167
160
before = ' ' . repeat ( size )
@@ -178,44 +171,44 @@ export function markdownTable(table, options) {
178
171
}
179
172
}
180
173
181
- if ( settings . delimiterStart !== false && ! columnIndex ) {
174
+ if ( options . delimiterStart !== false && ! columnIndex ) {
182
175
line . push ( '|' )
183
176
}
184
177
185
178
if (
186
- settings . padding !== false &&
179
+ options . padding !== false &&
187
180
// Don’t add the opening space if we’re not aligning and the cell is
188
181
// empty: there will be a closing space.
189
- ! ( settings . alignDelimiters === false && cell === '' ) &&
190
- ( settings . delimiterStart !== false || columnIndex )
182
+ ! ( options . alignDelimiters === false && cell === '' ) &&
183
+ ( options . delimiterStart !== false || columnIndex )
191
184
) {
192
185
line . push ( ' ' )
193
186
}
194
187
195
- if ( settings . alignDelimiters !== false ) {
188
+ if ( options . alignDelimiters !== false ) {
196
189
line . push ( before )
197
190
}
198
191
199
192
line . push ( cell )
200
193
201
- if ( settings . alignDelimiters !== false ) {
194
+ if ( options . alignDelimiters !== false ) {
202
195
line . push ( after )
203
196
}
204
197
205
- if ( settings . padding !== false ) {
198
+ if ( options . padding !== false ) {
206
199
line . push ( ' ' )
207
200
}
208
201
209
202
if (
210
- settings . delimiterEnd !== false ||
203
+ options . delimiterEnd !== false ||
211
204
columnIndex !== mostCellsPerRow - 1
212
205
) {
213
206
line . push ( '|' )
214
207
}
215
208
}
216
209
217
210
lines . push (
218
- settings . delimiterEnd === false
211
+ options . delimiterEnd === false
219
212
? line . join ( '' ) . replace ( / + $ / , '' )
220
213
: line . join ( '' )
221
214
)
0 commit comments