import { startOfWeek , startOfMonth , endOfWeek , endOfMonth , isBefore , addDays , isSameDay , isWithinInterval , toDate , parseISO , isValid } from "date-fns" ; import { DateRange } from "./types" ; export const identity = < T > ( x ) => x ; export const chunks = < T > ( array , size ) : T [ ] [ ] => { return Array . from ( { length : Math . ceil ( array . length / size ) } , ( v , i ) => array . slice ( i * size , i * size + size ) ) ; } ; export const combine = ( ... args ) : string => args . filter ( identity ) . join ( " " ) ; export const getDaysInMonth = ( date ) => { const startWeek = startOfWeek ( startOfMonth ( date ) ) ; const endWeek = endOfWeek ( endOfMonth ( date ) ) ; const days = [ ] ; for ( let curr = startWeek ; isBefore ( curr , endWeek ) ; ) { days . push ( curr ) ; curr = addDays ( curr , 1 ) ; } return days ; } ; export const isStartOfRange = ( { startDate } , day ) => ( startDate && isSameDay ( day , startDate ) ) as boolean ; export const isEndOfRange = ( { endDate } , day ) => ( endDate && isSameDay ( day , endDate ) ) as boolean ; export const inDateRange = ( { startDate , endDate } , day ) => ( startDate && endDate && ( isWithinInterval ( day , { start : startDate , end : endDate } ) || isSameDay ( day , startDate ) || isSameDay ( day , endDate ) ) ) as boolean ; export const isRangeSameDay = ( { startDate , endDate } ) => { if ( startDate && endDate ) { return isSameDay ( startDate , endDate ) ; } return false ; } ; type Falsy = false | null | undefined | 0 | "" ; export const parseOptionalDate = ( date , defaultValue ) => { if ( date instanceof Date ) { const parsed = toDate ( date ) ; if ( isValid ( parsed ) ) return parsed ; } if ( date instanceof String ) { const parsed = parseISO ( date as string ) ; if ( isValid ( parsed ) ) return parsed ; } return defaultValue ; } ;