forked from bunkat/later
-
Notifications
You must be signed in to change notification settings - Fork 0
/
later-recur.min.js
1 lines (1 loc) · 13.6 KB
/
later-recur.min.js
1
if(!Array.prototype.indexOf){Array.prototype.indexOf=function(searchElement){"use strict";if(this==null){throw new TypeError}var t=Object(this);var len=t.length>>>0;if(len===0){return-1}var n=0;if(arguments.length>1){n=Number(arguments[1]);if(n!=n){n=0}else if(n!=0&&n!=Infinity&&n!=-Infinity){n=(n>0||-1)*Math.floor(Math.abs(n))}}if(n>=len){return-1}var k=n>=0?n:Math.max(len-Math.abs(n),0);for(;k<len;k++){if(k in t&&t[k]===searchElement){return k}}return-1}}if(!String.prototype.trim){String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}}(function(){"use strict";var Later=function(resolution,useLocalTime){var isLocal=useLocalTime||false,get="get"+(isLocal?"":"UTC"),exec=true,SEC=1e3,MIN=SEC*60,HOUR=MIN*60,DAY=HOUR*24,ceil=Math.ceil,floor=Math.floor,max=Math.max,dateProto=Date.prototype,getYear=dateProto[get+"FullYear"],getMonth=dateProto[get+"Month"],getDate=dateProto[get+"Date"],getDay=dateProto[get+"Day"],getHour=dateProto[get+"Hours"],getMin=dateProto[get+"Minutes"],getSec=dateProto[get+"Seconds"];resolution=resolution===undefined?1:resolution;var nextInRange=function(val,values,minOffset){var cur,next=null,min=values[0],i=values.length;while(i--){cur=values[i];if(cur===val){return val}min=cur<min?cur:min;next=cur>val&&(!next||cur<next)?cur:next}return next||(minOffset===undefined?min:min+minOffset)};var prevInRange=function(val,values,maxOffset){var cur,prev=null,i=values.length,max=values[i-1];while(i--){cur=values[i];if(cur===val){return val}max=cur>max?cur:max;prev=cur<val&&(!prev||cur>prev)?cur:prev}return prev!==null?prev:maxOffset===undefined?max:max-maxOffset};var nextDate=function(yr,mt,dt,hr,mn,sc){mt=mt===undefined?0:mt;dt=dt===undefined?1:dt;hr=hr===undefined?0:hr;mn=mn===undefined?0:mn;sc=sc===undefined?0:sc;return isLocal?new Date(yr,mt,dt,hr,mn,sc):new Date(Date.UTC(yr,mt,dt,hr,mn,sc))};var prevDate=function(yr,mt,dt,hr,mn,sc){mt=mt===undefined?11:mt;dt=dt===undefined?getDate.call(nextDate(yr,mt+1,0)):dt;hr=hr===undefined?23:hr;mn=mn===undefined?59:mn;sc=sc===undefined?59:sc;return isLocal?new Date(yr,mt,dt,hr,mn,sc):new Date(Date.UTC(yr,mt,dt,hr,mn,sc))};var pad=function(val){return(val<10?"0":"")+val};var getNextInvalidSchedule=function(sched,start,reverse){var Y,M,D,d,h,m,s,oJan1,oMonthStart,oWeekStart,oWeekStartY,oMonthEnd,oDec31,t,dy,wy,wm,dc,daysInYear,daysInMonth,firstDayOfMonth,weekStart,weeksInYear,weeksInMonth,x;var range=reverse?prevInRange:nextInRange,date=reverse?prevDate:nextDate;Y=getYear.call(start);M=getMonth.call(start);D=getDate.call(start);h=getHour.call(start);m=getMin.call(start);s=getSec.call(start);t=pad(h)+":"+pad(m)+":"+pad(s);if(sched.t&&range(t,sched.t)===t){return reverse?date(Y,M,D,h,m,s-1):date(Y,M,D,h,m,s+1)}if(sched.s&&range(s,sched.s,60)===s){return reverse?date(Y,M,D,h,m,s-1):date(Y,M,D,h,m,s+1)}if(sched.m&&range(m,sched.m,60)===m){return reverse?date(Y,M,D,h,m-1):date(Y,M,D,h,m+1)}if(sched.h&&range(h,sched.h,24)===h){return reverse?date(Y,M,D,h-1):date(Y,M,D,h+1)}if(sched.tb){if(t<sched.tb[0]){x=sched.tb[0].split(":");if(reverse){return date(Y,M,D-1,x[0],x[1],x[2]-1)}else{return date(Y,M,D,x[0],x[1],x[2])}}}if(sched.ta){if(t>=sched.ta[0]){if(reverse){x=sched.ta[0].split(":");return date(Y,M,D,x[0],x[1],x[2]-1)}else{return date(Y,M,D+1)}}}if(sched.dc){dc=floor((D-1)/7)+1;if(range(dc,sched.dc,0)===dc){return reverse?date(Y,M,D-1):date(Y,M,D+1)}}d=getDay.call(start);if(sched.d&&range(d+1,sched.d,7)===d+1){return reverse?date(Y,M,D-1):date(Y,M,D+1)}if(sched.wm){firstDayOfMonth=getDay.call(nextDate(Y,M,1));wm=floor((D+firstDayOfMonth-1)/7+1);weeksInMonth=floor((daysInMonth+firstDayOfMonth-1)/7+1);if((range(wm,sched.wm,weeksInMonth)||weeksInMonth)===wm){return reverse?date(Y,M,D-1):date(Y,M,D+1)}}oMonthEnd=nextDate(Y,M+1,0);daysInMonth=getDate.call(oMonthEnd);if(sched.D&&(range(D,sched.D,daysInMonth)||daysInMonth)===D){return reverse?date(Y,M,D-1):date(Y,M,D+1)}oJan1=nextDate(Y,0,1,12);oDec31=nextDate(Y+1,0,0,12);if(sched.wy){oWeekStart=date(Y,M,D+4-(d||7));oWeekStartY=date(getYear.call(oWeekStart),0,1);weeksInYear=getDay.call(oJan1)===4||getDay.call(oDec31)===4?53:52;wy=ceil(((oWeekStart.getTime()-oWeekStartY.getTime())/DAY+1)/7);if((range(wy,sched.wy,weeksInYear)||weeksInYear)===wy){return reverse?date(Y,M,D-1):date(Y,M,D+1)}}if(sched.M&&range(M+1,sched.M,12)===M+1){return reverse?date(Y,M-1):date(Y,M+1)}if(sched.dy){dy=ceil((start.getTime()-oJan1.getTime())/DAY)+1;daysInYear=ceil((oDec31.getTime()-oJan1.getTime())/DAY)+1;if((range(dy,sched.dy,daysInYear)||daysInYear)===dy){return reverse?date(Y,M-1):date(Y,M+1)}}if(sched.Y&&range(Y,sched.Y,0)===Y){return reverse?date(Y-1):date(Y+1)}return start};var getNextForSchedule=function(sched,start,end,reverse){var next,inc,x,cur,Y,M,D,d,h,m,s,oJan1,oMonthStart,oWeekStart,oWeekStartY,oMonthEnd,oDec31,t,dy,wy,wm,dc,daysInYear,daysInMonth,firstDayOfMonth,weekStart,weeksInYear,weeksInMonth,maxLoopCount=1e3;var range=reverse?prevInRange:nextInRange,date=reverse?prevDate:nextDate;next=reverse?start:after(start,sched);while(next&&maxLoopCount--){if(end&&(!reverse&&next.getTime()>end.getTime()||reverse&&next.getTime()<end.getTime())){return null}Y=getYear.call(next);M=getMonth.call(next);D=getDate.call(next);if(sched.Y&&(inc=range(Y,sched.Y,0))!==Y){next=!reverse&&inc>Y||reverse&&inc<Y?date(inc):null;continue}oJan1=nextDate(Y,0,1,12,0,0);var cDate=nextDate(Y,M,D,12,0,0);oDec31=nextDate(Y+1,0,0);if(sched.dy){dy=ceil((cDate.getTime()-oJan1.getTime())/DAY)+1;daysInYear=ceil((oDec31.getTime()-oJan1.getTime())/DAY)+1;if(((inc=range(dy,sched.dy,daysInYear))||daysInYear)!==dy){next=date(Y,0,inc);continue}}if(sched.M&&(inc=range(M+1,sched.M,12))!==M+1){next=date(Y,inc-1);continue}d=getDay.call(next);if(sched.wy){oWeekStart=date(Y,M,D+4-(d||7),12);oWeekStartY=date(getYear.call(oWeekStart),0,1,12);weeksInYear=getDay.call(oJan1)===4||getDay.call(oDec31)===4?53:52;wy=ceil(((oWeekStart.getTime()-oWeekStartY.getTime())/DAY+1)/7);if(((inc=range(wy,sched.wy,weeksInYear))||weeksInYear)!==wy){if(inc<0)inc+=1;next=date(getYear.call(oWeekStart),getMonth.call(oWeekStart),getDate.call(oWeekStart)-3+(inc-wy)*7+(reverse?6:0));continue}}oMonthEnd=nextDate(Y,M+1,0,12);daysInMonth=getDate.call(oMonthEnd);if(sched.D&&((inc=range(D,sched.D,daysInMonth))||daysInMonth)!==D){next=date(Y,M,inc<0?0:inc);continue}if(sched.wm){firstDayOfMonth=getDay.call(nextDate(Y,M,1));wm=floor((D+firstDayOfMonth-1)/7+1);weeksInMonth=floor((daysInMonth+firstDayOfMonth-1)/7+1);if(((inc=range(wm,sched.wm,weeksInMonth))||weeksInMonth)!==wm){if(inc===0){next=date(Y,M,inc)}else{next=date(Y,M,(inc<0?inc+1:inc-1)*7-(firstDayOfMonth-1)-(inc>weeksInMonth&&getDay.call(oMonthEnd)<6?7:0)+(inc===weeksInMonth+1?getDay.call(oMonthEnd)+1:0)+(reverse?6:0))}continue}}if(sched.d&&(inc=range(d+1,sched.d,7))!==d+1){next=date(Y,M,D+(inc-1)-d);continue}if(sched.dc){dc=floor((D-1)/7)+1;if((inc=range(dc,sched.dc,0))!==dc){if(reverse){if(inc>0){next=date(Y,M+(inc<dc?0:-1),7+7*(inc-1));continue}else if(inc===0&&D<daysInMonth-6){next=date(Y,M,inc);continue}}else{if(inc>0){next=date(Y,M+(inc<dc?1:0),1+7*(inc-1));continue}else if(inc<1&&D<daysInMonth-6){next=date(Y,M,daysInMonth-6);continue}}}}h=getHour.call(next);m=getMin.call(next);s=getSec.call(next);t=pad(h)+":"+pad(m)+":"+pad(s);if(sched.ta){if(t<sched.ta[0]){if(reverse){next=date(Y,M,D-1)}else{x=sched.ta[0].split(":");next=date(Y,M,D,x[0],x[1],x[2])}continue}}if(sched.tb){if(t>=sched.tb[0]){if(reverse){x=sched.tb[0].split(":");next=date(Y,M,D,x[0],x[1],x[2]-1)}else{next=date(Y,M,D+1)}continue}}if(sched.h&&(inc=range(h,sched.h,24))!==h){next=date(Y,M,D,inc);continue}if(sched.m&&(inc=range(m,sched.m,60))!==m){next=date(Y,M,D,h,inc);continue}if(sched.s&&(inc=range(s,sched.s,60))!==s){next=date(Y,M,D,h,m,inc);continue}if(sched.t){if((inc=range(t,sched.t))!==t){x=inc.split(":");var dayInc=!reverse?t>inc?1:0:t<inc?-1:0;next=date(Y,M,D+dayInc,x[0],x[1],x[2]);continue}}break}return maxLoopCount>0?next:null};var after=function(start,sched){var yr=getYear.call(start)+getAfter(sched.aY),mt=getMonth.call(start)+getAfter(sched.aM),dt=getDate.call(start)+max(getAfter(sched.aD),getAfter(sched.ady),getAfter(sched.ad),getAfter(sched.awy)*7,getAfter(sched.awm)*7),hr=getHour.call(start)+getAfter(sched.ah),mn=getMin.call(start)+getAfter(sched.am),sc=getSec.call(start)+getAfter(sched.as);return nextDate(yr,mt,dt,hr,mn,sc)};var getAfter=function(constraint){return constraint&&constraint[0]?constraint[0]:0};var tick=function(date,backwards){return!backwards?new Date(date.getTime()+resolution*1e3):new Date(date.getTime()-resolution*1e3)};return{isValid:function(recur,date){date.setMilliseconds(0);var next=this.getNext(recur,date);return next?date.getTime()===next.getTime():false},get:function(recur,count,startDate,endDate,reverse){var occurrences=[],date;while(count-->0&&(date=!reverse?this.getNext(recur,date||startDate,endDate):this.getPrevious(recur,date||startDate,endDate))){occurrences.push(date);date=tick(date,reverse)}return occurrences},getNext:function(recur,startDate,endDate){var schedules=recur?recur.schedules||[]:[],exceptions=recur?recur.exceptions||[]:[],schedLen=schedules.length,exceptLen=exceptions.length,start=startDate||new Date,date;while(start){var tDate;if(endDate&&start.getTime()>endDate.getTime()){date=undefined;break}if(schedLen){for(var i=0;i<schedLen;i++){tDate=getNextForSchedule(schedules[i],start,endDate);if(tDate&&(!date||tDate.getTime()<date.getTime())){date=tDate}}}else{date=start}start=null;if(date&&exceptLen){tDate=this.getNextInvalid({schedules:exceptions},date);if(tDate.getTime()!==date.getTime()){start=tDate;date=undefined}}}return date},getNextInvalid:function(recur,startDate){var schedules=recur?recur.schedules||[]:[],exceptions=recur?recur.exceptions||[]:[],schedLen=schedules.length,exceptLen=exceptions.length,start=startDate||new Date;while(start&&this.isValid(recur,start)){var nextExcep,nextInvalid;for(var i=0;i<schedLen;i++){var tDate=getNextInvalidSchedule(schedules[i],start);if(tDate&&(!nextInvalid||tDate.getTime()>nextInvalid.getTime())){nextInvalid=tDate}}if(exceptLen){nextExcep=this.getNext({schedules:exceptions},start)}if(!nextInvalid&&!nextExcep){start=undefined}else{start=new Date(nextInvalid&&nextExcep?Math.min(nextInvalid.getTime(),nextExcep.getTime()):nextExcep||nextInvalid)}}return start},getPrevious:function(recur,startDate,endDate){var schedules=recur?recur.schedules||[]:[],exceptions={schedules:recur?recur.exceptions||[]:[]},end=startDate||new Date,date,tDate,i=schedules.length;if(endDate&&startDate.getTime()<endDate.getTime()){return null}if(i===0){date=end}else{while(i--){tDate=getNextForSchedule(schedules[i],end,endDate,true);if(!date||tDate>date){date=tDate}}}if(date&&exceptions.schedules.length>0&&this.isValid(exceptions,date)){date=this.getPrevious(recur,tick(date,true),endDate)}return date},exec:function(recur,startDate,callback,arg){var next=this.getNext(recur,tick(startDate));if(next){next=next.getTime()-(new Date).getTime();if(next>2147483647){exec=setTimeout(this.exec,2147483647,recur,startDate,callback,arg)}else{exec=setTimeout(this.handleExec,next,this,recur,callback,arg)}return true}},stopExec:function(){if(exec){clearTimeout(exec);exec=false}},handleExec:function(later,recur,callback,arg){callback(arg);if(exec){later.exec(recur,new Date,callback,arg)}}}};var root=typeof exports!=="undefined"&&exports!==null?exports:window;root.later=Later}).call(this);(function(){"use strict";var Recur=function(){var schedules=[],exceptions=[],cur,curArr=schedules,curName,values,every,after,applyMin,applyMax,i,last;var add=function(name,min,max){name=after?"a"+name:name;if(!cur){curArr.push({});cur=curArr[0]}if(!cur[name]){cur[name]=[]}curName=cur[name];if(every){values=[];for(i=min;i<=max;i+=every){values.push(i)}last={n:name,x:every,c:curName.length,m:max}}values=applyMin?[min]:applyMax?[max]:values;var length=values.length;for(i=0;i<length;i+=1){if(curName.indexOf(values[i])<0){curName.push(values[i])}}values=every=after=applyMin=applyMax=0};return{schedules:schedules,exceptions:exceptions,on:function(){values=arguments[0]instanceof Array?arguments[0]:arguments;return this},every:function(x){every=x;return this},after:function(x){after=true;values=[x];return this},first:function(){applyMin=1;return this},last:function(){applyMax=1;return this},at:function(){values=arguments;for(var i=0,len=values.length;i<len;i++){var split=values[i].split(":");if(split.length<3){values[i]+=":00"}}add("t");return this},afterTime:function(){values=arguments;for(var i=0,len=values.length;i<len;i++){var split=values[i].split(":");if(split.length<3){values[i]+=":00"}}add("ta");return this},beforeTime:function(){values=arguments;for(var i=0,len=values.length;i<len;i++){var split=values[i].split(":");if(split.length<3){values[i]+=":00"}}add("tb");return this},second:function(){add("s",0,59);return this},minute:function(){add("m",0,59);return this},hour:function(){add("h",0,23);return this},dayOfMonth:function(){add("D",1,applyMax?0:31);return this},dayOfWeek:function(){add("d",1,7);return this},onWeekend:function(){values=[1,7];return this.dayOfWeek()},onWeekday:function(){values=[2,3,4,5,6];return this.dayOfWeek()},dayOfWeekCount:function(){add("dc",1,applyMax?0:5);return this},dayOfYear:function(){add("dy",1,applyMax?0:366);return this},weekOfMonth:function(){add("wm",1,applyMax?0:5);return this},weekOfYear:function(){add("wy",1,applyMax?0:53);return this},month:function(){add("M",1,12);return this},year:function(){add("Y",1970,2450);return this},startingOn:function(start){return this.between(start,last.m)},between:function(start,end){cur[last.n]=cur[last.n].splice(0,last.c);every=last.x;add(last.n,start,end);return this},and:function(){cur=curArr[curArr.push({})-1];return this},except:function(){curArr=exceptions;cur=null;return this}}};var root=typeof exports!=="undefined"&&exports!==null?exports:window;root.recur=Recur}).call(this);