fix(common/Time): fix humanizeDuration for duration > 1 year

This commit is contained in:
Alice Gaudon 2022-03-06 13:22:12 +01:00
parent cb5001ce6e
commit 0c4349fac3

View File

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