UNPKG

prayer-timetable-lib

Version:
5 lines 6.29 kB
"use strict";var _dateFns=require("date-fns");Object.defineProperty(exports,"__esModule",{value:!0}),exports.prayersCalc=exports.dayCalc=void 0;// @ts-ignore const isDST=function(d){const jan=new Date(d.getFullYear(),0,1).getTimezoneOffset(),jul=new Date(d.getFullYear(),6,1).getTimezoneOffset();return Math.max(jan,jul)!==d.getTimezoneOffset()},dayCalc=(offsetDay=0,offSetHour=0,hijrioffset=0,city="Europe/Dublin")=>{const now=(0,_dateFns.addHours)(new Date,offSetHour+24*offsetDay),month=(0,_dateFns.getMonth)(now),date=(0,_dateFns.getDate)(now),hijri=(0,_dateFns.addDays)(now,hijrioffset),start=(0,_dateFns.startOfDay)(now),end=(0,_dateFns.endOfDay)(now),dstAdjust=isDST(now)?1:0;return{now,month,date,start,end,hijri,dstAdjust}};exports.dayCalc=dayCalc;const prayerCalc=(hourMinute,hourMinuteNext,index,now,when,jamaahmethods,jamaahoffsets,dstAdjust)=>{const[hour,minute]=hourMinute,[hourNext,minuteNext]=hourMinuteNext;let time=(0,_dateFns.addHours)((0,_dateFns.toDate)(new Date((0,_dateFns.getYear)(now),(0,_dateFns.getMonth)(now),(0,_dateFns.getDate)(now),hour,minute)),dstAdjust);/* *********************** */ /* JAMAAH CALC */ /* *********************** */const hourOffset=jamaahoffsets[index][0],minuteOffset=jamaahoffsets[index][1],jamaahmethod=jamaahmethods[index];let jtime;switch(jamaahmethod){case"afterthis":jtime=(0,_dateFns.addMinutes)(time,60*hourOffset+minuteOffset);break;case"fixed":jtime=(0,_dateFns.toDate)(new Date((0,_dateFns.getYear)(now),(0,_dateFns.getMonth)(now),(0,_dateFns.getDate)(now),hourOffset,minuteOffset));break;case"beforenext":const rawjtime=(0,_dateFns.toDate)(new Date((0,_dateFns.getYear)(now),(0,_dateFns.getMonth)(now),(0,_dateFns.getDate)(now),hourNext,minuteNext));jtime=(0,_dateFns.addMinutes)(rawjtime,60*-hourOffset-minuteOffset+60*dstAdjust);break;default:jtime=time;}// if jtime before adhan / ie. summer time (0,_dateFns.isBefore)(jtime,time)&&(time=jtime);/* *********************** */ /* NAMES */ /* *********************** */const name=["fajr","shurooq","dhuhr","asr","maghrib","isha"][index],hasPassed=(0,_dateFns.isAfter)(now,time),isJamaahPending=(0,_dateFns.isWithinInterval)(now,{start:time,end:jtime}),result={time,isJamaahPending,jtime,index,hasPassed,name,when,dstAdjust,isNext:!1};return result},prayersCalc=(timetable,settings,showJamaah=!0,city="Europe/Dublin")=>{const{hijrioffset,jamaahmethods,jamaahoffsets}=settings,{now,month,date,start,hijri,dstAdjust}=dayCalc(0,0,hijrioffset,city),{now:nowYesterday,month:monthYesterday,date:dateYesterday,dstAdjust:dstAdjustYesterday}=dayCalc(-1,0,hijrioffset,city),{now:nowTomorrow,month:monthTomorrow,date:dateTomorrow,dstAdjust:dstAdjustTomorrow}=dayCalc(1,0,hijrioffset,city),prayersToday=timetable[month+1][date].map((hourMinute,index)=>{const hourMinuteNext=5>index?timetable[month+1][date][index+1]:[24,0];return prayerCalc(hourMinute,hourMinuteNext,index,now,"today",jamaahmethods,jamaahoffsets,dstAdjust)}),prayersYesterday=timetable[monthYesterday+1][dateYesterday].map((hourMinute,index)=>{const hourMinuteNext=5>index?timetable[month+1][date][index+1]:[24,0];return prayerCalc(hourMinute,hourMinuteNext,index,nowYesterday,"yesterday",jamaahmethods,jamaahoffsets,dstAdjustYesterday)}),prayersTomorrow=timetable[monthTomorrow+1][dateTomorrow].map((hourMinute,index)=>{const hourMinuteNext=5>index?timetable[month+1][date][index+1]:[24,0];return prayerCalc(hourMinute,hourMinuteNext,index,nowTomorrow,"tomorrow",jamaahmethods,jamaahoffsets,dstAdjustTomorrow)});/* *********************** */ /* PREVIOUS, CURRENT, NEXT */ /* *********************** */let current,next,previous;(0,_dateFns.isWithinInterval)(now,{start,end:prayersToday[0].time})?(previous=prayersYesterday[4],current=prayersYesterday[5],next=prayersToday[0]):(0,_dateFns.isWithinInterval)(now,{start:prayersToday[0].time,end:prayersToday[1].time})?(previous=prayersYesterday[5],current=prayersToday[0],next=prayersToday[1]):(0,_dateFns.isWithinInterval)(now,{start:prayersToday[1].time,end:prayersToday[2].time})?(previous=prayersToday[0],current=prayersToday[1],next=prayersToday[2]):(0,_dateFns.isWithinInterval)(now,{start:prayersToday[2].time,end:prayersToday[3].time})?(previous=prayersToday[1],current=prayersToday[2],next=prayersToday[3]):(0,_dateFns.isWithinInterval)(now,{start:prayersToday[3].time,end:prayersToday[4].time})?(previous=prayersToday[2],current=prayersToday[3],next=prayersToday[4]):(0,_dateFns.isWithinInterval)(now,{start:prayersToday[4].time,end:prayersToday[5].time})?(previous=prayersToday[3],current=prayersToday[4],next=prayersToday[5]):(previous=prayersToday[4],current=prayersToday[5],next=prayersTomorrow[0]);/* *********************** */ /* COUNTDOWN/UP */ /* *********************** */const countUp={name:current.isJamaahPending||!showJamaah?current.name:`${current.name}${1===current.index?"":" jamaah"}`,time:current.isJamaahPending||!showJamaah?current.time:current.jtime,duration:current.isJamaahPending||!showJamaah?(0,_dateFns.differenceInSeconds)(now,current.time):(0,_dateFns.differenceInSeconds)(now,current.jtime)},countDown={name:current.isJamaahPending&&showJamaah?`${current.name}${1===current.index?"":" jamaah"}`:next.name,time:current.isJamaahPending&&showJamaah?current.jtime:next.time,duration:current.isJamaahPending&&showJamaah?(0,_dateFns.differenceInSeconds)(current.jtime,now)+1:(0,_dateFns.differenceInSeconds)(next.time,now)+1},totalDuration=countUp.duration+countDown.duration,percentageRaw=1e4-1e4*(countDown.duration/totalDuration),percentage=Math.floor(percentageRaw)/100,isAfterIsha=(0,_dateFns.isAfter)(now,prayersToday[5].jtime),isJamaahPending=(0,_dateFns.isWithinInterval)(now,{start:current.time,end:current.jtime}),focus=current.isJamaahPending?current:next;isAfterIsha?prayersTomorrow[focus.index].isNext=!0:prayersToday[focus.index].isNext=!0;// add day if after isha const newNow=isAfterIsha?(0,_dateFns.addDays)(now,1):now,newHijri=isAfterIsha?(0,_dateFns.addDays)(hijri,1):hijri,result={prayers:{today:prayersToday,yesterday:prayersYesterday,tomorrow:prayersTomorrow},previous,current,next,countUp,countDown,now:newNow,hijri:newHijri,trueNow:now,trueHijri:hijri,percentage,isAfterIsha,isJamaahPending,focus};return result};exports.prayersCalc=prayersCalc; //# sourceMappingURL=index.js.map