Chocobozzz/PeerTube

View on GitHub
client/src/app/shared/shared-main/date/time-duration-formatter.pipe.ts

Summary

Maintainability
A
45 mins
Test Coverage
import { Pipe, PipeTransform } from '@angular/core'

@Pipe({
  name: 'myTimeDurationFormatter',
  standalone: true
})
export class TimeDurationFormatterPipe implements PipeTransform {

  transform (value: number): string {
    const hours = Math.floor(value / 3600)
    const minutes = Math.floor((value % 3600) / 60)
    const seconds = value % 60

    if (hours > 0) {
      let result = $localize`${hours}h`

      if (minutes !== 0) result += ' ' + $localize`${minutes}min`
      if (seconds !== 0) result += ' ' + $localize`${seconds}sec`

      return result
    }

    if (minutes > 0) {
      let result = $localize`${minutes}min`

      if (seconds !== 0) result += ' ' + `${seconds}sec`

      return result
    }

    return $localize`${seconds} sec`
  }
}