import * as hebrewDateN from 'hebrew-date' ; import * as heDate from 'hebcal' ; import gematriya from 'gematriya' ; import * as Dayjs from 'dayjs' ; import { JewishDate , JewishDay , JewishMonthInfo , JewishMonth , BasicJewishMonthInfo , IdText , BasicJewishDate } from './interfaces' ; const dayjs = Dayjs . default ; const HeDate = heDate . default ; const hebrewDate = hebrewDateN . default ; export function isValidDate ( date ) { return date && Object . prototype . toString . call ( date ) === "[object Date]" ; } export const isMeubar = ( year ) : boolean => { const yearindex = year % 19 ; return ( yearindex === 0 || yearindex === 3 || yearindex === 6 || yearindex === 8 || yearindex === 11 || yearindex === 14 || yearindex === 17 ) ; } ; export const getHebWeekdays = ( ) : string [ ] => { return [ 'א' , 'ב' , 'ג' , 'ד' , 'ה' , 'ו' , 'ש' ] ; } ; export const getEngWeekdays = ( ) : string [ ] => { return [ 'Su' , 'Mo' , 'Tu' , 'We' , 'Th' , 'Fr' , 'Sa' ] ; } ; export const getWeekdays = ( isHebrew ) : string [ ] => { return isHebrew ? getHebWeekdays ( ) : getEngWeekdays ( ) ; } ; export const convertToHebrew = ( num , addGeresh = true , addPunctuate = true ) : string => { return gematriya ( num , { geresh : addGeresh , punctuate : addPunctuate } ) ; } ; export const getHebJewishMonthById = ( monthId ) : string => { const months = getHebJewishMonths ( ) ; const monthIndex = months . map ( ( month ) => month . id ) . indexOf ( monthId ) ; return months [ monthIndex ] . text ; } ; export const getHebJewishMonths = ( ) : IdText [ ] => { return [ { id : 'Tishri' , text : 'תשרי' , } , { id : 'Heshvan' , text : 'חשון' , } , { id : 'Kislev' , text : 'כסלו' , } , { id : 'Tevet' , text : 'טבת' , } , { id : 'Shevat' , text : 'שבט' , } , { id : 'AdarI' , text : 'אדר א' , } , { id : 'AdarII' , text : 'אדר ב' , } , { id : 'Nisan' , text : 'ניסן' , } , { id : 'Iyyar' , text : 'אייר' , } , { id : 'Sivan' , text : 'סיון' , } , { id : 'Tammuz' , text : 'תמוז' , } , { id : 'Av' , text : 'אב' , } , { id : 'Elul' , text : 'אלול' } ] ; } ; export const getEngJewishMonths = ( ) : IdText [ ] => { return [ { id : 'Tishri' , text : 'Tishri' , } , { id : 'Heshvan' , text : 'Heshvan' , } , { id : 'Kislev' , text : 'Kislev' , } , { id : 'Tevet' , text : 'Tevet' , } , { id : 'Shevat' , text : 'Shevat' , } , { id : 'AdarI' , text : 'AdarI' , } , { id : 'AdarII' , text : 'AdarII' , } , { id : 'Nisan' , text : 'Nisan' , } , { id : 'Iyyar' , text : 'Iyyar' , } , { id : 'Sivan' , text : 'Sivan' , } , { id : 'Tammuz' , text : 'Tammuz' , } , { id : 'Av' , text : 'Av' , } , { id : 'Elul' , text : 'Elul' , } ] ; } ; export const getJewishMonths = ( year , isHebrew ? ) : IdText [ ] => { const months = isHebrew ? getHebJewishMonths ( ) : getEngJewishMonths ( ) ; if ( isMeubar ( year ) ) { return months ; } else { return months . filter ( month => month . id !== 'AdarII' ) . map ( month => { if ( month . id == 'AdarI' ) { return { id : 'AdarI' , text : isHebrew ? 'אדר' : 'Adar' , } ; } else { return month ; } } ) ; } } ; export const getJewishYears = ( year = 5780 ) : number [ ] => { const years = [ ] ; for ( let i = 100 ; i > 0 ; i -- ) { const element = year - i ; years . push ( element ) ; } years . push ( year ) ; for ( let i = 1 ; i <= 100 ; i ++ ) { const element = year + i ; years . push ( element ) ; } return years ; } ; export const getPrevMonth = ( basicJewishMonthInfo ) : BasicJewishMonthInfo => { const result = { ... basicJewishMonthInfo } ; const months = getJewishMonths ( basicJewishMonthInfo . year , basicJewishMonthInfo . isHebrew ) ; const monthIndex = months . map ( ( month ) => month . id ) . indexOf ( basicJewishMonthInfo . month ) ; if ( monthIndex !== - 1 ) { if ( monthIndex === 0 ) { result . month = months [ months . length - 1 ] . id ; result . year -- ; } else { result . month = months [ monthIndex - 1 ] . id ; } } return result ; } ; export const getNextMonth = ( basicJewishMonthInfo ) : BasicJewishMonthInfo => { const result = { ... basicJewishMonthInfo } ; const months = getJewishMonths ( basicJewishMonthInfo . year ) ; const monthIndex = months . map ( ( month ) => month . id ) . indexOf ( basicJewishMonthInfo . month ) ; if ( monthIndex !== - 1 ) { if ( monthIndex === ( months . length - 1 ) ) { result . month = months [ 0 ] . id ; result . year ++ ; } else { result . month = months [ monthIndex + 1 ] . id ; } } return result ; } ; export const getGregDate = ( props ) : Date => { if ( ! props || props . monthName === '' || props . year < 1 || props . day < 1 ) { return new Date ( ) ; } if ( props . monthName === 'Heshvan' ) { props . monthName = 'Cheshvan' ; } const day = new HeDate . HDate ( props . day , props . monthName , props . year ) ; return day . greg ( ) ; } ; export const getJewishMonthInfo = ( date ) : JewishMonthInfo => { const jewishDate = getJewishDate ( date ) ; const startOfJewishMonth = dayjs ( date ) . subtract ( jewishDate . day - 1 , 'day' ) ; const dayOfWeek = Number ( startOfJewishMonth . format ( "d" ) ) ; const sundayStartOfTheMonth = startOfJewishMonth . subtract ( dayOfWeek , 'day' ) ; return { jewishDate , jewishMonth : jewishDate . month , startOfJewishMonth , sundayStartOfTheMonth } ; } ; export const formatJewishDate = ( jewishDate ) : string => { return `${ jewishDate . day } ${ jewishDate . monthName } ${ jewishDate . year }` ; } ; export const formatJewishDateHebrew = ( jewishDate ) : string => { return `${ convertToHebrew ( jewishDate . day ) } ${ getHebJewishMonthById ( jewishDate . monthName ) } ${ convertToHebrew ( jewishDate . year ) }` ; } ; export const getJewishDate = ( date ) : JewishDate => { const hebDate = hebrewDate ( date ) ; return { year : hebDate . year , month : hebDate . month , day : hebDate . date , monthName : hebDate . month_name } ; } ; export const IsJewishDatesEqual = ( jewishDate1 , jewishDate2 ) : boolean => { return jewishDate1 && jewishDate2 && jewishDate1 . day === jewishDate2 . day && jewishDate1 . month === jewishDate2 . month && jewishDate1 . year === jewishDate2 . year ; } ; export const getJewishMonth = ( date ) : JewishMonth => { const jewishMonthInfo = getJewishMonthInfo ( date ) ; const jewishMonth = { selectedDay : null , jewishMonth : jewishMonthInfo . jewishMonth , jewishYear : jewishMonthInfo . jewishDate . year , jewishMonthString : jewishMonthInfo . jewishDate . monthName , days : [ ] } ; let currentDate = jewishMonthInfo . sundayStartOfTheMonth ; for ( let i = 0 ; i < 42 ; i ++ ) { const jewishDate = getJewishDate ( currentDate . toDate ( ) ) ; const day = { day : jewishDate . day , jewishDateStr : formatJewishDate ( jewishDate ) , jewishDateStrHebrew : formatJewishDateHebrew ( jewishDate ) , jewishDate : jewishDate , dayjsDate : currentDate , date : currentDate . toDate ( ) , isCurrentMonth : jewishMonth . jewishMonth === jewishDate . month } ; if ( IsJewishDatesEqual ( jewishMonthInfo . jewishDate , jewishDate ) ) { jewishMonth . selectedDay = day ; } if ( i < 7 || day . isCurrentMonth || day . date . getDay ( ) > 0 ) { jewishMonth . days . push ( day ) ; currentDate = currentDate . add ( 1 , 'day' ) ; } } return jewishMonth ; } ;