-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
324 lines (300 loc) · 7.74 KB
/
index.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import moment = require('moment');
export = moment;
const allStates: Array<string> = [
'BW',
'BY',
'BE',
'BB',
'HB',
'HH',
'HE',
'MV',
'NI',
'NW',
'RP',
'SL',
'SN',
'ST',
'SH',
'TH',
];
const allHolidays: YearWithHolidays = {};
interface IsHolidayResult {
allStates: boolean;
holidayName: string;
holidayStates: Array<string>;
testedStates: Array<string>;
}
interface Holiday {
date: moment.Moment;
state: Array<string>;
}
interface Holidays {
[key: string]: Holiday;
}
interface YearWithHolidays {
[key: string]: Holidays;
}
declare module 'moment' {
export interface Moment {
isHoliday: (
_states?: string | Array<string>
) => boolean | string | IsHolidayResult;
}
export function getHolidaysByYear(_year: number): Holidays;
export function getAllStateCodes(): Array<string>;
}
const getAllStateCodes = function(): Array<string> {
return allStates;
};
const isHoliday = function(
this: moment.Moment,
_states?: string | Array<string>
): boolean | string | IsHolidayResult {
const _moment = this;
// calculate holidays if needed
if (
!Object.prototype.hasOwnProperty.call(
allHolidays,
_moment.year().toString()
)
) {
allHolidays[_moment.year().toString()] = calculateHolidays(_moment.year());
}
// call backwards compatible function
if (typeof _states === 'string' || !_states) {
return _isHoliday106(_moment, _states);
}
// return IsHolidayResult if _states parma is Array
if (Array.isArray(_states)) {
return _isHoliday(_moment, _states);
}
// TODO: return error
return false;
};
const getHolidaysByYear = (_year: number): Holidays => {
if (!Object.prototype.hasOwnProperty.call(allHolidays, _year.toString())) {
return calculateHolidays(_year);
}
return allHolidays[_year.toString()];
};
// add custom functions to moment
(moment as any).fn.isHoliday = isHoliday;
(moment as any).getHolidaysByYear = getHolidaysByYear;
(moment as any).getAllStateCodes = getAllStateCodes;
const _isHoliday106 = (
_moment: moment.Moment,
_state?: string
): string | boolean => {
if (
Object.prototype.hasOwnProperty.call(
allHolidays,
_moment.year().toString()
) &&
allHolidays[_moment.year().toString()]
) {
const holidays = allHolidays[_moment.year().toString()];
for (const h in holidays) {
// test if moment is holiday
if (_moment.isSame(holidays[h].date)) {
// return name if all states celebrate this holiday
if (holidays[h].state.length === 0) {
return h;
} else {
// return name if passed state celebrates holiday
if (_state && holidays[h].state.indexOf(_state) > -1) {
return h;
} else {
// return false if passed state does not celebrate holiday
return false;
}
}
}
}
}
return false;
};
const _isHoliday = (
_moment: moment.Moment,
_states: Array<string>
): IsHolidayResult => {
const result: IsHolidayResult = {
allStates: false,
holidayName: '',
holidayStates: [],
testedStates: [],
};
// test in all states if _states is empty
if (_states && _states.length < 1) {
result.testedStates = allStates;
} else {
// validate state codes from params
const validStateCodes = validateStateCodes(_states);
result.testedStates = validStateCodes;
}
// test multiple state codes
for (const s of result.testedStates) {
const holiday = _isHoliday106(_moment, s);
if (holiday && typeof holiday === 'string') {
result.holidayStates.push(s);
result.holidayName = holiday;
}
}
// test for nation wide holiday
const nationHoliday = _isHoliday106(_moment);
if (nationHoliday && typeof nationHoliday === 'string') {
result.allStates = true;
result.holidayName = nationHoliday;
}
return result;
};
const validateStateCodes = (_states: Array<string>): Array<string> => {
// sort out false values
let validCodes: Array<string> = [];
for (const s of _states) {
if (s && allStates.indexOf(s) > -1) {
validCodes.push(s);
}
}
// remove duplicates
validCodes = validCodes.filter((item, i) => validCodes.indexOf(item) === i);
return validCodes;
};
const calculateHolidays = (year: number): Holidays => {
const easter = calculateEasterDate(year);
const holidays: Holidays = {
Neujahrstag: {
date: moment(`${year}-01-01`),
state: [],
},
'Heilige Drei Könige': {
date: moment(`${year}-01-06`),
state: ['BW', 'BY', 'ST'],
},
Karfreitag: {
date: moment(`${easter}`).subtract(2, 'days'),
state: [],
},
Ostersonntag: {
date: moment(`${easter}`),
state: ['BB'],
},
Ostermontag: {
date: moment(`${easter}`).add(1, 'days'),
state: [],
},
Maifeiertag: {
date: moment(`${year}-05-01`),
state: [],
},
'Christi Himmelfahrt': {
date: moment(`${easter}`).add(39, 'days'),
state: [],
},
Pfingstsonntag: {
date: moment(`${easter}`).add(49, 'days'),
state: ['BB'],
},
Pfingstmontag: {
date: moment(`${easter}`).add(50, 'days'),
state: [],
},
Fronleichnam: {
date: moment(`${easter}`).add(60, 'days'),
state: ['BW', 'BY', 'HE', 'NW', 'RP', 'SL'],
},
'Mariä Himmelfahrt': {
date: moment(`${year}-08-15`),
state: ['SL'],
},
'Tag der deutschen Einheit': {
date: moment(`${year}-10-03`),
state: [],
},
Reformationstag: {
date: moment(`${year}-10-31`),
state: ['BB', 'MV', 'SN', 'ST', 'TH'],
},
Allerheiligen: {
date: moment(`${year}-11-01`),
state: ['BW', 'BY', 'NW', 'RP', 'SL'],
},
'Buß- und Bettag': {
date: calculateBandBDate(year),
state: ['SN'],
},
'1. Weihnachtsfeiertag': {
date: moment(`${year}-12-25`),
state: [],
},
'2. Weihnachtsfeiertag': {
date: moment(`${year}-12-26`),
state: [],
},
};
// EXCEPTIONS and ADDITIONS
// 2017 Reformationstag is holiday in all states
if (+year === 2017) {
holidays['Reformationstag'].state = [];
}
// since 2018 HB, HH, NI, SH celebrate 'Reformationstag' as well
if (+year > 2017) {
holidays['Reformationstag'].state = [
'BB',
'HB',
'HH',
'MV',
'NI',
'SN',
'ST',
'SH',
'TH',
];
}
// since 2019 new holidays
if (+year > 2018) {
holidays['Internationaler Frauentag'] = {
date: moment(`${year}-03-08`),
state: ['BE'],
};
holidays['Weltkindertag'] = {
date: moment(`${year}-09-20`),
state: ['TH'],
};
}
// one time only holiday in Berlin
if (year == 2020) {
holidays['Tag der Befreiung'] = {
date: moment(`${year}-05-08`),
state: ['BE'],
};
}
return holidays;
};
const calculateEasterDate = (Y: number): string => {
const C = Math.floor(Y / 100);
const N = Y - 19 * Math.floor(Y / 19);
const K = Math.floor((C - 17) / 25);
let I = C - Math.floor(C / 4) - Math.floor((C - K) / 3) + 19 * N + 15;
I = I - 30 * Math.floor(I / 30);
// prettier-ignore
I = I - Math.floor(I / 28) * (1 - Math.floor(I / 28) * Math.floor(29 / (I + 1)) * Math.floor((21 - N) / 11));
let J = Y + Math.floor(Y / 4) + I + 2 - C + Math.floor(C / 4);
J = J - 7 * Math.floor(J / 7);
const L = I - J;
const M = 3 + Math.floor((L + 40) / 44);
const D = L + 28 - 31 * Math.floor(M / 4);
return `${Y}-${padout(M)}-${padout(D)}`;
};
const padout = (num: number): string => {
return num < 10 ? `0${num}` : `${num}`;
};
const calculateBandBDate = function(year: number): moment.Moment {
for (let i = 1; i < 8; i++) {
const day = moment(`${year}-11-23`).subtract(i, 'days');
if (day.isoWeekday() === 3) {
return day;
}
}
return moment.invalid();
};