-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
82 lines (62 loc) · 2.14 KB
/
update.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
75
76
77
78
79
80
81
82
var fs = require('fs');
var _ = require('lodash');
var getTz = require('get-tz');
var worldCountries = require('world-countries');
const dataPath = './dist';
function getNativeNames(native) {
let names = [];
for (let lang of Object.keys(native)) {
if (_.has(native[lang], "common")) {
names.push(native[lang]["common"]);
}
if (_.has(native[lang], "official")) {
names.push(native[lang]["official"]);
}
}
return names;
}
function getCountryName(cca2) {
let c = worldCountries.find(country => {
return country["cca2"] == cca2;
});
if (c !== undefined && _.has(c, "name.official")) {
return c["name"]["official"];
}
return cca2;
}
function getCountryKeywords(cca2) {
let c = worldCountries.find(country => {
return country["cca2"] == cca2;
});
let keywords = [cca2];
keywords.push(getCountryName(cca2));
if (_.has(c, 'name.common')) {
keywords.push(c["name"]["common"]);
}
if (_.has(c, 'name.official')) {
keywords.push(c["name"]["official"]);
}
if (_.has(c, 'name.native')) {
keywords.push(getNativeNames(c["name"]["native"]));
}
return _.uniq(_.union(keywords).join(" ").split(/[\s,]+/)).join(" ");
}
getTz().then(rows => {
fs.writeFileSync(`${dataPath}/timezones-iana.json`, JSON.stringify(rows, null, 2), 'utf8');
let countryZones = [];
let keywordZones = [];
for (let row of rows) {
countryZones.push({
name: row.id,
countryCode: row.countryCode,
countryName: getCountryName(row.countryCode)
});
keywordZones.push({
value: row.id,
label: row.id, //TODO: translate to local timezone names and fork files
keywords: getCountryKeywords(row.countryCode)
});
}
fs.writeFileSync(`${dataPath}/timezones-countries.json`, JSON.stringify(countryZones, null, 2), 'utf8');
fs.writeFileSync(`${dataPath}/timezones-keywords.json`, JSON.stringify(keywordZones, null, 2), 'utf8');
});