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 28, 2021
1 parent 0c89557 commit 8a41e7f
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 46 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
39 changes: 30 additions & 9 deletions build-iso-3166-1-a2-table.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import unified from 'unified'
// @ts-ignore To do: remove when types are added
import format from 'rehype-format'
import stringify from 'rehype-stringify'
import h from 'hastscript'
import u from 'unist-builder'
// @ts-ignore To do: remove when types are added
import range from 'mdast-util-heading-range'
import {iso31661Reserved} from './1-reserved.js'
import {iso31661} from './1.js'
Expand All @@ -12,37 +14,56 @@ var processor = unified().use(format).use(stringify)
export default function table() {
return transform

/**
* @param {import('mdast').Root} tree
*/
function transform(tree) {
var fromCode = String.fromCharCode
var a = 65
var z = 90

var a2ToState = {}
var a2ToName = {}
iso31661.concat(iso31661Reserved).forEach((d) => {
a2ToState[d.alpha2] = d.state
a2ToName[d.alpha2] = d.name
})
iso31661
.map(({alpha2, state, name}) => ({alpha2, state, name}))
.concat(
iso31661Reserved.map(({alpha2, state, name}) => ({alpha2, state, name}))
)
.forEach((d) => {
a2ToState[d.alpha2] = d.state
a2ToName[d.alpha2] = d.name
})

range(tree, 'matrix', onrun)

function onrun(start, nodes, end) {
/**
* @param {import('unist').Node?} start
* @param {import('unist').Node[]} _
* @param {import('unist').Node?} end
*/
function onrun(start, _, end) {
var head = [h('th')]
/** @type {import('hast').Element[]} */
var rows = []
/** @type {import('hast').Element[]} */
var cells
/** @type {number} */
var x = a
/** @type {number} */
var y
/** @type {import('hast').Element|string} */
var code
/** @type {string} */
var state
/** @type {string} */
var title

while (x <= z) {
y = a
head.push(h('th', fromCode(x)))
cells = [h('th', {scope: 'row'}, fromCode(x))]
head.push(h('th', String.fromCharCode(x)))
cells = [h('th', {scope: 'row'}, String.fromCharCode(x))]

while (y <= z) {
code = fromCode(x) + fromCode(y)
code = String.fromCharCode(x) + String.fromCharCode(y)
state = a2ToState[code]
title = a2ToName[code]

Expand Down
Loading

0 comments on commit 8a41e7f

Please sign in to comment.