import { Pipe, PipeTransform } from '@angular/core'; import { eUnit } from '../enums'; @Pipe({ name: 'formatDuration', standalone: true, }) export class FormatDurationPipe implements PipeTransform { transform(time: { days: number; hours: number; minutes: number; seconds: number; }): string { if (!time || !time.hasOwnProperty(eUnit.HOURS)) { return `0${eUnit.MINUTES_SHORT}`; } const minutesRoundedToFive = Math.trunc(time.minutes / 5) * 5; const timeUnits = [ { value: time.days, label: eUnit.DAYS_SHORT }, { value: time.hours, label: eUnit.HOURS_SHORT }, { value: minutesRoundedToFive, label: eUnit.MINUTES_SHORT }, ]; const filteredUnits = timeUnits.filter( (unit) => unit.value > 0 || unit.label === eUnit.MINUTES_SHORT ); const topUnits = filteredUnits.slice(0, 2); return topUnits .map((unit) => { return `${unit.value}${unit.label}`; }) .join(' '); } }