From 7814699efe43b1a068d49a203a00d08557e03b37 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 4 Nov 2021 09:31:20 +0100 Subject: [PATCH] Refactor --- .gitignore | 6 +- build.js | 161 ++++++++++++++++++++++++++--------------------------- 2 files changed, 81 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 7625000..1468b92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -.DS_Store -*.d.ts -*.log coverage/ node_modules/ +*.d.ts +*.log +.DS_Store archive.zip yarn.lock diff --git a/build.js b/build.js index d76e866..c637281 100755 --- a/build.js +++ b/build.js @@ -1,11 +1,3 @@ -import fs from 'node:fs' -import path from 'node:path' -import https from 'node:https' -import concat from 'concat-stream' -import yauzl from 'yauzl' -import {tsvParse} from 'd3-dsv' -import {bail} from 'bail' - /** * @typedef {Object} Language * @property {string} name @@ -17,6 +9,14 @@ import {bail} from 'bail' * @property {string} [iso6391] */ +import fs from 'node:fs' +import path from 'node:path' +import https from 'node:https' +import concatStream from 'concat-stream' +import yauzl from 'yauzl' +import {tsvParse} from 'd3-dsv' +import {bail} from 'bail' + /** @type {string[]} */ const other = [] let found = false @@ -69,53 +69,89 @@ function onopen(error, archive) { read() - archive.on('entry', onentry) - archive.on('end', onend) - - /** - * @param {import('yauzl').Entry} entry - */ - function onentry(entry) { - const name = path.basename(entry.fileName) - - if (name !== expectedName) { - other.push(name) - return read() + archive.on( + 'entry', + /** + * @param {import('yauzl').Entry} entry + */ (entry) => { + const name = path.basename(entry.fileName) + + if (name !== expectedName) { + other.push(name) + return read() + } + + found = true + archive.openReadStream( + entry, + /** + * @param {Error?} error + * @param {import('stream').Readable} [rs] + */ (error, rs) => { + bail(error) + rs.pipe(concatStream(onconcat)).on('error', bail) + rs.on('end', read) + } + ) } + ) - found = true - 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) - rs.on('end', read) - } + archive.on('end', () => { + if (!found) { + throw new Error('File not found, pick one of: `' + other + '`') + } + }) function read() { archive.readEntry() } } -function onend() { - if (!found) { - throw new Error('File not found, pick one of: `' + other + '`') - } -} - /** * @param {Buffer} body */ function onconcat(body) { const data = tsvParse(String(body)).map( - // @ts-ignore - (d) => map(d) + /** + * @param {{Ref_Name: string, Id: string, Language_Type: string, Scope: string, Part2B?: string, Part2T: string, Part1?: string}} d + * @returns {Language} + */ + (d) => { + const name = d.Ref_Name + const id = d.Id + /** @type {string?} */ + const type = types[d.Language_Type] + /** @type {string?} */ + const scope = scopes[d.Scope] + + if (!name) { + console.error('Cannot handle language w/o name', d) + } + + if (!type) { + console.error('Cannot handle language w/o type', d) + } + + if (!scope) { + console.error('Cannot handle language w/o scope', d) + } + + if (!id) { + console.error('Cannot handle language w/o scope', d) + } + + return { + name, + type, + scope, + iso6393: id, + iso6392B: d.Part2B || undefined, + iso6392T: d.Part2T || undefined, + iso6391: d.Part1 || undefined + } + } ) + /** @type {Object.} */ const toB = {} /** @type {Object.} */ @@ -123,11 +159,9 @@ function onconcat(body) { /** @type {Object.} */ const to1 = {} let index = -1 - /** @type {Language} */ - let d while (++index < data.length) { - d = data[index] + const d = data[index] if (d.iso6392B) toB[d.iso6393] = d.iso6392B if (d.iso6392T) toT[d.iso6393] = d.iso6392T if (d.iso6391) to1[d.iso6393] = d.iso6391 @@ -154,42 +188,3 @@ function onconcat(body) { bail ) } - -/** - * @param {{Ref_Name: string, Id: string, Language_Type: string, Scope: string, Part2B?: string, Part2T: string, Part1?: string}} d - * @returns {Language} - */ -function map(d) { - const name = d.Ref_Name - const id = d.Id - /** @type {string?} */ - const type = types[d.Language_Type] - /** @type {string?} */ - const scope = scopes[d.Scope] - - if (!name) { - console.error('Cannot handle language w/o name', d) - } - - if (!type) { - console.error('Cannot handle language w/o type', d) - } - - if (!scope) { - console.error('Cannot handle language w/o scope', d) - } - - if (!id) { - console.error('Cannot handle language w/o scope', d) - } - - return { - name, - type, - scope, - iso6393: id, - iso6392B: d.Part2B || undefined, - iso6392T: d.Part2T || undefined, - iso6391: d.Part1 || undefined - } -}