-
Notifications
You must be signed in to change notification settings - Fork 1
/
hollydates.js
315 lines (250 loc) · 7.3 KB
/
hollydates.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
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
(function(window){
window.HollyDates = function() {
var DAYS_IN_WEEK = 7;
var SUN = 1,
MON = 2,
TUE = 3,
WED = 4,
THU = 5,
FRI = 6,
SAT = 7;
var JAN = 1,
FEB = 2,
MAR = 3,
APR = 4,
MAY = 5,
JUN = 6,
JUL = 7,
AUG = 8,
SEP = 9,
OCT = 10,
NOV = 11,
DEC = 12;
var FIRST = 1,
SECOND = 2,
THIRD = 3,
FOURTH = 4,
LAST = -1;
var CUSTOM_DATES = {
"New Year's Day": [JAN, 1],
"Washington's Birthday": [FEB, 17],
"Independence Day": [JUL, 4],
"Veterans Day": [NOV, 11],
"Christmas": [DEC, 25],
}
var HOLIDAYS = {
"Martin Luther King, Jr. Day": [THIRD, MON, JAN],
"Memorial Day": [LAST, MON, MAY],
"Labor Day": [FIRST, MON, SEP],
"Columbus Day": [SECOND, MON, OCT],
"Thanksgiving Day": [LAST, THU, NOV]
}
var CALLBACKS = {}
var reset = function()
{
CUSTOM_DATES = {};
HOLIDAYS = {};
CALLBACKS = {};
}
var onMatch = function(holiday)
{
return function(callBack)
{
CALLBACKS[holiday] = callBack;
}
}
var trigger = function(date)
{
if(typeof date == 'undefined')
{
date = new Date();
}
var holiday;
if((holiday = getRegisteredOccurrence(date)) != null)
{
return runCallback(holiday, date);
}
if((holiday = getRegisteredDate(date)) != null)
{
return runCallback(holiday, date);
}
return false;
}
var runCallback = function(holiday, date)
{
if(typeof CALLBACKS[holiday] == 'function')
{
CALLBACKS[holiday](holiday, date);
return true;
}
return false;
}
var addByOccurrence = function(holiday, occurrence, weekDay, month)
{
HOLIDAYS[holiday] = [occurrence, weekDay, month];
return {'onMatch' : onMatch(holiday)};
}
var addByDate = function(customDate, month, date)
{
CUSTOM_DATES[customDate] = [month, date];
return {'onMatch' : onMatch(customDate)};
}
var getMonthAWeekFromDate = function(date) {
var newDate = new Date(date);
newDate.setDate(date.getDate() + DAYS_IN_WEEK);
return newDate.getMonth() + 1;
}
var isDate = function(date, month, day) {
if (date.getMonth() != month - 1) {
return false;
}
if (date.getDate() != day) {
return false;
}
return true;
}
var isDay = function(date, occurrence, weekDay, month) {
if (date.getMonth() != month - 1) {
return false;
}
if (date.getDay() != weekDay - 1) {
return false;
}
if (occurrence == Math.ceil(date.getDate() / DAYS_IN_WEEK)) {
return true;
}
if (occurrence != LAST) {
return false;
}
return month != getMonthAWeekFromDate(date);
}
var getRegisteredOccurrence = function(date) {
var results = Object.keys(HOLIDAYS).filter(function(val, index) {
var occurrence = HOLIDAYS[val][0];
var weekDay = HOLIDAYS[val][1];
var month = HOLIDAYS[val][2];
if (isDay(date, occurrence, weekDay, month)) {
return true;
}
return false;
});
if (results) {
return results[0];
}
return null;
}
var getRegisteredDate = function(date) {
var results = Object.keys(CUSTOM_DATES).filter(function(val, index) {
var month = CUSTOM_DATES[val][0];
var day = CUSTOM_DATES[val][1];
if (isDate(date, month, day)) {
return true;
}
return false;
});
if (results) {
return results[0];
}
return null;
}
var isWeekend = function(date)
{
if(typeof date == 'undefined')
{
date = new Date();
}
if(date.getDay() == SAT -1)
{
return true;
}
if(date.getDay() == SUN -1)
{
return true;
}
return false;
}
var isHoliday = function(date) {
if(typeof date == 'undefined')
{
date = new Date();
}
if (getRegisteredOccurrence(date)) {
return true;
}
if (getRegisteredDate(date)) {
return true;
}
return false;
}
var getHoliday = function(date)
{
if(typeof date == 'undefined')
{
date = new Date();
}
var holiday;
if (( holiday = getRegisteredOccurrence(date)) != null) {
return holiday;
}
if (( holiday = getRegisteredDate(date)) != null) {
return holiday;
}
return null;
}
var getRegisteredOccurrences = function()
{
return HOLIDAYS;
}
var getCustomDates = function()
{
return CUSTOM_DATES;
}
var getCallBacks = function()
{
return CALLBACKS;
}
return {
'isHoliday': isHoliday,
'isWeekend': isWeekend,
'addByOccurrence': addByOccurrence,
'addByDate': addByDate,
'getHoliday': getHoliday,
'trigger': trigger,
'reset': reset,
'SUN': 1,
'MON': 2,
'TUE': 3,
'WED': 4,
'THU': 5,
'FRI': 6,
'SAT': 7,
'JAN': 1,
'FEB': 2,
'MAR': 3,
'APR': 4,
'MAY': 5,
'JUN': 6,
'JUL': 7,
'AUG': 8,
'SEP': 9,
'OCT': 0,
'NOV': 11,
'DEC': 12,
'FIRST': 1,
'SECOND': 2,
'THIRD': 3,
'FOURTH': 4,
'LAST': -1,
'_': {
'getRegisteredOccurrences': getRegisteredOccurrences,
'getCustomDates': getCustomDates,
'getCallBacks': getCallBacks,
'getRegisteredDate': getRegisteredDate,
'getRegisteredOccurrence': getRegisteredOccurrence,
'isDay': isDay,
'isDate': isDate,
'getMonthAWeekFromDate': getMonthAWeekFromDate
}
}
}
})(window);