Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 26, 2021
1 parent 8a04321 commit 3333a96
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
44 changes: 43 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import yauzl from 'yauzl'
import dsv from 'd3-dsv'
import {bail} from 'bail'

/**
* @typedef {Object} Language
* @property {string} name
* @property {string} type
* @property {string} scope
* @property {string} iso6393
* @property {string} [iso6392B]
* @property {string} [iso6392T]
* @property {string} [iso6391]
*/

/** @type {string[]} */
var other = []
var found = false

Expand Down Expand Up @@ -34,6 +46,9 @@ var expectedName = 'iso-639-3_20200515.tab'

https.request(url, onrequest).end()

/**
* @param {import('http').IncomingMessage} request
*/
function onrequest(request) {
request
.pipe(fs.createWriteStream('archive.zip'))
Expand All @@ -45,6 +60,10 @@ function onclose() {
yauzl.open('archive.zip', {lazyEntries: true}, onopen)
}

/**
* @param {Error?} error
* @param {import('yauzl').ZipFile} [archive]
*/
function onopen(error, archive) {
bail(error)

Expand All @@ -53,6 +72,9 @@ function onopen(error, archive) {
archive.on('entry', onentry)
archive.on('end', onend)

/**
* @param {import('yauzl').Entry} entry
*/
function onentry(entry) {
var name = path.basename(entry.fileName)

Expand All @@ -65,6 +87,10 @@ function onopen(error, archive) {
archive.openReadStream(entry, onreadstream)
}

/**
* @param {Error?} error
* @param {import('stream').Readable} [rs]
*/
function onreadstream(error, rs) {
bail(error)
rs.pipe(concat(onconcat)).on('error', bail)
Expand All @@ -82,12 +108,22 @@ function onend() {
}
}

/**
* @param {Buffer} body
*/
function onconcat(body) {
var data = dsv.tsvParse(String(body)).map((d) => map(d))
var data = dsv.tsvParse(String(body)).map(
// @ts-ignore
(d) => map(d)
)
/** @type {Object.<string, string>} */
var toB = {}
/** @type {Object.<string, string>} */
var toT = {}
/** @type {Object.<string, string>} */
var to1 = {}
var index = -1
/** @type {Language} */
var d

while (++index < data.length) {
Expand Down Expand Up @@ -119,10 +155,16 @@ function onconcat(body) {
)
}

/**
* @param {{Ref_Name: string, Id: string, Language_Type: string, Scope: string, Part2B?: string, Part2T: string, Part1?: string}} d
* @returns {Language}
*/
function map(d) {
var name = d.Ref_Name
var id = d.Id
/** @type {string?} */
var type = types[d.Language_Type]
/** @type {string?} */
var scope = scopes[d.Scope]

if (!name) {
Expand Down
25 changes: 23 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,47 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts",
"iso6393.js",
"iso6393.d.ts",
"iso6393-to-1.js",
"iso6393-to-1.d.ts",
"iso6393-to-2b.js",
"iso6393-to-2t.js"
"iso6393-to-2b.d.ts",
"iso6393-to-2t.js",
"iso6393-to-2t.d.ts"
],
"devDependencies": {
"@types/concat-stream": "^1.0.0",
"@types/d3-dsv": "^2.0.0",
"@types/node": "^14.0.0",
"@types/tape": "^4.0.0",
"@types/yauzl": "^2.0.0",
"bail": "^2.0.0",
"c8": "^7.0.0",
"concat-stream": "^2.0.0",
"d3-dsv": "^2.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0",
"yauzl": "^2.0.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"generate": "node build",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -72,5 +88,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"files": ["index.js"],
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}

0 comments on commit 3333a96

Please sign in to comment.