-
Notifications
You must be signed in to change notification settings - Fork 0
/
holidays.js
56 lines (47 loc) · 1.56 KB
/
holidays.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
function isHoliday(date, holidays) {
const formatted = toIsoDate(date);
return holidays.includes(formatted);
}
async function fetchHolidays(year) {
const url = "https://feiertage-api.de/api/?nur_land=BY&nur_daten=1&jahr=" + year;
const payload = await new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open("GET", url, true);
request.setRequestHeader("Accept", "application/json");
request.onerror = reject;
request.onload = function() {
const isSuccess = this.status >= 200 && this.status < 400;
if (isSuccess) {
const data = JSON.parse(this.response);
resolve(data);
return;
}
reject(this);
};
request.send();
});
const result = [];
const keys = Object.keys(payload);
for (let i = 0; i < keys.length; i++) {
const date = payload[keys[i]];
result.push(date);
}
return result;
}
async function getHolidays(start, end) {
try {
const startYear = start.getYear() + 1900;
const endYear = end.getYear() + 1900;
const minYear = Math.min(startYear, endYear);
const maxYear = Math.max(startYear, endYear);
let result = [];
for (let year = minYear; year <= maxYear; year++) {
const days = await fetchHolidays(year);
result = result.concat(days);
}
return result;
} catch (error) {
console.error("Error retrieving holidays", error);
return [];
}
}