Skip to content

Commit

Permalink
fix(time-duration): getMostSignificantUnitOnlyWithoutApprox created t…
Browse files Browse the repository at this point in the history
…o avoid approx
  • Loading branch information
rahul-p-rajesh committed Dec 21, 2023
1 parent db92812 commit 2ed44b0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions projects/common/src/time/time-duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ export class TimeDuration {
return new TimeDuration(amountForUnit, firstApplicableUnit);
}

public getMostSignificantUnitOnlyWithoutApprox(maxSupportedUnit?: TimeUnit): TimeDuration {

Check warning on line 77 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L77

Added line #L77 was not covered by tests
let orderedUnits: ConvertibleTimeUnit[] = isNil(maxSupportedUnit)
? TimeDuration.TIME_UNITS
: TimeDuration.TIME_UNITS.slice(indexOf(TimeDuration.TIME_UNITS, maxSupportedUnit));

Check warning on line 80 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L79-L80

Added lines #L79 - L80 were not covered by tests

let firstApplicableUnit = TimeUnit.Millisecond;
for (let unit of orderedUnits) {
const selectedUnitValue = this.getAmountForUnit(unit);

Check warning on line 84 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L82-L84

Added lines #L82 - L84 were not covered by tests

if (selectedUnitValue >= 1 && selectedUnitValue - Math.floor(selectedUnitValue) === 0) {
firstApplicableUnit = unit;

Check warning on line 87 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L87

Added line #L87 was not covered by tests

break;

Check warning on line 89 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L89

Added line #L89 was not covered by tests
} else if (selectedUnitValue >= 1) {
firstApplicableUnit = unit;

Check warning on line 91 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L91

Added line #L91 was not covered by tests
}
}
const amountForUnit = Math.floor(this.getAmountForUnit(firstApplicableUnit));

Check warning on line 94 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L94

Added line #L94 was not covered by tests

return new TimeDuration(amountForUnit, firstApplicableUnit);

Check warning on line 96 in projects/common/src/time/time-duration.ts

View check run for this annotation

Codecov / codecov/patch

projects/common/src/time/time-duration.ts#L96

Added line #L96 was not covered by tests
}

public toString(): string {
return `${this.value}${this.unit}`;
}
Expand Down

0 comments on commit 2ed44b0

Please sign in to comment.