Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion projects/common/src/time/time-duration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TimeDuration } from './time-duration';
import { TimeDuration, UnitStringType } from './time-duration';
import { TimeUnit } from './time-unit.type';

describe('Time duration', () => {
Expand All @@ -13,6 +13,13 @@ describe('Time duration', () => {
TimeUnit.Minute
)
).toBe('4h3m');
expect(
new TimeDuration(4 * 60 * 60 * 1000 + 3 * 60 * 1000 + 5 * 1000 + 689, TimeUnit.Millisecond).toMultiUnitString(
TimeUnit.Minute,
false,
UnitStringType.Long
)
).toBe('4 hours 3 minutes');
expect(
new TimeDuration(4 * 60 * 60 * 1000 + 5 * 1000 + 689, TimeUnit.Millisecond).toMultiUnitString(TimeUnit.Second)
).toBe('4h5s');
Expand Down
29 changes: 22 additions & 7 deletions projects/common/src/time/time-duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@ export class TimeDuration {
return this.toMillis() / this.unitInMillis(unit);
}

public toMultiUnitString(smallestUnit: ConvertibleTimeUnit = TimeUnit.Second, displayZero: boolean = true): string {
public toMultiUnitString(
smallestUnit: ConvertibleTimeUnit = TimeUnit.Second,
displayZero: boolean = true,
unitStringType: UnitStringType = UnitStringType.Short
): string {
const mostSignificantPortion = this.getMostSignificantUnitOnly();
const remainingMillis = this.millis - mostSignificantPortion.toMillis();
if (mostSignificantPortion.getAmountForUnit(smallestUnit) < 1) {
return displayZero ? new TimeDuration(0, smallestUnit).toString() : '';
return displayZero ? new TimeDuration(0, smallestUnit).toFormattedString(unitStringType) : '';
}
if (mostSignificantPortion.unit === smallestUnit || remainingMillis === 0) {
return mostSignificantPortion.toString();
return mostSignificantPortion.toFormattedString(unitStringType);
}

return `${mostSignificantPortion.toString()}${new TimeDuration(
remainingMillis,
TimeUnit.Millisecond
).toMultiUnitString(smallestUnit, false)}`;
const joiningStr = unitStringType === UnitStringType.Long ? ' ' : '';

return [
mostSignificantPortion.toFormattedString(unitStringType),
new TimeDuration(remainingMillis, TimeUnit.Millisecond).toMultiUnitString(smallestUnit, false, unitStringType)
].join(joiningStr);
}

private toFormattedString(unitStringType: UnitStringType = UnitStringType.Short): string {
return unitStringType === UnitStringType.Short ? this.toString() : this.toLongString();
}

public getMostSignificantUnitOnly(): TimeDuration {
Expand Down Expand Up @@ -141,3 +151,8 @@ type ConvertibleTimeUnit =
| TimeUnit.Minute
| TimeUnit.Second
| TimeUnit.Millisecond;

export enum UnitStringType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this got switched back from const again? was const causing issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. using const fails when the enum is used as a param value in a method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm - shouldn't be. I suspect that's a local caching artifact from the switch between const and non-const. Could you trying clearing your jest cache npm run test -- --clear-cache and then running with the const version?

Long = 'long',
Short = 'short'
}