Skip to content

Commit 2666fd4

Browse files
committedMar 18, 2021
Add JSDoc based types
1 parent f1a3411 commit 2666fd4

File tree

6 files changed

+93
-20
lines changed

6 files changed

+93
-20
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
*.d.ts
23
*.log
34
coverage/
45
node_modules/

‎index.js

+53-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,56 @@
11
import repeat from 'repeat-string'
22

3-
// Create a table from a matrix of strings.
3+
/**
4+
* @typedef MarkdownTableOptions
5+
* @property {string|string[]} [align]
6+
* @property {boolean} [padding=true]
7+
* @property {boolean} [delimiterStart=true]
8+
* @property {boolean} [delimiterStart=true]
9+
* @property {boolean} [delimiterEnd=true]
10+
* @property {boolean} [alignDelimiters=true]
11+
* @property {(value: string) => number} [stringLength]
12+
*/
13+
14+
/**
15+
* Create a table from a matrix of strings.
16+
*
17+
* @param {string[][]} table
18+
* @param {MarkdownTableOptions} [options]
19+
* @returns {string}
20+
*/
421
export function markdownTable(table, options) {
522
var settings = options || {}
623
var align = (settings.align || []).concat()
724
var stringLength = settings.stringLength || defaultStringLength
25+
/** @type {number[]} Character codes as symbols for alignment per column. */
826
var alignments = []
927
var rowIndex = -1
28+
/** @type {string[][]} Cells per row. */
1029
var cellMatrix = []
30+
/** @type {number[][]} Sizes of each cell per row. */
1131
var sizeMatrix = []
32+
/** @type {number[]} */
1233
var longestCellByColumn = []
1334
var mostCellsPerRow = 0
35+
/** @type {number} */
1436
var columnIndex
37+
/** @type {string[]} Cells of current row */
1538
var row
39+
/** @type {number[]} Sizes of current row */
1640
var sizes
17-
var largest
41+
/** @type {number} Sizes of current cell */
1842
var size
43+
/** @type {string} Current cell */
1944
var cell
45+
/** @type {string[]} */
2046
var lines
47+
/** @type {string[]} Chunks of current line. */
2148
var line
49+
/** @type {string} */
2250
var before
51+
/** @type {string} */
2352
var after
53+
/** @type {number} */
2454
var code
2555

2656
// This is a superfluous loop if we don’t align delimiters, but otherwise we’d
@@ -41,9 +71,10 @@ export function markdownTable(table, options) {
4171
size = stringLength(cell)
4272
sizes[columnIndex] = size
4373

44-
largest = longestCellByColumn[columnIndex]
45-
46-
if (largest === undefined || size > largest) {
74+
if (
75+
longestCellByColumn[columnIndex] === undefined ||
76+
size > longestCellByColumn[columnIndex]
77+
) {
4778
longestCellByColumn[columnIndex] = size
4879
}
4980
}
@@ -186,26 +217,36 @@ export function markdownTable(table, options) {
186217
}
187218
}
188219

189-
line = line.join('')
190-
191-
if (settings.delimiterEnd === false) {
192-
line = line.replace(/ +$/, '')
193-
}
194-
195-
lines.push(line)
220+
lines.push(
221+
settings.delimiterEnd === false
222+
? line.join('').replace(/ +$/, '')
223+
: line.join('')
224+
)
196225
}
197226

198227
return lines.join('\n')
199228
}
200229

230+
/**
231+
* @param {string|null|undefined} [value]
232+
* @returns {string}
233+
*/
201234
function serialize(value) {
202235
return value === null || value === undefined ? '' : String(value)
203236
}
204237

238+
/**
239+
* @param {string} value
240+
* @returns {number}
241+
*/
205242
function defaultStringLength(value) {
206243
return value.length
207244
}
208245

246+
/**
247+
* @param {string} value
248+
* @returns {number}
249+
*/
209250
function toAlignment(value) {
210251
var code = typeof value === 'string' ? value.charCodeAt(0) : 0
211252

‎package.json

+14-6
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,37 @@
2424
"sideEffects": false,
2525
"type": "module",
2626
"main": "index.js",
27+
"types": "index.d.ts",
2728
"files": [
29+
"index.d.ts",
2830
"index.js"
2931
],
3032
"dependencies": {
33+
"@types/repeat-string": "^1.0.0",
3134
"repeat-string": "^1.0.0"
3235
},
3336
"devDependencies": {
37+
"@types/tape": "^4.0.0",
3438
"c8": "^7.0.0",
3539
"chalk": "^4.0.0",
3640
"nyc": "^15.0.0",
3741
"prettier": "^2.0.0",
3842
"remark-cli": "^9.0.0",
3943
"remark-preset-wooorm": "^8.0.0",
44+
"rimraf": "^3.0.0",
4045
"strip-ansi": "^6.0.0",
4146
"tape": "^5.0.0",
47+
"type-coverage": "^2.0.0",
48+
"typescript": "^4.0.0",
4249
"xo": "^0.38.0"
4350
},
4451
"scripts": {
52+
"prepack": "npm run build && npm run format",
53+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4554
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
4655
"test-api": "node test.js",
4756
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
48-
"test": "npm run format && npm run test-coverage"
57+
"test": "npm run build && npm run format && npm run test-coverage"
4958
},
5059
"remarkConfig": {
5160
"plugins": [
@@ -68,10 +77,9 @@
6877
"prefer-arrow-callback": "off"
6978
}
7079
},
71-
"nyc": {
72-
"check-coverage": true,
73-
"lines": 100,
74-
"functions": 100,
75-
"branches": 100
80+
"typeCoverage": {
81+
"atLeast": 100,
82+
"detail": true,
83+
"strict": true
7684
}
7785
}

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Turns a given matrix of strings (an array of arrays of strings) into a table.
8181
###### `options.align`
8282

8383
One style for all columns, or styles for their respective columns (`string` or
84-
`Array.<string>`).
84+
`string[]`).
8585
Each style is either `'l'` (left), `'r'` (right), or `'c'` (center).
8686
Other values are treated as `''`, which doesn’t place the colon in the alignment
8787
row but does align left.

‎test.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ test('markdownTable()', function (t) {
2323
markdownTable([
2424
['Type', 'Value'],
2525
['string', 'alpha'],
26+
// @ts-ignore
2627
['number', 1],
28+
// @ts-ignore
2729
['boolean', true],
2830
['undefined', undefined],
2931
['null', null],
32+
// @ts-ignore
3033
['Array', [1, 2, 3]]
3134
]),
3235
[
@@ -285,7 +288,11 @@ test('markdownTable()', function (t) {
285288
t.end()
286289
})
287290

288-
// Get the length of a string, minus ANSI color characters.
291+
/**
292+
* Get the length of a string, minus ANSI color characters.
293+
* @param {string} value
294+
* @returns {number}
295+
*/
289296
function stringLength(value) {
290297
return strip(value).length
291298
}

‎tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"files": ["index.js"],
3+
"include": ["*.js"],
4+
"compilerOptions": {
5+
"target": "ES2020",
6+
"lib": ["ES2020"],
7+
"module": "ES2020",
8+
"moduleResolution": "node",
9+
"allowJs": true,
10+
"checkJs": true,
11+
"declaration": true,
12+
"emitDeclarationOnly": true,
13+
"allowSyntheticDefaultImports": true,
14+
"skipLibCheck": true
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.