Skip to content

Commit

Permalink
[JSC] Use "short" for "digital" style in Intl.DurationFormat
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=246165
rdar://100860476

Reviewed by Mark Lam.

In the last ECMA 402 meeting, we agreed that default style of Intl.DurationFormat should be "short",
but the spec was not updated for "digital". It was a spec bug[1]. We should fix our implementation.

[1]: tc39/proposal-intl-duration-format#127

* JSTests/stress/intl-durationformat-digital.js:
(Intl.DurationFormat.shouldBeOneOf.fmt.format):
(Intl.DurationFormat.shouldBeOneOf):
* JSTests/stress/intl-durationformat-format-to-parts.js:
(Intl.DurationFormat.shouldBe.JSON.stringify.fmt.formatToParts):
* Source/JavaScriptCore/runtime/IntlDurationFormat.cpp:
(JSC::IntlDurationFormat::initializeDurationFormat):

Canonical link: https://commits.webkit.org/255255@main
  • Loading branch information
Constellation committed Oct 7, 2022
1 parent 1caa146 commit 9bcd2c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions JSTests/stress/intl-durationformat-digital.js
Expand Up @@ -16,44 +16,44 @@ if (Intl.DurationFormat) {
style: 'digital'
});
shouldBeOneOf(fmt.format({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 }), [
`1y 2mo 3w 4d 10.34.33`,
`1y 2m 3w 4d 10.34.33`,
`1y, 2mo, 3w, 4d, 10.34.33`,
`1y, 2m, 3w, 4d, 10.34.33`,
]);
}
{
var fmt = new Intl.DurationFormat('en-DK-u-ca-buddhist', {
style: 'digital'
});
shouldBeOneOf(fmt.format({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 }), [
`1y 2mo 3w 4d 10.34.33`,
`1y 2m 3w 4d 10.34.33`,
`1y, 2mo, 3w, 4d, 10.34.33`,
`1y, 2m, 3w, 4d, 10.34.33`,
]);
}
{
var fmt = new Intl.DurationFormat('en-DK-u-nu-hanidec', {
style: 'digital'
});
shouldBeOneOf(fmt.format({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 }), [
`一y 二mo 三w 四d 一〇:三四:三三`,
`一y 二m 三w 四d 一〇:三四:三三`,
`一y, 二mo, 三w, 四d, 一〇:三四:三三`,
`一y, 二m, 三w, 四d, 一〇:三四:三三`,
]);
}
{
var fmt = new Intl.DurationFormat('en', {
style: 'digital'
});
shouldBeOneOf(fmt.format({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 }), [
`1y 2mo 3w 4d 10:34:33`,
`1y 2m 3w 4d 10:34:33`,
`1y, 2mo, 3w, 4d, 10:34:33`,
`1y, 2m, 3w, 4d, 10:34:33`,
]);
}
{
var fmt = new Intl.DurationFormat('en', {
style: 'digital'
});
shouldBeOneOf(fmt.format({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 }), [
`1y 2mo 3w 4d 10:34:33`,
`1y 2m 3w 4d 10:34:33`,
`1y, 2mo, 3w, 4d, 10:34:33`,
`1y, 2m, 3w, 4d, 10:34:33`,
]);
}
{
Expand All @@ -65,8 +65,8 @@ if (Intl.DurationFormat) {
});

shouldBeOneOf(fmt.format({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 }), [
`1y 2mo 3w 4d 10:34:33.032000000`,
`1y 2m 3w 4d 10:34:33.032000000`,
`1y, 2mo, 3w, 4d, 10:34:33.032000000`,
`1y, 2m, 3w, 4d, 10:34:33.032000000`,
]);
}
{
Expand All @@ -78,8 +78,8 @@ if (Intl.DurationFormat) {
});

shouldBeOneOf(fmt.format({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 }), [
`1y 2mo 3w 4d 10:34:33.03`,
`1y 2m 3w 4d 10:34:33.03`,
`1y, 2mo, 3w, 4d, 10:34:33.03`,
`1y, 2m, 3w, 4d, 10:34:33.03`,
]);
}
{
Expand All @@ -90,7 +90,7 @@ if (Intl.DurationFormat) {
});

shouldBeOneOf(fmt.format({ hours: 10, seconds: 33, milliseconds: 32 }), [
`10 33.032000000`,
`10, 33.032000000`,
]);
}
{
Expand Down
4 changes: 2 additions & 2 deletions JSTests/stress/intl-durationformat-format-to-parts.js
Expand Up @@ -54,8 +54,8 @@ if (Intl.DurationFormat) {
});

shouldBeOneOf(JSON.stringify(fmt.formatToParts({ years: 1, months: 2, weeks: 3, days: 4, hours: 10, minutes: 34, seconds: 33, milliseconds: 32 })), [
`[{"type":"years","value":"1y"},{"type":"literal","value":" "},{"type":"months","value":"2mo"},{"type":"literal","value":" "},{"type":"weeks","value":"3w"},{"type":"literal","value":" "},{"type":"days","value":"4d"},{"type":"literal","value":" "},{"type":"hours","value":"10"},{"type":"literal","value":":"},{"type":"minutes","value":"34"},{"type":"literal","value":":"},{"type":"seconds","value":"33.03"}]`,
`[{"type":"years","value":"1y"},{"type":"literal","value":" "},{"type":"months","value":"2m"},{"type":"literal","value":" "},{"type":"weeks","value":"3w"},{"type":"literal","value":" "},{"type":"days","value":"4d"},{"type":"literal","value":" "},{"type":"hours","value":"10"},{"type":"literal","value":":"},{"type":"minutes","value":"34"},{"type":"literal","value":":"},{"type":"seconds","value":"33.03"}]`,
`[{"type":"years","value":"1y"},{"type":"literal","value":", "},{"type":"months","value":"2mo"},{"type":"literal","value":", "},{"type":"weeks","value":"3w"},{"type":"literal","value":", "},{"type":"days","value":"4d"},{"type":"literal","value":", "},{"type":"hours","value":"10"},{"type":"literal","value":":"},{"type":"minutes","value":"34"},{"type":"literal","value":":"},{"type":"seconds","value":"33.03"}]`,
`[{"type":"years","value":"1y"},{"type":"literal","value":", "},{"type":"months","value":"2m"},{"type":"literal","value":", "},{"type":"weeks","value":"3w"},{"type":"literal","value":", "},{"type":"days","value":"4d"},{"type":"literal","value":", "},{"type":"hours","value":"10"},{"type":"literal","value":":"},{"type":"minutes","value":"34"},{"type":"literal","value":":"},{"type":"seconds","value":"33.03"}]`,
]);
}
}
4 changes: 2 additions & 2 deletions Source/JavaScriptCore/runtime/IntlDurationFormat.cpp
Expand Up @@ -249,15 +249,15 @@ void IntlDurationFormat::initializeDurationFormat(JSGlobalObject* globalObject,
auto toUListFormatterWidth = [](Style style) {
// 6. Let listStyle be durationFormat.[[Style]].
// 7. If listStyle is "digital", then
// a. Set listStyle to "narrow".
// a. Set listStyle to "short".
// 8. Perform ! CreateDataPropertyOrThrow(lfOpts, "style", listStyle).
switch (style) {
case Style::Long:
return ULISTFMT_WIDTH_WIDE;
case Style::Short:
case Style::Digital:
return ULISTFMT_WIDTH_SHORT;
case Style::Narrow:
case Style::Digital:
return ULISTFMT_WIDTH_NARROW;
}
return ULISTFMT_WIDTH_WIDE;
Expand Down

0 comments on commit 9bcd2c5

Please sign in to comment.