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