import { DEFAULT_SEPARATORS , DEFAULT_UNITS , TimeTypes } from './constants' ; /**
 * The duration of each time type in milliseconds
 */ const kTimeDurations = [ [ TimeTypes . Year , 31536000000 ] , [ TimeTypes . Month , 2628000000 ] , [ TimeTypes . Week , 1000 * 60 * 60 * 24 * 7 ] , [ TimeTypes . Day , 1000 * 60 * 60 * 24 ] , [ TimeTypes . Hour , 1000 * 60 * 60 ] , [ TimeTypes . Minute , 1000 * 60 ] , [ TimeTypes . Second , 1000 ] ] ; param duration param assets export class DurationFormatter { public constructor ( public units = DEFAULT_UNITS ) { } public format ( duration , precision = 7 , { left : leftSeparator = DEFAULT_SEPARATORS . left , right : rightSeparator = DEFAULT_SEPARATORS . right } = DEFAULT_SEPARATORS ) { const output = [ ] ; const negative = duration < 0 ; if ( negative ) duration *= - 1 ; for ( const [ type , timeDuration ] of kTimeDurations ) { const substraction = duration / timeDuration ; if ( substraction < 1 ) continue ; const floored = Math . floor ( substraction ) ; duration -= floored * timeDuration ; output . push ( addUnit ( floored , this . units [ type ] , leftSeparator ! ) ) ; if ( output . length >= precision ) break ; } return `${ negative ? '-' : '' }${ output . join ( rightSeparator ) || addUnit ( 0 , this . units . second , leftSeparator ! ) }` ; } } param time param unit function addUnit ( time , unit , separator ) { if ( Reflect . has ( unit , time ) ) return `${ time }${ separator }${ Reflect . get ( unit , time ) }` ; return `${ time }${ separator }${ unit . DEFAULT }` ; } export interface DurationFormatSeparators { left ? : string ; right ? : string ; } export interface DurationFormatAssetsUnit extends Record < number , string > { DEFAULT : string ; } export interface DurationFormatAssetsTime { [ TimeTypes . Second ] : DurationFormatAssetsUnit ; [ TimeTypes . Minute ] : DurationFormatAssetsUnit ; [ TimeTypes . Hour ] : DurationFormatAssetsUnit ; [ TimeTypes . Day ] : DurationFormatAssetsUnit ; [ TimeTypes . Week ] : DurationFormatAssetsUnit ; [ TimeTypes . Month ] : DurationFormatAssetsUnit ; [ TimeTypes . Year ] : DurationFormatAssetsUnit ; }