LibJS: Implement the PartitionDurationFormatPattern abstract operation

This commit is contained in:
Idan Horowitz 2022-06-30 15:45:16 +03:00
parent f4785e2468
commit b1fe6c3f68
Notes: sideshowbarker 2024-07-17 09:47:56 +09:00
2 changed files with 253 additions and 10 deletions

View file

@ -4,8 +4,15 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/DurationFormat.h>
#include <LibJS/Runtime/Intl/ListFormat.h>
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
#include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
#include <LibJS/Runtime/Intl/PluralRules.h>
#include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
#include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
namespace JS::Intl {
@ -232,4 +239,213 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(GlobalObject& g
return DurationUnitOptions { .style = move(style), .display = display.as_string().string() };
}
// FIXME: LibUnicode currently only exposes unit patterns converted to an ECMA402 NumberFormat-specific format,
// since DurationFormat only needs a tiny subset of it, it's much easier to just convert it to the expected format
// here, but at some point we should split the the NumberFormat exporter to export both formats of the data.
static String convert_number_format_pattern_to_duration_format_template(Unicode::NumberFormat const& number_format)
{
auto result = number_format.zero_format.replace("{number}", "{0}");
for (size_t i = 0; i < number_format.identifiers.size(); ++i)
result = result.replace(String::formatted("{{unitIdentifier:{}}}", i), number_format.identifiers[i]);
return result;
}
// 1.1.3 PartitionDurationFormatPattern ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-partitiondurationformatpattern
ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(GlobalObject& global_object, DurationFormat const& duration_format, Temporal::DurationRecord const& duration)
{
auto& vm = global_object.vm();
// 1. Let result be a new empty List.
Vector<PatternPartition> result;
// 2. For each row in Table 1, except the header row, in table order, do
for (size_t i = 0; i < duration_instances_components.size(); ++i) {
auto const& duration_instances_component = duration_instances_components[i];
// a. Let styleSlot be the Style Slot value.
auto style_slot = duration_instances_component.get_style_slot;
decltype(DurationInstanceComponent::get_style_slot) next_style_slot = nullptr;
// FIXME: Missing spec step - If this is not the last row
if (i < duration_instances_components.size() - 1) {
// b. Let nextStyleSlot be the Style Slot value of the next row.
next_style_slot = duration_instances_components[i + 1].get_style_slot;
}
// c. Let displaySlot be the Display Slot value.
auto display_slot = duration_instances_component.get_display_slot;
// d. Let valueSlot be the Value Slot value.
auto value_slot = duration_instances_component.value_slot;
// e. Let unit be the Unit value.
auto unit = duration_instances_component.unit;
// f. Let style be the current value of the styleSlot slot of durationFormat.
auto style = (duration_format.*style_slot)();
DurationFormat::ValueStyle next_style = DurationFormat::ValueStyle::Long;
// FIXME: Missing spec step - If this is not the last row
if (next_style_slot) {
// g. Let nextStyle be the current value of the nextStyleSlot slot of durationFormat.
next_style = (duration_format.*next_style_slot)();
}
// h. Let nfOpts be ! OrdinaryObjectCreate(null).
auto* number_format_options = Object::create(global_object, nullptr);
// i. Let value be 0.
auto value = Value(0);
// j. Let done be false.
auto done = false;
// k. If unit is "seconds", "milliseconds" or "microseconds" and nextStyle is "numeric", then
if (unit.is_one_of("seconds"sv, "milliseconds"sv, "microseconds"sv) && next_style == DurationFormat::ValueStyle::Numeric) {
// i. Set value to duration.[[Microseconds]] + duration.[[Nanoseconds]] / 1000.
auto value_number = duration.microseconds + (duration.nanoseconds / 1000);
// ii. If unit is "seconds" or "milliseconds", then
if (unit.is_one_of("seconds"sv, "milliseconds"sv)) {
// 1. Set value to duration.[[Milliseconds]] + value / 1000.
value_number = duration.milliseconds + (value_number / 1000);
// 2. If unit is "seconds", then
if (unit == "seconds"sv) {
// a. Set value to duration.[[Seconds]] + value / 1000.
value_number = duration.seconds + (value_number / 1000);
}
}
value = Value(value_number);
// iii. Perform ! CreateDataPropertyOrThrow(nfOpts, "maximumFractionDigits", durationFormat.[[FractionalDigits]]).
MUST(number_format_options->create_data_property_or_throw(vm.names.maximumFractionDigits, duration_format.has_fractional_digits() ? Value(duration_format.fractional_digits()) : js_undefined()));
// iv. Set done to true.
done = true;
}
// l. Else,
else {
// i. Set value to the current value of the valueSlot slot of duration.
value = Value(duration.*value_slot);
}
// m. If style is "2-digit", then
if (style == DurationFormat::ValueStyle::TwoDigit) {
// i. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumIntegerDigits", 2).
MUST(number_format_options->create_data_property_or_throw(vm.names.minimumIntegerDigits, Value(2)));
}
// FIXME: Missing spec step - Let display be the current value of the displaySlot slot of durationFormat.
auto display = (duration_format.*display_slot)();
// n. If value is +0𝔽 or -0𝔽 and display is "auto", then
if ((value.is_negative_zero() || value.is_positive_zero()) && display == DurationFormat::Display::Auto) {
// i. Skip to the next iteration.
continue;
}
// o. Let nf be ? Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
auto* number_format = static_cast<Intl::NumberFormat*>(TRY(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, duration_format.locale()), number_format_options)));
// FIXME: durationFormat.[[NumberFormat]] is not a thing, the spec likely means 'nf' in this case
// p. Let num be ! FormatNumeric(durationFormat.[[NumberFormat]], value).
auto number = format_numeric(global_object, *number_format, value);
// q. Let dataLocale be durationFormat.[[DataLocale]].
auto const& data_locale = duration_format.data_locale();
// r. Let dataLocaleData be the current value of the dataLocale slot of %DurationFormat%.[[LocaleData]].
// s. If style is "2-digit" or "numeric", then
if (style == DurationFormat::ValueStyle::TwoDigit || style == DurationFormat::ValueStyle::Numeric) {
// i. Append the new Record { [[Type]]: unit, [[Value]]: num} to the end of result.
result.append({ unit, number });
// ii. If unit is "hours" or "minutes", then
if (unit.is_one_of("hours"sv, "minutes"sv)) {
// 1. Let separator be dataLocaleData.[[formats]].[[digital]].[[separator]].
auto separator = Unicode::get_number_system_symbol(data_locale, duration_format.numbering_system(), Unicode::NumericSymbol::TimeSeparator).value_or(":"sv);
// 2. Append the new Record { [[Type]]: "literal", [[Value]]: separator} to the end of result.
result.append({ "literal", separator });
}
}
// t. Else,
else {
// i. Let pr be ? Construct(%PluralRules%, « durationFormat.[[Locale]] »).
// ii. Let prv be ! ResolvePlural(pr, value).
// FIXME: Use ResolvePlural when Intl.PluralRules is implemented.
auto formats = Unicode::get_unit_formats(data_locale, duration_instances_component.unit_singular, static_cast<Unicode::Style>(style));
auto pattern = Unicode::select_pattern_with_plurality(formats, value.as_double()).release_value();
// iii. Let template be the current value of the prv slot of the unit slot of the style slot of dataLocaleData.[[formats]].
auto template_ = convert_number_format_pattern_to_duration_format_template(pattern);
// FIXME: MakePartsList takes a list, not a string, so likely missing spec step: Let fv be ! PartitionNumberPattern(nf, value).
auto formatted_value = partition_number_pattern(global_object, *number_format, value);
// FIXME: Spec issue - see above, fv instead of num
// iv. Let parts be ! MakePartsList(template, unit, num).
auto parts = make_parts_list(template_, unit, formatted_value);
// v. Let concat be an empty String.
StringBuilder concat;
// vi. For each element part in parts, in List order, do
for (auto const& part : parts) {
// 1. Set concat to the string-concatenation of concat and part.[[Value]].
concat.append(part.value);
// FIXME: It's not clear why this step is here, the unit is functional, it should not be part of the final formatted text
// 2. If part has a [[Unit]] field, then
// if (!part.unit.is_null()) {
// // a. Set concat to the string-concatenation of concat and part.[[Unit]].
// concat.append(part.unit);
// }
}
// vii. Append the new Record { [[Type]]: unit, [[Value]]: concat } to the end of result.
result.append({ unit, concat.build() });
}
// u. If done is true, then
if (done) {
// i. Stop iteration.
break;
}
}
// 3. Let lf be ? Construct(%ListFormat%, « durationFormat.[[Locale]] »).
auto* list_format = static_cast<Intl::ListFormat*>(TRY(construct(global_object, *global_object.intl_list_format_constructor(), js_string(vm, duration_format.locale()))));
// 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
Vector<String> string_result;
bool merge = false;
for (size_t i = 0; i < result.size(); ++i) {
auto const& part = result[i];
if (part.type == "literal") {
string_result.last() = String::formatted("{}{}", string_result.last(), part.value);
merge = true;
continue;
}
if (merge) {
string_result.last() = String::formatted("{}{}", string_result.last(), part.value);
merge = false;
continue;
}
string_result.append(part.value);
}
// 4. Set result to ! CreatePartsFromList(lf, result).
auto final_result = create_parts_from_list(*list_format, string_result);
// 5. Return result.
return final_result;
}
}

