mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 16:57:35 +00:00
LibJS: Create DurationFormat's ListFormat object with type and style
This is a normative change in the Intl.DurationFormat spec. See:
1304e4b
This commit is contained in:
parent
127b28c940
commit
c477425b9b
4 changed files with 57 additions and 36 deletions
|
@ -490,8 +490,28 @@ Vector<PatternPartition> partition_duration_format_pattern(VM& vm, DurationForma
|
|||
}
|
||||
}
|
||||
|
||||
// 4. Let lf be ! Construct(%ListFormat%, « durationFormat.[[Locale]] »).
|
||||
auto* list_format = static_cast<ListFormat*>(MUST(construct(vm, *realm.intrinsics().intl_list_format_constructor(), js_string(vm, duration_format.locale()))));
|
||||
// 4. Let lfOpts be ! OrdinaryObjectCreate(null).
|
||||
auto* list_format_options = Object::create(realm, nullptr);
|
||||
|
||||
// 5. Perform ! CreateDataPropertyOrThrow(lfOpts, "type", "unit").
|
||||
MUST(list_format_options->create_data_property_or_throw(vm.names.type, js_string(vm, "unit"sv)));
|
||||
|
||||
// 6. Let listStyle be durationFormat.[[Style]].
|
||||
auto list_style = duration_format.style();
|
||||
|
||||
// 7. If listStyle is "digital", then
|
||||
if (list_style == DurationFormat::Style::Digital) {
|
||||
// a. Set listStyle to "narrow".
|
||||
list_style = DurationFormat::Style::Narrow;
|
||||
}
|
||||
|
||||
auto unicode_list_style = Unicode::style_to_string(static_cast<Unicode::Style>(list_style));
|
||||
|
||||
// 8. Perform ! CreateDataPropertyOrThrow(lfOpts, "style", listStyle).
|
||||
MUST(list_format_options->create_data_property_or_throw(vm.names.style, js_string(vm, unicode_list_style)));
|
||||
|
||||
// 9. Let lf be ! Construct(%ListFormat%, « durationFormat.[[Locale]], lfOpts »).
|
||||
auto* list_format = static_cast<ListFormat*>(MUST(construct(vm, *realm.intrinsics().intl_list_format_constructor(), js_string(vm, duration_format.locale()), list_format_options)));
|
||||
|
||||
// FIXME: CreatePartsFromList expects a list of strings and creates a list of Pattern Partition records, but we already created a list of Pattern Partition records
|
||||
// so we try to hack something together from it that looks mostly right
|
||||
|
@ -512,10 +532,10 @@ Vector<PatternPartition> partition_duration_format_pattern(VM& vm, DurationForma
|
|||
string_result.append(part.value);
|
||||
}
|
||||
|
||||
// 5. Set result to ! CreatePartsFromList(lf, result).
|
||||
// 10. Set result to ! CreatePartsFromList(lf, result).
|
||||
auto final_result = create_parts_from_list(*list_format, string_result);
|
||||
|
||||
// 6. Return result.
|
||||
// 11. Return result.
|
||||
return final_result;
|
||||
}
|
||||
|
||||
|
|
|
@ -61,6 +61,7 @@ public:
|
|||
String const& numbering_system() const { return m_numbering_system; }
|
||||
|
||||
void set_style(StringView style) { m_style = style_from_string(style); }
|
||||
Style style() const { return m_style; }
|
||||
String style_string() const { return style_to_string(m_style); }
|
||||
|
||||
void set_years_style(StringView years_style) { m_years_style = date_style_from_string(years_style); }
|
||||
|
|
|
@ -17,22 +17,22 @@ describe("correct behavior", () => {
|
|||
nanoseconds: 9,
|
||||
};
|
||||
expect(new Intl.DurationFormat().format(duration)).toBe(
|
||||
"1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, and 9 ns"
|
||||
"1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"
|
||||
);
|
||||
expect(new Intl.DurationFormat("en").format(duration)).toBe(
|
||||
"1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, and 9 ns"
|
||||
"1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"
|
||||
);
|
||||
expect(new Intl.DurationFormat("en", { style: "long" }).format(duration)).toBe(
|
||||
"1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds, 8 microseconds, and 9 nanoseconds"
|
||||
"1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds, 8 microseconds, 9 nanoseconds"
|
||||
);
|
||||
expect(new Intl.DurationFormat("en", { style: "short" }).format(duration)).toBe(
|
||||
"1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, and 9 ns"
|
||||
"1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"
|
||||
);
|
||||
expect(new Intl.DurationFormat("en", { style: "narrow" }).format(duration)).toBe(
|
||||
"1y, 2m, 3w, 3d, 4h, 5m, 6s, 7ms, 8μs, and 9ns"
|
||||
"1y 2m 3w 3d 4h 5m 6s 7ms 8μs 9ns"
|
||||
);
|
||||
expect(new Intl.DurationFormat("en", { style: "digital" }).format(duration)).toBe(
|
||||
"1y, 2m, 3w, 3d, and 4:05:06"
|
||||
"1y 2m 3w 3d 4:05:06"
|
||||
);
|
||||
expect(
|
||||
new Intl.DurationFormat("en", {
|
||||
|
@ -40,7 +40,7 @@ describe("correct behavior", () => {
|
|||
nanoseconds: "numeric",
|
||||
fractionalDigits: 3,
|
||||
}).format(duration)
|
||||
).toBe("1y, 2m, 3w, 3d, 4h, 5m, 6s, 7ms, and 8.009μs");
|
||||
).toBe("1y 2m 3w 3d 4h 5m 6s 7ms 8.009μs");
|
||||
|
||||
expect(new Intl.DurationFormat("de", { style: "long" }).format(duration)).toBe(
|
||||
"1 Jahr, 2 Monate, 3 Wochen, 3 Tage, 4 Stunden, 5 Minuten, 6 Sekunden, 7 Millisekunden, 8 Mikrosekunden und 9 Nanosekunden"
|
||||
|
|
|
@ -34,7 +34,7 @@ describe("correct behavior", () => {
|
|||
{ type: "element", value: "7 ms" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "8 μs" },
|
||||
{ type: "literal", value: ", and " },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "9 ns" },
|
||||
]);
|
||||
expect(new Intl.DurationFormat("en").formatToParts(duration)).toEqual([
|
||||
|
@ -55,7 +55,7 @@ describe("correct behavior", () => {
|
|||
{ type: "element", value: "7 ms" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "8 μs" },
|
||||
{ type: "literal", value: ", and " },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "9 ns" },
|
||||
]);
|
||||
expect(new Intl.DurationFormat("en", { style: "long" }).formatToParts(duration)).toEqual([
|
||||
|
@ -76,7 +76,7 @@ describe("correct behavior", () => {
|
|||
{ type: "element", value: "7 milliseconds" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "8 microseconds" },
|
||||
{ type: "literal", value: ", and " },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "9 nanoseconds" },
|
||||
]);
|
||||
expect(new Intl.DurationFormat("en", { style: "short" }).formatToParts(duration)).toEqual([
|
||||
|
@ -97,40 +97,40 @@ describe("correct behavior", () => {
|
|||
{ type: "element", value: "7 ms" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "8 μs" },
|
||||
{ type: "literal", value: ", and " },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "element", value: "9 ns" },
|
||||
]);
|
||||
expect(new Intl.DurationFormat("en", { style: "narrow" }).formatToParts(duration)).toEqual([
|
||||
{ type: "element", value: "1y" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "2m" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "3w" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "3d" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "4h" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "5m" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "6s" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "7ms" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "8μs" },
|
||||
{ type: "literal", value: ", and " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "9ns" },
|
||||
]);
|
||||
expect(new Intl.DurationFormat("en", { style: "digital" }).formatToParts(duration)).toEqual(
|
||||
[
|
||||
{ type: "element", value: "1y" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "2m" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "3w" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "3d" },
|
||||
{ type: "literal", value: ", and " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "4:05:06" },
|
||||
]
|
||||
);
|
||||
|
@ -142,21 +142,21 @@ describe("correct behavior", () => {
|
|||
}).formatToParts(duration)
|
||||
).toEqual([
|
||||
{ type: "element", value: "1y" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "2m" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "3w" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "3d" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "4h" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "5m" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "6s" },
|
||||
{ type: "literal", value: ", " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "7ms" },
|
||||
{ type: "literal", value: ", and " },
|
||||
{ type: "literal", value: " " },
|
||||
{ type: "element", value: "8.009μs" },
|
||||
]);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue