import { Week } from "../../../../model/room/Constants" ; import { add , addDays , addMilliseconds , compareDesc , differenceInMilliseconds , eachDayOfInterval , getDay , getHours , getMilliseconds , getMinutes , getSeconds , toDate , } from "date-fns/fp" ; param { Date } start param { Date } end param { Date } endDate param { Week [ ] } weeks returns { DateIntervalResult [ ] } example export const dateIntervalByEndTime = ( { start , end , endDate , weeks , } ) : DateIntervalResult [ ] => { { const result = compareDesc ( endDate ) ( start ) ; if ( result === - 1 ) { throw new Error ( "The periodic end time cannot be less than the creation room begin time" ) ; } if ( result === 0 ) { return [ { start : toDate ( start ) , end : toDate ( end ) , } , ] ; } } const completionTimeInfo = { hours : getHours ( start ) , minutes : getMinutes ( start ) , seconds : getSeconds ( start ) , milliseconds : getMilliseconds ( start ) , } ; const duration = differenceInMilliseconds ( start ) ( end ) ; const allDays = eachDayOfInterval ( { start , end : endDate , } ) ; const result = [ ] ; for ( let i = 0 ; i < allDays . length ; i ++ ) { const date = allDays [ i ] ; if ( weeks . includes ( getDay ( date ) ) ) { const start = addMilliseconds ( completionTimeInfo . milliseconds ) ( add ( completionTimeInfo ) ( date ) ) ; result . push ( { start : start , end : addMilliseconds ( duration ) ( start ) , } ) ; } } return result ; } ; param { Date } start param { Date } end param { number } rate param { Week [ ] } weeks returns { DateIntervalResult [ ] } example export const dateIntervalByRate = ( { start , end , rate , weeks , } ) : DateIntervalResult [ ] => { const completionTimeInfo = { hours : getHours ( start ) , minutes : getMinutes ( start ) , seconds : getSeconds ( start ) , milliseconds : getMilliseconds ( start ) , } ; const duration = differenceInMilliseconds ( start ) ( end ) ; const allDays = eachDayOfInterval ( { start , end : addDays ( rate * 7 + 6 ) ( start ) , } ) ; const result = [ ] ; for ( let i = 0 ; i < allDays . length ; i ++ ) { const date = allDays [ i ] ; if ( weeks . includes ( getDay ( date ) ) ) { const start = addMilliseconds ( completionTimeInfo . milliseconds ) ( add ( completionTimeInfo ) ( date ) ) ; result . push ( { start , end : addMilliseconds ( duration ) ( start ) , } ) ; if ( result . length === rate ) { return result ; } } } return result ; } ; interface DateIntervalByWeekParams { start : number | Date ; end : number | Date ; endDate : number | Date ; weeks : Week [ ] ; } interface DateIntervalByRateParams { start : number | Date ; end : number | Date ; rate : number ; weeks : Week [ ] ; } export interface DateIntervalResult { start : Date ; end : Date ; }