diff --git a/src/common/Time.ts b/src/common/Time.ts index d2ee1d2..6659715 100644 --- a/src/common/Time.ts +++ b/src/common/Time.ts @@ -67,7 +67,7 @@ export class Time { public static humanizeDuration( duration: number, short: boolean = false, - skipOneUnitNumber: boolean = false, + skipNumberTextPartWhenSingular: boolean = false, units: TimeUnit[] = [ this.UNITS.SECOND, this.UNITS.MINUTE, @@ -77,13 +77,13 @@ export class Time { this.UNITS.YEAR, ], ): string { - for (let i = units.length - 1; i > 0; i--) { - if (duration >= units[i - 1].milliseconds && duration < units[i].milliseconds) { - const amount = Math.floor(duration / units[i - 1].milliseconds); - const unit = short ? - units[i - 1].shortName : - ' ' + units[i - 1].longName + (amount > 1 ? 's' : ''); - return (amount > 1 || !skipOneUnitNumber ? amount : '') + unit; + for (const unit of units.reverse()) { + if (duration >= unit.milliseconds) { + const amount = Math.floor(duration / unit.milliseconds); + const unitTextPart = short ? + unit.shortName : + ' ' + unit.longName + (amount > 1 ? 's' : ''); + return (amount > 1 || !skipNumberTextPartWhenSingular ? amount : '') + unitTextPart; } }