-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add support for long format in multi unit string #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ce7ba59
39c983d
a09098d
ef93154
155344b
42ec8ce
82573b4
b052bee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -141,3 +151,8 @@ type ConvertibleTimeUnit = | |
| | TimeUnit.Minute | ||
| | TimeUnit.Second | ||
| | TimeUnit.Millisecond; | ||
|
|
||
| export enum UnitStringType { | ||
arjunlalb marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| Long = 'long', | ||
| Short = 'short' | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.