@@ -4,44 +4,20 @@ var repeat = require('repeat-string')
4
4
5
5
module . exports = markdownTable
6
6
7
- var trailingWhitespace = / + $ /
8
-
9
- // Characters.
10
- var space = ' '
11
- var lineFeed = '\n'
12
- var dash = '-'
13
- var colon = ':'
14
- var verticalBar = '|'
15
-
16
- var x = 0
17
- var C = 67
18
- var L = 76
19
- var R = 82
20
- var c = 99
21
- var l = 108
22
- var r = 114
23
-
24
7
// Create a table from a matrix of strings.
25
8
function markdownTable ( table , options ) {
26
9
var settings = options || { }
27
- var padding = settings . padding !== false
28
- var start = settings . delimiterStart !== false
29
- var end = settings . delimiterEnd !== false
30
10
var align = ( settings . align || [ ] ) . concat ( )
31
- var alignDelimiters = settings . alignDelimiters !== false
32
- var alignments = [ ]
33
11
var stringLength = settings . stringLength || defaultStringLength
12
+ var alignments = [ ]
34
13
var rowIndex = - 1
35
- var rowLength = table . length
36
14
var cellMatrix = [ ]
37
15
var sizeMatrix = [ ]
38
- var row = [ ]
39
- var sizes = [ ]
40
16
var longestCellByColumn = [ ]
41
17
var mostCellsPerRow = 0
42
- var cells
43
18
var columnIndex
44
- var columnLength
19
+ var row
20
+ var sizes
45
21
var largest
46
22
var size
47
23
var cell
@@ -53,21 +29,19 @@ function markdownTable(table, options) {
53
29
54
30
// This is a superfluous loop if we don’t align delimiters, but otherwise we’d
55
31
// do superfluous work when aligning, so optimize for aligning.
56
- while ( ++ rowIndex < rowLength ) {
57
- cells = table [ rowIndex ]
32
+ while ( ++ rowIndex < table . length ) {
58
33
columnIndex = - 1
59
- columnLength = cells . length
60
34
row = [ ]
61
35
sizes = [ ]
62
36
63
- if ( columnLength > mostCellsPerRow ) {
64
- mostCellsPerRow = columnLength
37
+ if ( table [ rowIndex ] . length > mostCellsPerRow ) {
38
+ mostCellsPerRow = table [ rowIndex ] . length
65
39
}
66
40
67
- while ( ++ columnIndex < columnLength ) {
68
- cell = serialize ( cells [ columnIndex ] )
41
+ while ( ++ columnIndex < table [ rowIndex ] . length ) {
42
+ cell = serialize ( table [ rowIndex ] [ columnIndex ] )
69
43
70
- if ( alignDelimiters === true ) {
44
+ if ( settings . alignDelimiters !== false ) {
71
45
size = stringLength ( cell )
72
46
sizes [ columnIndex ] = size
73
47
@@ -87,51 +61,50 @@ function markdownTable(table, options) {
87
61
88
62
// Figure out which alignments to use.
89
63
columnIndex = - 1
90
- columnLength = mostCellsPerRow
91
64
92
65
if ( typeof align === 'object' && 'length' in align ) {
93
- while ( ++ columnIndex < columnLength ) {
66
+ while ( ++ columnIndex < mostCellsPerRow ) {
94
67
alignments [ columnIndex ] = toAlignment ( align [ columnIndex ] )
95
68
}
96
69
} else {
97
70
code = toAlignment ( align )
98
71
99
- while ( ++ columnIndex < columnLength ) {
72
+ while ( ++ columnIndex < mostCellsPerRow ) {
100
73
alignments [ columnIndex ] = code
101
74
}
102
75
}
103
76
104
77
// Inject the alignment row.
105
78
columnIndex = - 1
106
- columnLength = mostCellsPerRow
107
79
row = [ ]
108
80
sizes = [ ]
109
81
110
- while ( ++ columnIndex < columnLength ) {
82
+ while ( ++ columnIndex < mostCellsPerRow ) {
111
83
code = alignments [ columnIndex ]
112
84
before = ''
113
85
after = ''
114
86
115
- if ( code === l ) {
116
- before = colon
117
- } else if ( code === r ) {
118
- after = colon
119
- } else if ( code === c ) {
120
- before = colon
121
- after = colon
87
+ if ( code === 99 /* `c` */ ) {
88
+ before = ':'
89
+ after = ':'
90
+ } else if ( code === 108 /* `l` */ ) {
91
+ before = ':'
92
+ } else if ( code === 114 /* `r` */ ) {
93
+ after = ':'
122
94
}
123
95
124
96
// There *must* be at least one hyphen-minus in each alignment cell.
125
- size = alignDelimiters
126
- ? Math . max (
127
- 1 ,
128
- longestCellByColumn [ columnIndex ] - before . length - after . length
129
- )
130
- : 1
97
+ size =
98
+ settings . alignDelimiters === false
99
+ ? 1
100
+ : Math . max (
101
+ 1 ,
102
+ longestCellByColumn [ columnIndex ] - before . length - after . length
103
+ )
131
104
132
- cell = before + repeat ( dash , size ) + after
105
+ cell = before + repeat ( '-' , size ) + after
133
106
134
- if ( alignDelimiters === true ) {
107
+ if ( settings . alignDelimiters !== false ) {
135
108
size = before . length + size + after . length
136
109
137
110
if ( size > longestCellByColumn [ columnIndex ] ) {
@@ -149,83 +122,84 @@ function markdownTable(table, options) {
149
122
sizeMatrix . splice ( 1 , 0 , sizes )
150
123
151
124
rowIndex = - 1
152
- rowLength = cellMatrix . length
153
125
lines = [ ]
154
126
155
- while ( ++ rowIndex < rowLength ) {
127
+ while ( ++ rowIndex < cellMatrix . length ) {
156
128
row = cellMatrix [ rowIndex ]
157
129
sizes = sizeMatrix [ rowIndex ]
158
130
columnIndex = - 1
159
- columnLength = mostCellsPerRow
160
131
line = [ ]
161
132
162
- while ( ++ columnIndex < columnLength ) {
133
+ while ( ++ columnIndex < mostCellsPerRow ) {
163
134
cell = row [ columnIndex ] || ''
164
135
before = ''
165
136
after = ''
166
137
167
- if ( alignDelimiters === true ) {
138
+ if ( settings . alignDelimiters !== false ) {
168
139
size = longestCellByColumn [ columnIndex ] - ( sizes [ columnIndex ] || 0 )
169
140
code = alignments [ columnIndex ]
170
141
171
- if ( code === r ) {
172
- before = repeat ( space , size )
173
- } else if ( code === c ) {
174
- if ( size % 2 === 0 ) {
175
- before = repeat ( space , size / 2 )
176
- after = before
142
+ if ( code === 114 /* `r` */ ) {
143
+ before = repeat ( ' ' , size )
144
+ } else if ( code === 99 /* `c` */ ) {
145
+ if ( size % 2 ) {
146
+ before = repeat ( ' ' , size / 2 + 0.5 )
147
+ after = repeat ( ' ' , size / 2 - 0.5 )
177
148
} else {
178
- before = repeat ( space , size / 2 + 0.5 )
179
- after = repeat ( space , size / 2 - 0.5 )
149
+ before = repeat ( ' ' , size / 2 )
150
+ after = before
180
151
}
181
152
} else {
182
- after = repeat ( space , size )
153
+ after = repeat ( ' ' , size )
183
154
}
184
155
}
185
156
186
- if ( start === true && columnIndex === 0 ) {
187
- line . push ( verticalBar )
157
+ if ( settings . delimiterStart !== false && ! columnIndex ) {
158
+ line . push ( '|' )
188
159
}
189
160
190
161
if (
191
- padding === true &&
162
+ settings . padding !== false &&
192
163
// Don’t add the opening space if we’re not aligning and the cell is
193
164
// empty: there will be a closing space.
194
- ! ( alignDelimiters === false && cell === '' ) &&
195
- ( start === true || columnIndex !== 0 )
165
+ ! ( settings . alignDelimiters === false && cell === '' ) &&
166
+ ( settings . delimiterStart !== false || columnIndex )
196
167
) {
197
- line . push ( space )
168
+ line . push ( ' ' )
198
169
}
199
170
200
- if ( alignDelimiters === true ) {
171
+ if ( settings . alignDelimiters !== false ) {
201
172
line . push ( before )
202
173
}
203
174
204
175
line . push ( cell )
205
176
206
- if ( alignDelimiters === true ) {
177
+ if ( settings . alignDelimiters !== false ) {
207
178
line . push ( after )
208
179
}
209
180
210
- if ( padding === true ) {
211
- line . push ( space )
181
+ if ( settings . padding !== false ) {
182
+ line . push ( ' ' )
212
183
}
213
184
214
- if ( end === true || columnIndex !== columnLength - 1 ) {
215
- line . push ( verticalBar )
185
+ if (
186
+ settings . delimiterEnd !== false ||
187
+ columnIndex !== mostCellsPerRow - 1
188
+ ) {
189
+ line . push ( '|' )
216
190
}
217
191
}
218
192
219
193
line = line . join ( '' )
220
194
221
- if ( end === false ) {
222
- line = line . replace ( trailingWhitespace , '' )
195
+ if ( settings . delimiterEnd === false ) {
196
+ line = line . replace ( / + $ / , '' )
223
197
}
224
198
225
199
lines . push ( line )
226
200
}
227
201
228
- return lines . join ( lineFeed )
202
+ return lines . join ( '\n' )
229
203
}
230
204
231
205
function serialize ( value ) {
@@ -237,13 +211,13 @@ function defaultStringLength(value) {
237
211
}
238
212
239
213
function toAlignment ( value ) {
240
- var code = typeof value === 'string' ? value . charCodeAt ( 0 ) : x
241
-
242
- return code === L || code === l
243
- ? l
244
- : code === R || code === r
245
- ? r
246
- : code === C || code === c
247
- ? c
248
- : x
214
+ var code = typeof value === 'string' ? value . charCodeAt ( 0 ) : 0
215
+
216
+ return code === 67 /* `C` */ || code === 99 /* `c` */
217
+ ? 99 /* `c` */
218
+ : code === 76 /* `L` */ || code === 108 /* `l` */
219
+ ? 108 /* `l` */
220
+ : code === 82 /* `R` */ || code === 114 /* `r` */
221
+ ? 114 /* `r` */
222
+ : 0
249
223
}
0 commit comments