-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: add Duration #55
Conversation
//------ | ||
|
||
test('Duration#toISOString fills out every field', () => { | ||
expect(dur().toISOString()).toBe('P1Y2M1W3DT4H5M6.007S'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is fractional units valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes: If necessary for a particular application, the lowest order components may have a decimal fraction.
}); | ||
|
||
test('Duration#toISOString handles negative durations', () => { | ||
expect(duration({years: -3, seconds: -45}).toISOString()).toBe('P-3YT-45S'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does negative value mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Negative durations are described in ISO 8601-2:
[ 11.3.2 Composite representation ]
| The composite representation of a duration is a more flexible and
| relaxed specification for duration than that of .ISO 8601-1:2019,
| 5.5.2. It accepts all expressions of the duration representation given
| in ISO 8601-1:2019, 5.5.2 and is given as follows.
|
| [!]["-"]["P"][ durationUnits(m)]
|
| where [durationUnits(m)] contains time scale components for expressing
| (positive or negative) duration (see 11.2).
|
| Expressions in the two examples below are valid in ISO 8601-1.
|
| EXAMPLE 1 'P3D', duration of three days.
| EXAMPLE 2 'P180Y800D', duration of one-hundred-and-eighty years and eight-hundred days.
|
| Expressions in the following four examples below are not valid in ISO
| 8601-1, but are valid as specified in this clause.
|
| EXAMPLE 3 'P3W2D', duration of three weeks and two days, which is 23 days (equivalent to the expression
| 'P23D'). In ISO 8601-1, ["W"] is not permitted to occur along with any other component.
| EXAMPLE 4 'PSYlOW', duration of five years and ten weeks.
| EXAMPLE 5 'P-3M-3DT1HSM', duration of three months and three days in the reverse direction, with one hour
| and five minutes in the original direction.
| EXAMPLE 6 'P-ZM-1D', duration in the reverse direction of two months and one day.
|
| When a minus sign is provided as prefix to the duration designator
| ["P"], the minus sign can be internalized into individual time scale
| components within the duration expression by applying to every time
| scale component within.
|
| EXAMPLE 7 '-P2M1D' is equivalent to 'P-2M-1D'.
| EXAMPLE 8 '-P5DT10H' is equivalent to 'P-5DT-10H'.
|
| When a minus sign is applied to a time scale component whose value is
| already negative (pointing to the reverse direction), it means that
| the direction of duration should be once again reversed and should be
| turned into a positive value.
|
| EXAMPLE 9 '-P8M-1D', duration in reverse, "eight months minus one day", is equivalent to 'P-8M1D', "eight
| months ago with a day ahead".
| EXAMPLE 10 '-P-5WT-18H30M', duration in reverse, "go back five weeks, eighteen hours but thirty minutes
| ahead", is equivalent to 'P5WT18H-30M', "go ahead five weeks, eighteen hours, but thirty minutes back".
expect(duration({seconds: 45}).humanize()).toBe('a minute'); | ||
expect(duration({seconds: 89}).humanize()).toBe('a minute'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an option to display it like 1 minute
instead of a minute
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. For fixed numbers you can use humanizeIntl
, but it gives exact numbers e.g. 45 seconds
|
||
function parseIso(inp: string | undefined) { | ||
const res = inp ? parseFloat(inp.replace(',', '.')) : 0; | ||
return isNaN(res) ? 0 : res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number.isNaN
will be more accurate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it different in this case?
Closes #48