prettyETA.ts 828 B

12345678910111213141516171819202122232425262728
  1. import secondsToTime from './secondsToTime.ts'
  2. export default function prettyETA(seconds: number): string {
  3. const time = secondsToTime(seconds)
  4. // Only display hours and minutes if they are greater than 0 but always
  5. // display minutes if hours is being displayed
  6. // Display a leading zero if the there is a preceding unit: 1m 05s, but 5s
  7. const hoursStr = time.hours === 0 ? '' : `${time.hours}h`
  8. const minutesStr =
  9. time.minutes === 0 ?
  10. ''
  11. : `${
  12. time.hours === 0 ?
  13. time.minutes
  14. : ` ${time.minutes.toString(10).padStart(2, '0')}`
  15. }m`
  16. const secondsStr =
  17. time.hours !== 0 ?
  18. ''
  19. : `${
  20. time.minutes === 0 ?
  21. time.seconds
  22. : ` ${time.seconds.toString(10).padStart(2, '0')}`
  23. }s`
  24. return `${hoursStr}${minutesStr}${secondsStr}`
  25. }