View file

@ -32,6 +32,9 @@ public:
Numeric,
TwoDigit
};
static_assert(to_underlying(ValueStyle::Long) == to_underlying(Unicode::Style::Long));
static_assert(to_underlying(ValueStyle::Short) == to_underlying(Unicode::Style::Short));
static_assert(to_underlying(ValueStyle::Narrow) == to_underlying(Unicode::Style::Narrow));
enum class Display {
Auto,
@ -61,63 +64,83 @@ public:
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); }
ValueStyle years_style() const { return m_years_style; }
StringView years_style_string() const { return value_style_to_string(m_years_style); }
void set_years_display(StringView years_display) { m_years_display = display_from_string(years_display); }
Display years_display() const { return m_years_display; }
StringView years_display_string() const { return display_to_string(m_years_display); }
void set_months_style(StringView months_style) { m_months_style = date_style_from_string(months_style); }
ValueStyle months_style() const { return m_months_style; }
StringView months_style_string() const { return value_style_to_string(m_months_style); }
void set_months_display(StringView months_display) { m_months_display = display_from_string(months_display); }
Display months_display() const { return m_months_display; }
StringView months_display_string() const { return display_to_string(m_months_display); }
void set_weeks_style(StringView weeks_style) { m_weeks_style = date_style_from_string(weeks_style); }
ValueStyle weeks_style() const { return m_weeks_style; }
StringView weeks_style_string() const { return value_style_to_string(m_weeks_style); }
void set_weeks_display(StringView weeks_display) { m_weeks_display = display_from_string(weeks_display); }
Display weeks_display() const { return m_weeks_display; }
StringView weeks_display_string() const { return display_to_string(m_weeks_display); }
void set_days_style(StringView days_style) { m_days_style = date_style_from_string(days_style); }
ValueStyle days_style() const { return m_days_style; }
StringView days_style_string() const { return value_style_to_string(m_days_style); }
void set_days_display(StringView days_display) { m_days_display = display_from_string(days_display); }
Display days_display() const { return m_days_display; }
StringView days_display_string() const { return display_to_string(m_days_display); }
void set_hours_style(StringView hours_style) { m_hours_style = time_style_from_string(hours_style); }
ValueStyle hours_style() const { return m_hours_style; }
StringView hours_style_string() const { return value_style_to_string(m_hours_style); }
void set_hours_display(StringView hours_display) { m_hours_display = display_from_string(hours_display); }
Display hours_display() const { return m_hours_display; }
StringView hours_display_string() const { return display_to_string(m_hours_display); }
void set_minutes_style(StringView minutes_style) { m_minutes_style = time_style_from_string(minutes_style); }
ValueStyle minutes_style() const { return m_minutes_style; }
StringView minutes_style_string() const { return value_style_to_string(m_minutes_style); }
void set_minutes_display(StringView minutes_display) { m_minutes_display = display_from_string(minutes_display); }
Display minutes_display() const { return m_minutes_display; }
StringView minutes_display_string() const { return display_to_string(m_minutes_display); }
void set_seconds_style(StringView seconds_style) { m_seconds_style = time_style_from_string(seconds_style); }
ValueStyle seconds_style() const { return m_seconds_style; }
StringView seconds_style_string() const { return value_style_to_string(m_seconds_style); }
void set_seconds_display(StringView seconds_display) { m_seconds_display = display_from_string(seconds_display); }
Display seconds_display() const { return m_seconds_display; }
StringView seconds_display_string() const { return display_to_string(m_seconds_display); }
void set_milliseconds_style(StringView milliseconds_style) { m_milliseconds_style = sub_second_style_from_string(milliseconds_style); }
ValueStyle milliseconds_style() const { return m_milliseconds_style; }
StringView milliseconds_style_string() const { return value_style_to_string(m_milliseconds_style); }
void set_milliseconds_display(StringView milliseconds_display) { m_milliseconds_display = display_from_string(milliseconds_display); }
Display milliseconds_display() const { return m_milliseconds_display; }
StringView milliseconds_display_string() const { return display_to_string(m_milliseconds_display); }
void set_microseconds_style(StringView microseconds_style) { m_microseconds_style = sub_second_style_from_string(microseconds_style); }
ValueStyle microseconds_style() const { return m_microseconds_style; }
StringView microseconds_style_string() const { return value_style_to_string(m_microseconds_style); }
void set_microseconds_display(StringView microseconds_display) { m_microseconds_display = display_from_string(microseconds_display); }
Display microseconds_display() const { return m_microseconds_display; }
StringView microseconds_display_string() const { return display_to_string(m_microseconds_display); }
void set_nanoseconds_style(StringView nanoseconds_style) { m_nanoseconds_style = sub_second_style_from_string(nanoseconds_style); }
ValueStyle nanoseconds_style() const { return m_nanoseconds_style; }
StringView nanoseconds_style_string() const { return value_style_to_string(m_nanoseconds_style); }
void set_nanoseconds_display(StringView nanoseconds_display) { m_nanoseconds_display = display_from_string(nanoseconds_display); }
Display nanoseconds_display() const { return m_nanoseconds_display; }
StringView nanoseconds_display_string() const { return display_to_string(m_nanoseconds_display); }
void set_fractional_digits(Optional<u8> fractional_digits) { m_fractional_digits = move(fractional_digits); }
@ -163,9 +186,12 @@ private:
struct DurationInstanceComponent {
double Temporal::DurationRecord::*value_slot;
DurationFormat::ValueStyle (DurationFormat::*get_style_slot)() const;
void (DurationFormat::*set_style_slot)(StringView);
DurationFormat::Display (DurationFormat::*get_display_slot)() const;
void (DurationFormat::*set_display_slot)(StringView);
StringView unit;
StringView unit_singular;
Span<StringView const> values;
StringView digital_default;
};
@ -175,16 +201,16 @@ static constexpr AK::Array<StringView, 3> date_values = { "long"sv, "short"sv, "
static constexpr AK::Array<StringView, 5> time_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv, "2-digit"sv };
static constexpr AK::Array<StringView, 4> sub_second_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv };
static constexpr AK::Array<DurationInstanceComponent, 10> duration_instances_components {
DurationInstanceComponent { &Temporal::DurationRecord::years, &DurationFormat::set_years_style, &DurationFormat::set_years_display, "years"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::months, &DurationFormat::set_months_style, &DurationFormat::set_months_display, "months"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::weeks, &DurationFormat::set_weeks_style, &DurationFormat::set_weeks_display, "weeks"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::days, &DurationFormat::set_days_style, &DurationFormat::set_days_display, "days"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::hours, &DurationFormat::set_hours_style, &DurationFormat::set_hours_display, "hours"sv, time_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::minutes, &DurationFormat::set_minutes_style, &DurationFormat::set_minutes_display, "minutes"sv, time_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::seconds, &DurationFormat::set_seconds_style, &DurationFormat::set_seconds_display, "seconds"sv, time_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::milliseconds, &DurationFormat::set_milliseconds_style, &DurationFormat::set_milliseconds_display, "milliseconds"sv, sub_second_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::microseconds, &DurationFormat::set_microseconds_style, &DurationFormat::set_microseconds_display, "microseconds"sv, sub_second_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::nanoseconds, &DurationFormat::set_nanoseconds_style, &DurationFormat::set_nanoseconds_display, "nanoseconds"sv, sub_second_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::years, &DurationFormat::years_style, &DurationFormat::set_years_style, &DurationFormat::years_display, &DurationFormat::set_years_display, "years"sv, "year"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::months, &DurationFormat::months_style, &DurationFormat::set_months_style, &DurationFormat::months_display, &DurationFormat::set_months_display, "months"sv, "month"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::weeks, &DurationFormat::weeks_style, &DurationFormat::set_weeks_style, &DurationFormat::weeks_display, &DurationFormat::set_weeks_display, "weeks"sv, "week"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::days, &DurationFormat::days_style, &DurationFormat::set_days_style, &DurationFormat::days_display, &DurationFormat::set_days_display, "days"sv, "day"sv, date_values, "narrow"sv },
DurationInstanceComponent { &Temporal::DurationRecord::hours, &DurationFormat::hours_style, &DurationFormat::set_hours_style, &DurationFormat::hours_display, &DurationFormat::set_hours_display, "hours"sv, "hour"sv, time_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::minutes, &DurationFormat::minutes_style, &DurationFormat::set_minutes_style, &DurationFormat::minutes_display, &DurationFormat::set_minutes_display, "minutes"sv, "minute"sv, time_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::seconds, &DurationFormat::seconds_style, &DurationFormat::set_seconds_style, &DurationFormat::seconds_display, &DurationFormat::set_seconds_display, "seconds"sv, "second"sv, time_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::milliseconds, &DurationFormat::milliseconds_style, &DurationFormat::set_milliseconds_style, &DurationFormat::milliseconds_display, &DurationFormat::set_milliseconds_display, "milliseconds"sv, "millisecond"sv, sub_second_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::microseconds, &DurationFormat::microseconds_style, &DurationFormat::set_microseconds_style, &DurationFormat::microseconds_display, &DurationFormat::set_microseconds_display, "microseconds"sv, "microsecond"sv, sub_second_values, "numeric"sv },
DurationInstanceComponent { &Temporal::DurationRecord::nanoseconds, &DurationFormat::nanoseconds_style, &DurationFormat::set_nanoseconds_style, &DurationFormat::nanoseconds_display, &DurationFormat::set_nanoseconds_display, "nanoseconds"sv, "nanosecond"sv, sub_second_values, "numeric"sv },
};
struct DurationUnitOptions {
@ -194,5 +220,6 @@ struct DurationUnitOptions {
ThrowCompletionOr<Temporal::DurationRecord> to_duration_record(GlobalObject& global_object, Value input);
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(GlobalObject& global_object, String const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, Optional<String> const& previous_style);
ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(GlobalObject& global_object, DurationFormat const& duration_format, Temporal::DurationRecord const& duration);
}