-
Notifications
You must be signed in to change notification settings - Fork 18
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
Update README.md #106
Update README.md #106
Changes from all commits
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 |
---|---|---|
|
@@ -40,13 +40,20 @@ | |
|
||
## Quick Start | ||
|
||
Use the `format` getter function for formatting using `long` and `short` style options. | ||
|
||
```javascript | ||
new Intl.DurationFormat("fr-FR", { style: "long" }).format({ | ||
hours: 1, | ||
minutes: 46, | ||
seconds: 40, | ||
}); | ||
// => "1 heure, 46 minutes et 40 secondes" | ||
const duration = { | ||
hours: 1, | ||
minutes: 46, | ||
seconds: 40, | ||
}; | ||
|
||
new Intl.DurationFormat("fr-FR", { style: "long" }).format(duration); | ||
// → '1 heure, 46 minutes et 40 secondes' | ||
|
||
new Intl.DurationFormat("en", { style: "short" }).format(duration); | ||
// → '1 hr, 46 min and 40 sec' | ||
``` | ||
|
||
## Motivation | ||
|
@@ -65,8 +72,9 @@ In this section, we are going to illustrate each user needs (requirements) and a | |
|
||
#### Design | ||
|
||
* Input value will be an object of type `Temporal.Duration` | ||
* Example: `new DurationFormat().format(Temporal.Duration.from({hours: 3, minutes: 4});` | ||
* Input value will be an object, possible values include: | ||
"`months`", " `weeks`", "`days`", "`hours`", "`minutes`", " `seconds`", "`milliseconds`", "`microseconds`", "`nanoseconds`". | ||
* Example: `new DurationFormat().format({hours: 3, minutes: 4 });` | ||
|
||
### Formatting width | ||
|
||
|
@@ -182,20 +190,68 @@ We allow users to specify a `fractionalDigits` option that will display the smal | |
#### Example | ||
|
||
```javascript | ||
new Intl.DurationFormat('en', { fractionalDigits: 2 }).format('PT12.3456S'); // => 12.34 sec | ||
new Intl.DurationFormat('en', { milliseconds: 'narrow', fractionalDigits: 2 }).format('PT12.3456S'); // => 12s 345.60ms | ||
const duration = { | ||
seconds: 12, | ||
milliseconds: 345, | ||
microseconds: 600, | ||
}; | ||
|
||
// Example using fractionalDigits | ||
new Intl.DurationFormat("en", { fractionalDigits: 2 }).format(duration); | ||
// => 12 sec, 345 ms, 600 μs | ||
|
||
// Example using fractionalDigits and milliseconds set to `long` | ||
new Intl.DurationFormat("en", { | ||
milliseconds: "long", | ||
fractionalDigits: 2, | ||
}).format(duration); | ||
// => 12 sec, 345 milliseconds, 600 μs | ||
``` | ||
|
||
## API design | ||
|
||
``` javascript | ||
new Intl.DurationFormat('en').format(Temporal.Duration.from('PT2H46M40S')); // => 2 hr 46 min 40 sec | ||
new Intl.DurationFormat('en', { | ||
hours: 'numeric', | ||
seconds: 'numeric', | ||
}).format(Temporal.Duration.from('PT2H40S')); // => 2:00:40 | ||
const duration = { | ||
hours: 2, | ||
minutes: 46, | ||
seconds: 40, | ||
}; | ||
|
||
new Intl.DurationFormat("en").format(duration); | ||
// => 2 hr 46 min 40 sec | ||
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.
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. https://github.com/tc39/proposal-intl-duration-format/pull/127/files guess this modifies the behaviour you explain |
||
|
||
new Intl.DurationFormat("en", { | ||
hours: "numeric", | ||
seconds: "numeric", | ||
}).format(duration); | ||
// => 2:46:40 | ||
``` | ||
|
||
```javascript | ||
const duration = { | ||
years: 1, | ||
months: 2, | ||
weeks: 3, | ||
days: 3, | ||
hours: 4, | ||
minutes: 5, | ||
seconds: 6, | ||
milliseconds: 7, | ||
microseconds: 8, | ||
nanoseconds: 9, | ||
}; | ||
|
||
new Intl.DurationFormat("en").format(duration); | ||
// => '1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns' | ||
new Intl.DurationFormat("en", { style: "short" }).format(duration); | ||
// => '1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns' | ||
new Intl.DurationFormat("en", { style: "narrow" }).format(duration); | ||
// => '1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns' | ||
new Intl.DurationFormat("en", { style: "digital" }).format(duration); | ||
// = '1y, 2mo, 3w, 3d, 4:05:06' | ||
``` | ||
|
||
|
||
### Constructor | ||
|
||
#### Syntax | ||
|
@@ -240,7 +296,7 @@ new Intl.DurationFormat(locales, options) | |
|
||
##### Default values | ||
|
||
* The per-unit style options default to the value of `style` for all styles except `"digital"`, for which units years till days default to `"narrow"` and hours till nanoseconds default to `"numeric"`. | ||
* The per-unit style options default to the value of `style` for all styles except `"digital"`, for which units years till days default to `"short"` and hours till nanoseconds default to `"numeric"`. | ||
* The per-unit display options default to `"auto"` if the corresponding style option is `undefined` and `"always"` otherwise. | ||
|
||
#### Notes | ||
|
@@ -269,7 +325,42 @@ A `string` containing the formatted duration. | |
#### Syntax | ||
|
||
```javascript | ||
new Intl.DurationFormat('en').formatToParts(duration) | ||
const duration = { | ||
hours: 7, | ||
minutes: 8, | ||
seconds: 9, | ||
milliseconds: 123, | ||
microseconds: 456, | ||
nanoseconds: 789, | ||
}; | ||
|
||
new Intl.DurationFormat('en').formatToParts(duration); | ||
|
||
// => [ | ||
// { type: "integer", value: "7", unit: "hour" }, | ||
// { type: "literal", value: " ", unit: "hour" }, | ||
// { type: "unit", value: "hr", unit: "hour" }, | ||
// { type: "literal", value: ", " }, | ||
// { type: "integer", value: "8", unit: "minute" }, | ||
// { type: "literal", value: " ", unit: "minute" }, | ||
// { type: "unit", value: "min", unit: "minute" }, | ||
// { type: "literal", value: ", " }, | ||
// { type: "integer", value: "9", unit: "second" }, | ||
// { type: "literal", value: " ", unit: "second" }, | ||
// { type: "unit", value: "sec", unit: "second" }, | ||
// { type: "literal", value: ", " }, | ||
// { type: "integer", value: "123", unit: "millisecond" }, | ||
// { type: "literal", value: " ", unit: "millisecond" }, | ||
// { type: "unit", value: "msec", unit: "millisecond" }, | ||
// { type: "literal", value: ", " }, | ||
// { type: "integer", value: "456", unit: "microsecond" }, | ||
// { type: "literal", value: " ", unit: "microsecond" }, | ||
// { type: "unit", value: "μsec", unit: "microsecond" }, | ||
// { type: "literal", value: " and " }, | ||
// { type: "integer", value: "789", unit: "nanosecond" }, | ||
// { type: "literal", value: " ", unit: "nanosecond" }, | ||
// { type: "unit", value: "nsec", unit: "nanosecond" }, | ||
// ] | ||
``` | ||
|
||
#### Parameters | ||
|
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.
If we want to reduce the surprise with this behavior, a solution would be to set the default style for milliseconds, microseconds, and nanoseconds to be "numeric", even in non-digital styles.
Either way, please include an example with fractionalDigits and digital format.