Debian Codegraph @

scripts/compare-versions.js

raw symbols SCIP
1 /* jshint esversion: 6 */
2
3 const assert = require('assert');
4
5 const FROM_YEAR = 1970;
6 const TO_YEAR = 2050;
7
8 compareVersions('2019b', 'latest');
9
10 function compareVersions(v1, v2, timezones = 'all') {
11
12 /**
13 * Get moment-timezone version v1
14 */
15 const version1 = require("../moment-timezone");
16 version1.tz.load(require(`../data/packed/${v1}.json`));
17
18 /**
19 * Get timezone names and dates from v1
20 */
21 const names1 = version1.tz.names();
22 const dates1 = getFormattedDates(version1, timezones);
23
24 /**
25 * Clear cache
26 */
27 delete require.cache[require.resolve('moment')]
28 delete require.cache[require.resolve('../moment-timezone')]
29
30 /**
31 * Get moment-timezone version v2
32 */
33 var version2 = require("../moment-timezone");
34 version2.tz.load(require(`../data/packed/${v2}.json`));
35
36 /**
37 * Get timezone names and dates from v2
38 */
39 const names2 = version1.tz.names();
40 const dates2 = getFormattedDates(version2, timezones);
41
42 console.log(`Comparing versions ${v1} and ${v2} from ${FROM_YEAR} to ${TO_YEAR}`);
43
44 /**
45 * Compare timezone names
46 */
47 names1.forEach((name1, i) => {
48 if (name1 !== names2[i]) {
49 console.log(`${name1} !== ${names2[i]}`);
50 }
51 });
52
53 /**
54 * Compare dates
55 */
56 dates1.forEach((date1, i) => {
57 if (date1.formatted !== dates2[i].formatted) {
58 console.log(`${date1.zone}: ${date1.date} = ${dates1[i].formatted} in ${v1}, ${dates2[i].formatted} in ${v2}`)
59 }
60 });
61 }
62
63 /**
64 * Iterates from FROM_YEAR to TO_YEAR
65 * and returns formatted dates for each timezone for each month
66 *
67 * @param momentInstance - instance of moment with timezone
68 * @param timezones = 'all' or array of timezones you want to check
69 * @return {Array}
70 */
71 function getFormattedDates (momentInstance, timezones = 'all') {
72 const names = timezones === 'all' ? momentInstance.tz.names() : timezones;
73 const dates = [];
74
75 // ensure that names are equal
76 names.forEach((name, i) => {
77 for (let year = FROM_YEAR; year <= TO_YEAR; year++) {
78 for (let month = 1; month <= 12; month++) {
79 const monthFormatted = month < 10 ? '0' + month : month;
80 dates.push({
81 zone: name,
82 date: `${year}-${monthFormatted}-01T00:00:00Z`,
83 formatted: momentInstance(`${year}-${monthFormatted}-01T00:00:00Z`).tz(name).format()
84 });
85 }
86 }
87 });
88
89 return dates;
90 }