-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
101 lines (80 loc) · 2.47 KB
/
utils.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const moment = require('moment-timezone');
const orderBy = require('lodash.orderby');
const uniq = require('lodash.uniq');
const { cityTranslations } = require('./config');
const CONTINENT_ALLOWLIST = [
'Europe',
'Asia',
'America',
'America/Argentina',
'Africa',
'Australia',
'Pacific',
'Atlantic',
'Antarctica',
'Arctic',
'Indian',
];
const LABELS_DENYLIST = [
'Australia/West',
'Australia/South',
'America/North_Dakota/Center',
];
const _getDates = (startDate, numDays) => {
const dateArray = [];
const momentStart = moment(startDate);
for (let i = 0; i <= numDays; i++) {
dateArray.push(momentStart.clone().add(i, 'days').format('YYYY-MM-DD'));
}
return dateArray;
};
const _extractContinent = label => {
if (label.indexOf('Istanbul') !== -1) {
return 'Europe';
}
const lastIndex = label.lastIndexOf('/');
return (lastIndex === -1 ? label : label.substr(0, lastIndex));
}
const _isRegularContinent = continent => CONTINENT_ALLOWLIST.includes(continent);
const generateMappedDB = (database, startDate, numDays) => {
console.log(`Initializing data starting ${startDate} with ${numDays} days in the future, comparing ${database.length} timezones`);
const theDates = _getDates(startDate, numDays);
return database.map(d => {
const continent = _extractContinent(d.label);
return {
...d,
continent,
isRegularContinent: _isRegularContinent(continent),
dates: theDates.map(date => moment.tz(date, d.label).utc().format()),
}
});
};
const compareDateArrs = (arr1, arr2) => arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);
const _extractCity = label => {
if (cityTranslations[label]) {
return cityTranslations[label];
}
if (label.indexOf('Etc/') === 0 || LABELS_DENYLIST.includes(label)) {
return label;
}
const lastIndex = label.lastIndexOf('/');
return (lastIndex === -1 ? label : label.substr(lastIndex + 1)).replace(/[\W_]/g, ' ');
}
const calculateGroupLabel = (rawTZs, max = 5) => {
rawTZs = orderBy(rawTZs, 'count', 'desc');
const shrinkedTZs = rawTZs.filter(({ label }) => _isRegularContinent(_extractContinent(label)));
rawTZs = shrinkedTZs.length === 0 ? [rawTZs[0]] : shrinkedTZs;
const uniqueLabels = uniq(
rawTZs
.map(({ label }) => _extractCity(label))
.filter(_ => !!_)
);
return uniqueLabels
.slice(0, max)
.join(', ');
}
module.exports = {
generateMappedDB,
compareDateArrs,
calculateGroupLabel,
};