Skip to content

Commit fd3da2c

Browse files
committed
Add JSDoc based types
1 parent b00bde6 commit fd3da2c

File tree

9 files changed

+79
-105
lines changed

9 files changed

+79
-105
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

+47-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
1+
/**
2+
* @typedef {import('xast').Root} Root
3+
* @typedef {import('xast').Element} Element
4+
*
5+
* @typedef {string | Omit<Entry, 'lang'|'alternate'>} Alternate
6+
*
7+
* @typedef Entry Entries represent a single URL and describe them with metadata.
8+
* @property {string} url Full URL (example: `'https://example.org/'`). See <https://www.sitemaps.org/protocol.html#locdef>
9+
* @property {number|string|Date} [modified] Value indicating when the page last changed.
10+
* @property {string} [lang] BCP 47 tag indicating the language of the page (example: `'en-GB'`). See <https://github.com/wooorm/bcp-47>
11+
* @property {Object.<string, Alternate>} [alternate] Translations of the page, where each key is a BCP 47 tag and each value an entry. Alternate resources inherit fields from the entry they are described in.
12+
*/
13+
14+
import {URL} from 'url'
115
import bcp47 from 'bcp-47-normalize'
216
import {u} from 'unist-builder'
317
import {x} from 'xastscript'
418

519
var own = {}.hasOwnProperty
620

21+
/**
22+
* Build a sitemap.
23+
*
24+
* @param {Array<string|Entry>} [data] URLs to build a sitemap for.
25+
* @returns {Root}
26+
*/
727
export function sitemap(data) {
28+
var index = -1
29+
/** @type {Array.<Element>} */
830
var nodes = []
31+
/** @type {Object.<string, Entry>} */
932
var urls = {}
33+
/** @type {Object.<string, Array.<string>>} */
1034
var groupings = {}
11-
var index = -1
12-
var i18n
35+
/** @type {Array.<string>} */
1336
var grouping
37+
/** @type {Entry} */
1438
var entry
39+
/** @type {Entry} */
1540
var alt
41+
/** @type {string} */
1642
var key
43+
/** @type {Element} */
1744
var node
45+
/** @type {Date} */
1846
var modified
47+
/** @type {boolean} */
48+
var i18n
1949

2050
if (data) {
2151
while (++index < data.length) {
@@ -85,14 +115,11 @@ export function sitemap(data) {
85115
if (own.call(urls, key)) {
86116
node = x('url', [x('loc', key)])
87117
entry = urls[key]
88-
modified = entry.modified
89118

90119
nodes.push(node)
91120

92-
if (modified != null) {
93-
if (typeof modified !== 'object') {
94-
modified = new Date(modified)
95-
}
121+
if (entry.modified != null) {
122+
modified = toDate(entry.modified)
96123

97124
if (Number.isNaN(modified.valueOf())) {
98125
throw new TypeError(
@@ -133,8 +160,13 @@ export function sitemap(data) {
133160
])
134161
}
135162

163+
/**
164+
* @param {string|Entry} d
165+
*/
136166
function toEntry(d) {
167+
/** @type {Entry} */
137168
var entry = {}
169+
/** @type {string} */
138170
var url
139171

140172
if (typeof d === 'string') {
@@ -149,3 +181,11 @@ function toEntry(d) {
149181
entry.url = new URL(url).href
150182
return entry
151183
}
184+
185+
/**
186+
* @param {Date|string|number} value
187+
* @returns {Date}
188+
*/
189+
export function toDate(value) {
190+
return typeof value === 'object' ? value : new Date(value)
191+
}

package.json

+15-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"sideEffects": false,
2626
"type": "module",
2727
"main": "index.js",
28-
"types": "types/",
28+
"types": "index.d.ts",
2929
"files": [
30-
"types/index.d.ts",
30+
"index.d.ts",
3131
"index.js"
3232
],
3333
"dependencies": {
@@ -37,21 +37,27 @@
3737
"xastscript": "^3.0.0"
3838
},
3939
"devDependencies": {
40+
"@types/tape": "^4.0.0",
4041
"c8": "^7.0.0",
4142
"concat-stream": "^2.0.0",
4243
"prettier": "^2.0.0",
4344
"regenerate": "^1.0.0",
4445
"remark-cli": "^9.0.0",
4546
"remark-preset-wooorm": "^8.0.0",
47+
"rimraf": "^3.0.0",
4648
"tape": "^5.0.0",
49+
"type-coverage": "^2.0.0",
50+
"typescript": "^4.0.0",
4751
"xast-util-to-xml": "^3.0.0",
4852
"xo": "^0.39.0"
4953
},
5054
"scripts": {
55+
"prepack": "npm run build && npm run format",
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5157
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5258
"test-api": "node test.js",
5359
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
54-
"test": "npm run format && npm run test-coverage"
60+
"test": "npm run build && npm run format && npm run test-coverage"
5561
},
5662
"prettier": {
5763
"tabWidth": 2,
@@ -76,14 +82,16 @@
7682
"no-eq-null": "off",
7783
"no-var": "off",
7884
"prefer-arrow-callback": "off"
79-
},
80-
"ignores": [
81-
"types"
82-
]
85+
}
8386
},
8487
"remarkConfig": {
8588
"plugins": [
8689
"preset-wooorm"
8790
]
91+
},
92+
"typeCoverage": {
93+
"atLeast": 100,
94+
"detail": true,
95+
"strict": true
8896
}
8997
}

test.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ test('sitemap', function (t) {
7676

7777
t.throws(
7878
function () {
79+
// @ts-ignore runtime.
7980
sitemap([{}])
8081
},
8182
/Invalid URL: undefined/,

tsconfig.json

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

types/index.d.ts

-47
This file was deleted.

types/test.ts

-21
This file was deleted.

types/tsconfig.json

-13
This file was deleted.

types/tslint.json

-10
This file was deleted.

0 commit comments

Comments
 (0)