Skip to content

Commit

Permalink
Add strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 27, 2021
1 parent 9d3c382 commit cf2dbe4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
91 changes: 46 additions & 45 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,73 @@
import fs from 'node:fs'
import assert from 'node:assert'
import https from 'node:https'
import fs from 'node:fs'
import {URL} from 'node:url'
import concat from 'concat-stream'
import yauzl from 'yauzl'
import * as dsv from 'd3-dsv'
import {bail} from 'bail'
import not from 'not'

const headers = ['code', 'numeric', 'english', 'french', 'pva', 'age', 'date']
/** @type {string[]} */
const other = []
let found = false

https
.request(
new URL('https://www.unicode.org/iso15924/iso15924.txt.zip'),
onconnection
(response) => {
response
.pipe(fs.createWriteStream('archive.zip'))
.on('close', onclose)
.on('error', bail)
}
)
.end()

function onconnection(response) {
response
.pipe(fs.createWriteStream('archive.zip'))
.on('close', onclose)
.on('error', bail)
}

function onclose() {
yauzl.open('archive.zip', {lazyEntries: true}, onopen)
}

function onopen(error, archive) {
bail(error)

read()
yauzl.open('archive.zip', {lazyEntries: true}, (error, archive) => {
bail(error)

archive.on('entry', onentry)
archive.on('end', onend)
read()

function onentry(entry) {
if (entry.fileName !== 'iso15924-utf8-20210217.txt') {
other.push(entry.fileName)
return read()
}
assert(archive, 'expected archive')

found = true
archive.openReadStream(entry, onreadstream)
}
archive.on('entry', (/** @type {import('yauzl').Entry} */ entry) => {
if (entry.fileName !== 'iso15924-utf8-20210217.txt') {
other.push(entry.fileName)
return read()
}

function onreadstream(error, rs) {
bail(error)
rs.pipe(concat(onconcat)).on('error', bail)
rs.on('end', read)
}
found = true
archive.openReadStream(entry, (error, rs) => {
bail(error)
assert(rs, 'expected read stream')
rs.pipe(concat(onconcat)).on('error', bail)
rs.on('end', read)
})
})

function read() {
archive.readEntry()
}
}
archive.on('end', onend)

function onend() {
if (!found) {
throw new Error('File not found, but these were: `' + other + '`')
}
function read() {
assert(archive, 'expected archive')
archive.readEntry()
}
})
}

/**
* @param {Buffer} body
*/
function onconcat(body) {
const data = dsv
.dsvFormat(';')
.parse(
headers.join(';') +
String(body).split('\n').filter(not(comment)).join('\n')
String(body)
.split('\n')
.filter((d) => d.charAt(0) !== '#')
.join('\n')
)
.map(function (script) {
return {
Expand All @@ -90,16 +89,18 @@ function onconcat(body) {
' * @property {string} numeric Three character ISO 15924 code',
' * @property {string} [pva] Property value alias',
' * @property {string} date Date of addition (e.g., `2016-12-05`)',
' *',
' * @type {Script[]} List of scripts.',
' */',
'',
'/** @type {Script[]} */',
'export const iso15924 = ' + JSON.stringify(data, null, 2),
''
].join('\n'),
bail
)
}

function comment(line) {
return line.charAt(0) === '#'
function onend() {
if (!found) {
throw new Error('File not found, but these were: `' + other + '`')
}
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @property {string} numeric Three character ISO 15924 code
* @property {string} [pva] Property value alias
* @property {string} date Date of addition (e.g., `2016-12-05`)
*
* @type {Script[]} List of scripts.
*/

/** @type {Script[]} */
export const iso15924 = [
{
code: 'Adlm',
Expand Down
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,30 @@
"index.js"
],
"devDependencies": {
"@types/d3-dsv": "^3.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": "^3.0.0",
"not": "^0.1.0",
"prettier": "^2.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^9.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.45.0",
"yauzl": "^2.0.0"
},
"scripts": {
"prepublishOnly": "npm run build",
"generate": "node build",
"prebuild": "rimraf \"*.d.ts\"",
"build": "tsc",
"prepublishOnly": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
"test": "npm run generate && npm run build && npm run format && npm run test-coverage"
},
"prettier": {
Expand All @@ -68,5 +69,11 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"files": ["index.js"],
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020", "dom"],
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit cf2dbe4

Please sign in to comment.