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(
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;
}
}