Skip to content

Commit d1cd6fa

Browse files
committed
Refactor
1 parent 5818440 commit d1cd6fa

File tree

3 files changed

+66
-73
lines changed

3 files changed

+66
-73
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.DS_Store
2-
*.d.ts
3-
*.log
41
coverage/
52
node_modules/
3+
*.d.ts
4+
*.log
5+
.DS_Store
66
yarn.lock

index.js

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @typedef MarkdownTableOptions
3-
* @property {string|null|Array.<string|null|undefined>} [align]
2+
* @typedef Options
3+
* @property {string|null|Array<string|null|undefined>} [align]
44
* @property {boolean} [padding=true]
55
* @property {boolean} [delimiterStart=true]
66
* @property {boolean} [delimiterStart=true]
@@ -9,62 +9,51 @@
99
* @property {(value: string) => number} [stringLength]
1010
*/
1111

12+
/**
13+
* @typedef {Options} MarkdownTableOptions
14+
* @todo
15+
* Remove next major.
16+
*/
17+
1218
/**
1319
* Create a table from a matrix of strings.
1420
*
15-
* @param {Array.<Array.<string|null|undefined>>} table
16-
* @param {MarkdownTableOptions} [options]
21+
* @param {Array<Array<string|null|undefined>>} table
22+
* @param {Options} [options]
1723
* @returns {string}
1824
*/
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. */
2429
const alignments = []
25-
let rowIndex = -1
26-
/** @type {string[][]} Cells per row. */
30+
/** @type {Array<Array<string>>} Cells per row. */
2731
const cellMatrix = []
28-
/** @type {number[][]} Sizes of each cell per row. */
32+
/** @type {Array<Array<number>>} Sizes of each cell per row. */
2933
const sizeMatrix = []
30-
/** @type {number[]} */
34+
/** @type {Array<number>} */
3135
const longestCellByColumn = []
3236
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
5138

5239
// This is a superfluous loop if we don’t align delimiters, but otherwise we’d
5340
// do superfluous work when aligning, so optimize for aligning.
5441
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
5847

5948
if (table[rowIndex].length > mostCellsPerRow) {
6049
mostCellsPerRow = table[rowIndex].length
6150
}
6251

6352
while (++columnIndex < table[rowIndex].length) {
64-
cell = serialize(table[rowIndex][columnIndex])
53+
const cell = serialize(table[rowIndex][columnIndex])
6554

66-
if (settings.alignDelimiters !== false) {
67-
size = stringLength(cell)
55+
if (options.alignDelimiters !== false) {
56+
const size = stringLength(cell)
6857
sizes[columnIndex] = size
6958

7059
if (
@@ -83,14 +72,14 @@ export function markdownTable(table, options) {
8372
}
8473

8574
// Figure out which alignments to use.
86-
columnIndex = -1
75+
let columnIndex = -1
8776

8877
if (typeof align === 'object' && 'length' in align) {
8978
while (++columnIndex < mostCellsPerRow) {
9079
alignments[columnIndex] = toAlignment(align[columnIndex])
9180
}
9281
} else {
93-
code = toAlignment(align)
82+
const code = toAlignment(align)
9483

9584
while (++columnIndex < mostCellsPerRow) {
9685
alignments[columnIndex] = code
@@ -99,13 +88,15 @@ export function markdownTable(table, options) {
9988

10089
// Inject the alignment row.
10190
columnIndex = -1
102-
row = []
103-
sizes = []
91+
/** @type {Array<string>} */
92+
const row = []
93+
/** @type {Array<number>} */
94+
const sizes = []
10495

10596
while (++columnIndex < mostCellsPerRow) {
106-
code = alignments[columnIndex]
107-
before = ''
108-
after = ''
97+
const code = alignments[columnIndex]
98+
let before = ''
99+
let after = ''
109100

110101
if (code === 99 /* `c` */) {
111102
before = ':'
@@ -117,17 +108,17 @@ export function markdownTable(table, options) {
117108
}
118109

119110
// 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
122113
? 1
123114
: Math.max(
124115
1,
125116
longestCellByColumn[columnIndex] - before.length - after.length
126117
)
127118

128-
cell = before + '-'.repeat(size) + after
119+
const cell = before + '-'.repeat(size) + after
129120

130-
if (settings.alignDelimiters !== false) {
121+
if (options.alignDelimiters !== false) {
131122
size = before.length + size + after.length
132123

133124
if (size > longestCellByColumn[columnIndex]) {
@@ -145,23 +136,25 @@ export function markdownTable(table, options) {
145136
sizeMatrix.splice(1, 0, sizes)
146137

147138
rowIndex = -1
148-
/** @type {string[]} */
139+
/** @type {Array<string>} */
149140
const lines = []
150141

151142
while (++rowIndex < cellMatrix.length) {
152-
row = cellMatrix[rowIndex]
153-
sizes = sizeMatrix[rowIndex]
143+
const row = cellMatrix[rowIndex]
144+
const sizes = sizeMatrix[rowIndex]
154145
columnIndex = -1
155-
line = []
146+
/** @type {Array<string>} */
147+
const line = []
156148

157149
while (++columnIndex < mostCellsPerRow) {
158-
cell = row[columnIndex] || ''
159-
before = ''
160-
after = ''
150+
const cell = row[columnIndex] || ''
151+
let before = ''
152+
let after = ''
161153

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]
165158

166159
if (code === 114 /* `r` */) {
167160
before = ' '.repeat(size)
@@ -178,44 +171,44 @@ export function markdownTable(table, options) {
178171
}
179172
}
180173

181-
if (settings.delimiterStart !== false && !columnIndex) {
174+
if (options.delimiterStart !== false && !columnIndex) {
182175
line.push('|')
183176
}
184177

185178
if (
186-
settings.padding !== false &&
179+
options.padding !== false &&
187180
// Don’t add the opening space if we’re not aligning and the cell is
188181
// 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)
191184
) {
192185
line.push(' ')
193186
}
194187

195-
if (settings.alignDelimiters !== false) {
188+
if (options.alignDelimiters !== false) {
196189
line.push(before)
197190
}
198191

199192
line.push(cell)
200193

201-
if (settings.alignDelimiters !== false) {
194+
if (options.alignDelimiters !== false) {
202195
line.push(after)
203196
}
204197

205-
if (settings.padding !== false) {
198+
if (options.padding !== false) {
206199
line.push(' ')
207200
}
208201

209202
if (
210-
settings.delimiterEnd !== false ||
203+
options.delimiterEnd !== false ||
211204
columnIndex !== mostCellsPerRow - 1
212205
) {
213206
line.push('|')
214207
}
215208
}
216209

217210
lines.push(
218-
settings.delimiterEnd === false
211+
options.delimiterEnd === false
219212
? line.join('').replace(/ +$/, '')
220213
: line.join('')
221214
)

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
"xo": "^0.47.0"
4545
},
4646
"scripts": {
47-
"prepack": "npm run build && npm run format",
47+
"prepublishOnly": "npm run build && npm run format",
4848
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4949
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
50-
"test-api": "node test.js",
51-
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
50+
"test-api": "node --conditions development test.js",
51+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
5252
"test": "npm run build && npm run format && npm run test-coverage"
5353
},
5454
"prettier": {

0 commit comments

Comments
 (0)