-
Notifications
You must be signed in to change notification settings - Fork 51
/
compile.js
74 lines (54 loc) · 1.68 KB
/
compile.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
69
70
71
72
73
74
var fs = require("fs");
var csv = require("csv");
var filesize = require("file-size");
var OUTPUT_FILE = __dirname + "/iso-3166-2.json";
function eQuest(cb) {
var sourceFile = __dirname + "/data/eQuest.csv";
fs.readFile(sourceFile, "utf8", function (err, data) {
if (err) { return cb(err); }
csv.parse(data, function (err, rows) {
if (err) { return cb(err); }
var output = {};
var currentCode, currentSub;
function validate(sub) {
if (sub.split("-")[0] !== currentCode) {
throw new Error("country did not equal sub code");
}
}
rows.forEach(function (row) {
var code = row[0];
var sub1 = row[1];
var sub2 = row[2];
var name = row[3];
var sub = sub1 || sub2;
if (code) {
currentCode = code;
output[currentCode] = {name: name, divisions: {}}
}
if (sub) {
validate(sub);
currentSub = sub;
output[currentCode].divisions[currentSub] = name;
}
});
cb(null, output);
});
});
}
eQuest(function (err, data) {
if (err) { console.error(err); }
var output = JSON.stringify(data, null, " ");
fs.stat(OUTPUT_FILE, function (err, stats) {
if (err) { console.error(err); }
var oldSize = stats.size;
fs.writeFile(OUTPUT_FILE, output, "utf8", function (err) {
if (err) { console.error(err); }
fs.stat(OUTPUT_FILE, function (err, stats) {
if (err) { console.error(err); }
var newSize = stats.size;
console.log("OLD FILE SIZE: %s", filesize(oldSize).human());
console.log("NEW FILE SIZE: %s", filesize(newSize).human());
});
});
});
});