Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 19, 2020
0 parents commit 8cd8f44
Show file tree
Hide file tree
Showing 12 changed files with 2,593 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
*.log
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- lts/dubnium
- node
137 changes: 137 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
'use strict'

var fs = require('fs')
var https = require('https')
var concat = require('concat-stream')
var unified = require('unified')
var parse = require('rehype-parse')
var $ = require('hast-util-select')
var toString = require('hast-util-to-string')

var headerToField = {
'Global Code': 'global',
'Global Name': 'globalName',
'Region Code': 'region',
'Region Name': 'regionName',
'Sub-region Code': 'subregion',
'Sub-region Name': 'subregionName',
'Intermediate Region Code': 'intermediate',
'Intermediate Region Name': 'intermediateName',
'Country or Area': 'areaName',
'M49 Code': 'area',
'ISO-alpha3 Code': 'iso3166',
'Least Developed Countries (LDC)': 'ldc',
'Land Locked Developing Countries (LLDC)': 'lldc',
'Small Island Developing States (SIDS)': 'sids',
'Developed / Developing Countries': 'status'
}

var booleanFields = ['ldc', 'lldc', 'sids']

// From big to small:
var types = ['global', 'region', 'subregion', 'intermediate', 'area']

https
.request('https://unstats.un.org/unsd/methodology/m49/overview/', onrequest)
.end()

function onrequest(res) {
res.pipe(concat(onconcat)).on('error', console.error)
}

function onconcat(buf) {
var tree = unified()
.use(parse)
.parse(buf)

var table = $.select('#downloadTableEN', tree)
var headers = $.selectAll('thead td', table).map(clean)
var rows = $.selectAll('tbody tr', table)

var fields = headers.map(d => {
if (!(d in headerToField)) {
throw new Error('Cannot handle unknown column header: ', d)
}

return headerToField[d]
})

var records = rows.map(row => {
var record = {}

$.selectAll('td', row).forEach((cell, index) => {
var field = fields[index]
var value = clean(cell)

if (!field) {
throw new Error('Cannot handle superfluous cell: ', cell, index)
}

if (booleanFields.includes(field)) {
value = /^x$/i.test(value)
}

record[field] = value
})

return record
})

var byCode = {}

records.forEach(record => {
var stack = []

types.forEach((prefix, kind) => {
var code = record[prefix]
var name = record[prefix + 'Name']

// Sometimes, intermediate sizes aren’t available (e.g., for Antarctica).
if (!code || !name) {
return
}

if (code in byCode) {
byCode[code].stack = Object.assign([], byCode[code].stack, stack)
} else {
byCode[code] = {
type: kind,
name: name,
code: code,
iso3166: prefix === 'area' ? record.iso3166 : undefined,
stack: stack.concat()
}
}

stack[kind] = code
})
})

var toIso = {}

var codes = Object.keys(byCode)
.sort()
.map(code => {
var entry = byCode[code]

entry.parent = entry.stack.pop()
entry.stack = undefined

if (entry.iso3166) {
toIso[entry.code] = entry.iso3166
}

return entry
})

write('index', codes)
write('to-iso-3166', toIso)

function write(name, data) {
fs.writeFileSync(name + '.json', JSON.stringify(data, null, 2) + '\n')
}
}

function clean(d) {
return toString(d).trim()
}
1 change: 1 addition & 0 deletions funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: wooorm
Loading

0 comments on commit 8cd8f44

Please sign in to comment.