-
Notifications
You must be signed in to change notification settings - Fork 1
/
make.js
68 lines (50 loc) · 1.39 KB
/
make.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const fs = require('fs')
const https = require('https')
const csvParse = require('csv-parse')
const URL = 'https://raw.githubusercontent.com/vincentarelbundock/countrycode/master/data/countrycode_data.csv'
const FILE = './index.js'
const ISO3_COLNAME = 'iso3c'
const REGEX_COLNAME = 'country.name.en.regex'
var body = ''
https.get(URL, function (res) {
res.on('data', (chunk) => { body += chunk })
res.on('end', parse)
})
.on('error', (err) => { throw err })
function parse () {
csvParse(body, { columns: true }, (err, data) => {
if (err) throw err
formatHash(data)
})
}
function formatHash (data) {
var hash = {}
data.forEach((obj) => {
var iso3 = obj[ISO3_COLNAME]
var regex = obj[REGEX_COLNAME]
if (!iso3 || !regex) return
hash[iso3] = regex
})
formatString(hash)
}
function formatString (hash) {
var strHash = JSON.stringify(hash, null, 2)
var lines = strHash.split('\n')
// remove "" in props
// replace " for '
lines = lines.map((line, i) => {
if (i === 0 || i === lines.length - 1) return line
var parts = line.split(':')
var lhs = parts[0].replace(/"/g, '')
var rhs = parts.slice(1).join('').replace(/"/g, "'")
return lhs + ':' + rhs
})
// make CommonJS module
var str = 'module.exports = ' + lines.join('\n') + '\n'
write(str)
}
function write (str) {
fs.writeFile(FILE, str, (err) => {
if (err) throw err
})
}