import * as moment from 'moment' ; import { List , Map } from 'immutable' ; import { LEGACY_DATE_FORMAT , WORKER_DATE_FORMAT , DATE_FORMAT , MOMENT_LOCALE , HHMMSS_FORMAT , CONTACT_DATE_FORMAT } from '../constants/dates' ; import { range } from './arrays' ; import { LegacyDateFormat , WorkerDateFormat , HeatMapValue } from 'types' ; import { Duration } from 'constants/enums' ; export type DurationUnit = moment . DurationInputArg2 ; export const dateObjectTo = ( date ) => ( format ) => moment ( date ) . format ( format ) ; export const stringToDate = ( dateString ) => ( format ) => moment ( dateString , format ) . toDate ( ) ; export const workerDateFormatToLegacyDateFormat = ( dateString ) : LegacyDateFormat => moment ( dateString , WORKER_DATE_FORMAT ) . format ( LEGACY_DATE_FORMAT ) ; export const legacyDateFormatToContactDateFormat = ( dateString ) => moment ( dateString , LEGACY_DATE_FORMAT ) . format ( CONTACT_DATE_FORMAT ) ; export const legacyDateFormatToDateObj = ( dateStr ) : Date => moment ( dateStr , LEGACY_DATE_FORMAT ) . toDate ( ) ; export const shiftDate = ( date , numDays ) => { return moment ( date ) . add ( numDays , Duration . DAYS ) . toDate ( ) ; } ; export const shiftDateString = ( dateString , numDays ) => { return moment ( dateString , LEGACY_DATE_FORMAT ) . subtract ( numDays , Duration . DAYS ) . format ( LEGACY_DATE_FORMAT ) ; } ; export const todayFormatted = ( ) : string => moment ( ) . format ( LEGACY_DATE_FORMAT ) ; export const dateStringToLocaleDateString = ( dateString ) : string => moment ( dateString , LEGACY_DATE_FORMAT ) . format ( MOMENT_LOCALE ) ; export const generateOneYearOfDates = ( startDate ) => { const startMoment = moment ( startDate ) ; return dateRange ( startMoment , 365 + startMoment . day ( ) ) ; } ; export const generateOneYearOfData = ( map , oneYearOfDates ) => { return oneYearOfDates . reduce ( ( acc , date ) => { const count = map . get ( date ) ; const data = count ? { date , count : Math . round ( count * 100 ) / 100 } : { date , count : 0 } ; return acc . push ( data ) ; } , List ( ) ) ; } ; export const dateRange = ( startDate , numDays = 365 ) : List < LegacyDateFormat > => range ( numDays ) . reduce ( ( acc , cur ) => acc . push ( startDate . clone ( ) . subtract ( cur , Duration . DAYS ) . format ( LEGACY_DATE_FORMAT ) ) , List ( ) ) . reverse ( ) as List < LegacyDateFormat > ; export interface DateComparatorFunction { ( date , timeBefore , start , unit ) : boolean ; } export const olderThan = ( date , timeBefore , start , unit ) => { const daysBeforeNow = moment ( start ) . subtract ( timeBefore , unit ) ; return moment ( date ) . isBefore ( daysBeforeNow ) ; } ; export const youngerThan = ( date , timeBefore , start , unit ) => moment ( date ) . isAfter ( moment ( start ) . subtract ( timeBefore , unit ) ) ; export const secondsToMinutes = ( numSeconds ) : number => Math . floor ( numSeconds / 60 ) ; export const formatSecondsAsHhMmSs = ( numSeconds ) : string => moment . utc ( numSeconds * 1000 ) . format ( HHMMSS_FORMAT ) ; param delay export const calculateDateAfterDelay = ( now , delay ) : Date => { const timeAfterDelay = new Date ( now + delay * 1000 ) ; return Number . isNaN ( timeAfterDelay . valueOf ( ) ) ? new Date ( now + 10000 ) : timeAfterDelay ; } ;