import { RECURRENCE , FORMAT } from '../constants' ; import CalendarRecurrence from '../types/CalendarRecurrence' ; param { number | string } n returns { string } const addLeadingZero = ( n = '' ) : string => { return `0${ parseInt ( n . toString ( ) , 10 ) }` . slice ( - 2 ) ; } ; param { number } start param { number } end returns { string } const getDuration = ( start , end ) : string => { const seconds = Math . floor ( ( end - start ) / 1000 ) ; const hours = Math . floor ( seconds / 3600 ) ; const mins = ( ( seconds / 3600 ) % 1 ) * 60 ; return `${ addLeadingZero ( hours ) }${ addLeadingZero ( mins ) }` ; } ; param { number } start param { number } end returns { number } const getHoursDiff = ( start , end ) : number => { const seconds = Math . floor ( ( end - start ) / 1000 ) ; return Math . floor ( seconds / 3600 ) ; } ; param { CalendarRecurrence } recurrence returns { number } const getRecurrenceLengthDays = ( recurrence ) : number => { const { frequency , interval } = recurrence ; const { FREQUENCY } = RECURRENCE ; if ( interval ) { switch ( frequency ) { case FREQUENCY . YEARLY : return interval * 365.25 ; case FREQUENCY . MONTHLY : return interval * 30.42 ; case FREQUENCY . WEEKLY : return interval * 7 ; default : return interval ; } } return 365.25 * 100 ; } ; param { Date } d param { string } format returns { string } const formatDate = ( d = new Date ( ) , format ) : string => { const dateValues = { YYYY : d . getUTCFullYear ( ) , MM : addLeadingZero ( d . getUTCMonth ( ) + 1 ) , DD : addLeadingZero ( d . getUTCDate ( ) ) , hh : addLeadingZero ( d . getUTCHours ( ) ) , mm : addLeadingZero ( d . getUTCMinutes ( ) ) , ss : addLeadingZero ( d . getUTCSeconds ( ) ) } ; return Object . keys ( dateValues ) . reduce ( ( date , key ) : string => { return date . replace ( key , dateValues [ key ] . toString ( ) ) ; } , format ) ; } ; param { Date } d param { string } format returns { string } const formatDateNoUtc = ( d = new Date ( ) , format ) : string => { const dateValues = { YYYY : d . getFullYear ( ) , MM : addLeadingZero ( d . getMonth ( ) + 1 ) , DD : addLeadingZero ( d . getDate ( ) ) , hh : addLeadingZero ( d . getHours ( ) ) , mm : addLeadingZero ( d . getMinutes ( ) ) , ss : addLeadingZero ( d . getSeconds ( ) ) } ; return Object . keys ( dateValues ) . reduce ( ( date , key ) : string => { return date . replace ( key , dateValues [ key ] . toString ( ) ) ; } , format ) ; } ; returns { string } const getTimeCreated = ( ) : string => { return formatDate ( new Date ( ) , FORMAT . DATE ) ; } ; param { Date } dateInput param { number } days returns { Date } const incrementDate = ( dateInput , days ) : Date => { const additionalTime = days * 86400000 ; const newDate = new Date ( ) ; newDate . setTime ( dateInput . getTime ( ) + additionalTime ) ; return newDate ; } ; export default { addLeadingZero , getDuration , getHoursDiff , getRecurrenceLengthDays , formatDate , formatDateNoUtc , getTimeCreated , incrementDate } ;