From 2ed44b065c519d40c960151a0017624fcb4fb5a4 Mon Sep 17 00:00:00 2001 From: rahulprajesh Date: Thu, 21 Dec 2023 10:35:11 +0530 Subject: [PATCH] fix(time-duration): getMostSignificantUnitOnlyWithoutApprox created to avoid approx --- projects/common/src/time/time-duration.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/projects/common/src/time/time-duration.ts b/projects/common/src/time/time-duration.ts index b73b9c9ac..6758523c7 100644 --- a/projects/common/src/time/time-duration.ts +++ b/projects/common/src/time/time-duration.ts @@ -74,6 +74,28 @@ export class TimeDuration { return new TimeDuration(amountForUnit, firstApplicableUnit); } + public getMostSignificantUnitOnlyWithoutApprox(maxSupportedUnit?: TimeUnit): TimeDuration { + let orderedUnits: ConvertibleTimeUnit[] = isNil(maxSupportedUnit) + ? TimeDuration.TIME_UNITS + : TimeDuration.TIME_UNITS.slice(indexOf(TimeDuration.TIME_UNITS, maxSupportedUnit)); + + let firstApplicableUnit = TimeUnit.Millisecond; + for (let unit of orderedUnits) { + const selectedUnitValue = this.getAmountForUnit(unit); + + if (selectedUnitValue >= 1 && selectedUnitValue - Math.floor(selectedUnitValue) === 0) { + firstApplicableUnit = unit; + + break; + } else if (selectedUnitValue >= 1) { + firstApplicableUnit = unit; + } + } + const amountForUnit = Math.floor(this.getAmountForUnit(firstApplicableUnit)); + + return new TimeDuration(amountForUnit, firstApplicableUnit); + } + public toString(): string { return `${this.value}${this.unit}`; }