forked from moment/moment-timezone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-dedupe.js
73 lines (59 loc) · 1.7 KB
/
data-dedupe.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
"use strict";
function dedupe(zone) {
var abbrs = [],
untils = [],
offsets = [],
length = zone.abbrs.length,
i;
for (i = length - 1; i >= 0; i--) {
if (abbrs[0] === zone.abbrs[i] &&
offsets[0] === zone.offsets[i]) { continue;}
untils.unshift(i === length - 1 ? Infinity : zone.untils[i + 1]);
abbrs.unshift(zone.abbrs[i]);
offsets.unshift(zone.offsets[i]);
}
return {
name : zone.name,
abbrs : abbrs,
untils : untils,
offsets : offsets,
population : zone.population,
countries : zone.countries
};
}
function findVersion (source) {
var matches = source.match(/\nRelease (\d{4}[a-z]) /);
if (matches && matches[1]) {
return matches[1];
}
throw new Error("Could not find version from temp/download/latest/NEWS.");
}
function addCountries(countries) {
var result = [];
for (var country in countries) {
result.push({
name : countries[country].abbr,
zones : countries[country].zones
});
}
return result;
}
module.exports = function (grunt) {
grunt.registerTask('data-dedupe', '6. Remove duplicate entries from data-collect.', function (version) {
version = version || 'latest';
var zones = grunt.file.readJSON('temp/collect/' + version + '.json'),
meta = grunt.file.readJSON('data/meta/' + version + '.json'),
output = {
version : version,
zones : zones.map(dedupe),
links : [],
countries : addCountries(meta.countries)
};
if (version === 'latest') {
output.version = findVersion(grunt.file.read('temp/download/latest/NEWS'));
}
grunt.file.mkdir('data/unpacked');
grunt.file.write('data/unpacked/' + version + '.json', JSON.stringify(output, null, 2));
grunt.log.ok('Deduped data for ' + version);
});
};