mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-22 09:12:13 -05:00
LibJS: Use enums instead of strings in Intl.DurationFormat AOs
This commit is contained in:
parent
37fc9b6511
commit
5cfd50224c
Notes:
github-actions[bot]
2024-12-04 13:02:51 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/5cfd50224c4 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2731 Reviewed-by: https://github.com/shannonbooth
4 changed files with 184 additions and 141 deletions
|
@ -586,6 +586,7 @@ namespace JS {
|
|||
P(weekday) \
|
||||
P(weekend) \
|
||||
P(weekOfYear) \
|
||||
P(week) \
|
||||
P(weeks) \
|
||||
P(weeksDisplay) \
|
||||
P(with) \
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/GenericShorthands.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
|
@ -15,7 +16,6 @@
|
|||
#include <LibJS/Runtime/Intl/PluralRules.h>
|
||||
#include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
|
||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||
#include <LibJS/Runtime/ValueInlines.h>
|
||||
|
||||
|
@ -58,49 +58,6 @@ StringView DurationFormat::style_to_string(Style style)
|
|||
}
|
||||
}
|
||||
|
||||
DurationFormat::ValueStyle DurationFormat::date_style_from_string(StringView date_style)
|
||||
{
|
||||
if (date_style == "long"sv)
|
||||
return ValueStyle::Long;
|
||||
if (date_style == "short"sv)
|
||||
return ValueStyle::Short;
|
||||
if (date_style == "narrow"sv)
|
||||
return ValueStyle::Narrow;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
DurationFormat::ValueStyle DurationFormat::time_style_from_string(StringView time_style)
|
||||
{
|
||||
if (time_style == "long"sv)
|
||||
return ValueStyle::Long;
|
||||
if (time_style == "short"sv)
|
||||
return ValueStyle::Short;
|
||||
if (time_style == "narrow"sv)
|
||||
return ValueStyle::Narrow;
|
||||
if (time_style == "numeric"sv)
|
||||
return ValueStyle::Numeric;
|
||||
if (time_style == "2-digit"sv)
|
||||
return ValueStyle::TwoDigit;
|
||||
if (time_style == "fractional"sv)
|
||||
return ValueStyle::Fractional;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
DurationFormat::ValueStyle DurationFormat::sub_second_style_from_string(StringView sub_second_style)
|
||||
{
|
||||
if (sub_second_style == "long"sv)
|
||||
return ValueStyle::Long;
|
||||
if (sub_second_style == "short"sv)
|
||||
return ValueStyle::Short;
|
||||
if (sub_second_style == "narrow"sv)
|
||||
return ValueStyle::Narrow;
|
||||
if (sub_second_style == "numeric"sv)
|
||||
return ValueStyle::Numeric;
|
||||
if (sub_second_style == "fractional"sv)
|
||||
return ValueStyle::Fractional;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
DurationFormat::Display DurationFormat::display_from_string(StringView display)
|
||||
{
|
||||
if (display == "auto"sv)
|
||||
|
@ -110,6 +67,23 @@ DurationFormat::Display DurationFormat::display_from_string(StringView display)
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
DurationFormat::ValueStyle DurationFormat::value_style_from_string(StringView value_style)
|
||||
{
|
||||
if (value_style == "long"sv)
|
||||
return ValueStyle::Long;
|
||||
if (value_style == "short"sv)
|
||||
return ValueStyle::Short;
|
||||
if (value_style == "narrow"sv)
|
||||
return ValueStyle::Narrow;
|
||||
if (value_style == "numeric"sv)
|
||||
return ValueStyle::Numeric;
|
||||
if (value_style == "2-digit"sv)
|
||||
return ValueStyle::TwoDigit;
|
||||
if (value_style == "fractional"sv)
|
||||
return ValueStyle::Fractional;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
StringView DurationFormat::value_style_to_string(ValueStyle value_style)
|
||||
{
|
||||
switch (value_style) {
|
||||
|
@ -141,6 +115,60 @@ StringView DurationFormat::display_to_string(Display display)
|
|||
}
|
||||
}
|
||||
|
||||
static PropertyKey const& unit_to_property_key(VM& vm, DurationFormat::Unit unit)
|
||||
{
|
||||
switch (unit) {
|
||||
case DurationFormat::Unit::Years:
|
||||
return vm.names.years;
|
||||
case DurationFormat::Unit::Months:
|
||||
return vm.names.months;
|
||||
case DurationFormat::Unit::Weeks:
|
||||
return vm.names.weeks;
|
||||
case DurationFormat::Unit::Days:
|
||||
return vm.names.days;
|
||||
case DurationFormat::Unit::Hours:
|
||||
return vm.names.hours;
|
||||
case DurationFormat::Unit::Minutes:
|
||||
return vm.names.minutes;
|
||||
case DurationFormat::Unit::Seconds:
|
||||
return vm.names.seconds;
|
||||
case DurationFormat::Unit::Milliseconds:
|
||||
return vm.names.milliseconds;
|
||||
case DurationFormat::Unit::Microseconds:
|
||||
return vm.names.microseconds;
|
||||
case DurationFormat::Unit::Nanoseconds:
|
||||
return vm.names.nanoseconds;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
static PropertyKey const& unit_to_number_format_property_key(VM& vm, DurationFormat::Unit unit)
|
||||
{
|
||||
switch (unit) {
|
||||
case DurationFormat::Unit::Years:
|
||||
return vm.names.year;
|
||||
case DurationFormat::Unit::Months:
|
||||
return vm.names.month;
|
||||
case DurationFormat::Unit::Weeks:
|
||||
return vm.names.week;
|
||||
case DurationFormat::Unit::Days:
|
||||
return vm.names.day;
|
||||
case DurationFormat::Unit::Hours:
|
||||
return vm.names.hour;
|
||||
case DurationFormat::Unit::Minutes:
|
||||
return vm.names.minute;
|
||||
case DurationFormat::Unit::Seconds:
|
||||
return vm.names.second;
|
||||
case DurationFormat::Unit::Milliseconds:
|
||||
return vm.names.millisecond;
|
||||
case DurationFormat::Unit::Microseconds:
|
||||
return vm.names.microsecond;
|
||||
case DurationFormat::Unit::Nanoseconds:
|
||||
return vm.names.nanosecond;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
static GC::Ref<NumberFormat> construct_number_format(VM& vm, DurationFormat const& duration_format, GC::Ref<Object> options)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -260,11 +288,13 @@ i8 duration_sign(DurationRecord const& duration)
|
|||
}
|
||||
|
||||
// 1.1.6 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle, twoDigitHours ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions
|
||||
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String const& unit, Object const& options, StringView base_style, ReadonlySpan<StringView> styles_list, StringView digital_base, StringView previous_style, bool two_digit_hours)
|
||||
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, DurationFormat::Unit unit, Object const& options, DurationFormat::Style base_style, ReadonlySpan<StringView> styles_list, DurationFormat::ValueStyle digital_base, Optional<DurationFormat::ValueStyle> previous_style, bool two_digit_hours)
|
||||
{
|
||||
auto const& unit_property_key = unit_to_property_key(vm, unit);
|
||||
|
||||
// 1. Let style be ? GetOption(options, unit, STRING, stylesList, undefined).
|
||||
auto style_value = TRY(get_option(vm, options, unit.to_byte_string(), OptionType::String, styles_list, Empty {}));
|
||||
StringView style;
|
||||
auto style_value = TRY(get_option(vm, options, unit_property_key, OptionType::String, styles_list, Empty {}));
|
||||
DurationFormat::ValueStyle style;
|
||||
|
||||
// 2. Let displayDefault be "always".
|
||||
auto display_default = "always"sv;
|
||||
|
@ -272,9 +302,9 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String
|
|||
// 3. If style is undefined, then
|
||||
if (style_value.is_undefined()) {
|
||||
// a. If baseStyle is "digital", then
|
||||
if (base_style == "digital"sv) {
|
||||
if (base_style == DurationFormat::Style::Digital) {
|
||||
// i. If unit is not one of "hours", "minutes", or "seconds", then
|
||||
if (!unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv)) {
|
||||
if (!first_is_one_of(unit, DurationFormat::Unit::Hours, DurationFormat::Unit::Minutes, DurationFormat::Unit::Seconds)) {
|
||||
// 1. Set displayDefault to "auto".
|
||||
display_default = "auto"sv;
|
||||
}
|
||||
|
@ -285,15 +315,15 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String
|
|||
// b. Else,
|
||||
else {
|
||||
// i. If prevStyle is "fractional", "numeric" or "2-digit", then
|
||||
if (previous_style.is_one_of("fractional"sv, "numeric"sv, "2-digit"sv)) {
|
||||
if (first_is_one_of(previous_style, DurationFormat::ValueStyle::Fractional, DurationFormat::ValueStyle::Numeric, DurationFormat::ValueStyle::TwoDigit)) {
|
||||
// 1. If unit is not one of "minutes" or "seconds", then
|
||||
if (!unit.is_one_of("minutes"sv, "seconds"sv)) {
|
||||
if (!first_is_one_of(unit, DurationFormat::Unit::Minutes, DurationFormat::Unit::Seconds)) {
|
||||
// a. Set displayDefault to "auto".
|
||||
display_default = "auto"sv;
|
||||
}
|
||||
|
||||
// 2. Set style to "numeric".
|
||||
style = "numeric"sv;
|
||||
style = DurationFormat::ValueStyle::Numeric;
|
||||
}
|
||||
// ii. Else,
|
||||
else {
|
||||
|
@ -301,19 +331,19 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String
|
|||
display_default = "auto"sv;
|
||||
|
||||
// 2. Set style to baseStyle.
|
||||
style = base_style;
|
||||
style = static_cast<DurationFormat::ValueStyle>(base_style);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
style = style_value.as_string().utf8_string_view();
|
||||
style = DurationFormat::value_style_from_string(style_value.as_string().utf8_string_view());
|
||||
}
|
||||
|
||||
// 4. If style is "numeric", then
|
||||
if (style == "numeric"sv) {
|
||||
if (style == DurationFormat::ValueStyle::Numeric) {
|
||||
// a. If unit is one of "milliseconds", "microseconds", or "nanoseconds", then
|
||||
if (unit.is_one_of("milliseconds"sv, "microseconds"sv, "nanoseconds"sv)) {
|
||||
if (first_is_one_of(unit, DurationFormat::Unit::Milliseconds, DurationFormat::Unit::Microseconds, DurationFormat::Unit::Nanoseconds)) {
|
||||
// i. Set style to "fractional".
|
||||
style = "fractional"sv;
|
||||
style = DurationFormat::ValueStyle::Fractional;
|
||||
|
||||
// ii. Set displayDefault to "auto".
|
||||
display_default = "auto"sv;
|
||||
|
@ -321,50 +351,50 @@ ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String
|
|||
}
|
||||
|
||||
// 5. Let displayField be the string-concatenation of unit and "Display".
|
||||
auto display_field = MUST(String::formatted("{}Display", unit));
|
||||
auto display_field = MUST(String::formatted("{}Display", unit_property_key));
|
||||
|
||||
// 6. Let display be ? GetOption(options, displayField, STRING, « "auto", "always" », displayDefault).
|
||||
auto display_value = TRY(get_option(vm, options, display_field.to_byte_string(), OptionType::String, { "auto"sv, "always"sv }, display_default));
|
||||
auto display = display_value.as_string().utf8_string();
|
||||
auto display = DurationFormat::display_from_string(display_value.as_string().utf8_string());
|
||||
|
||||
// 7. If display is "always" and style is "fractional", then
|
||||
if (display == "always"sv && style == "fractional"sv) {
|
||||
if (display == DurationFormat::Display::Always && style == DurationFormat::ValueStyle::Fractional) {
|
||||
// a. Throw a RangeError exception.
|
||||
return vm.throw_completion<RangeError>(ErrorType::IntlFractionalUnitsMixedWithAlwaysDisplay, unit, display_field);
|
||||
return vm.throw_completion<RangeError>(ErrorType::IntlFractionalUnitsMixedWithAlwaysDisplay, unit_property_key, display_field);
|
||||
}
|
||||
|
||||
// 8. If prevStyle is "fractional", then
|
||||
if (previous_style == "fractional"sv) {
|
||||
if (previous_style == DurationFormat::ValueStyle::Fractional) {
|
||||
// a. If style is not "fractional", then
|
||||
if (style != "fractional"sv) {
|
||||
if (style != DurationFormat::ValueStyle::Fractional) {
|
||||
// i. Throw a RangeError exception.
|
||||
return vm.throw_completion<RangeError>(ErrorType::IntlFractionalUnitFollowedByNonFractionalUnit, unit);
|
||||
return vm.throw_completion<RangeError>(ErrorType::IntlFractionalUnitFollowedByNonFractionalUnit, unit_property_key);
|
||||
}
|
||||
}
|
||||
|
||||
// 9. If prevStyle is "numeric" or "2-digit", then
|
||||
if (previous_style.is_one_of("numeric"sv, "2-digit"sv)) {
|
||||
if (first_is_one_of(previous_style, DurationFormat::ValueStyle::Numeric, DurationFormat::ValueStyle::TwoDigit)) {
|
||||
// a. If style is not "fractional", "numeric" or "2-digit", then
|
||||
if (!style.is_one_of("fractional"sv, "numeric"sv, "2-digit"sv)) {
|
||||
if (!first_is_one_of(style, DurationFormat::ValueStyle::Fractional, DurationFormat::ValueStyle::Numeric, DurationFormat::ValueStyle::TwoDigit)) {
|
||||
// i. Throw a RangeError exception.
|
||||
return vm.throw_completion<RangeError>(ErrorType::IntlNonNumericOr2DigitAfterNumericOr2Digit);
|
||||
}
|
||||
|
||||
// b. If unit is "minutes" or "seconds", then
|
||||
if (unit.is_one_of("minutes"sv, "seconds"sv)) {
|
||||
if (first_is_one_of(unit, DurationFormat::Unit::Minutes, DurationFormat::Unit::Seconds)) {
|
||||
// i. Set style to "2-digit".
|
||||
style = "2-digit"sv;
|
||||
style = DurationFormat::ValueStyle::TwoDigit;
|
||||
}
|
||||
}
|
||||
|
||||
// 10. If unit is "hours" and twoDigitHours is true, then
|
||||
if (unit == "hours"sv && two_digit_hours) {
|
||||
if (unit == DurationFormat::Unit::Hours && two_digit_hours) {
|
||||
// a. Set style to "2-digit".
|
||||
style = "2-digit"sv;
|
||||
style = DurationFormat::ValueStyle::TwoDigit;
|
||||
}
|
||||
|
||||
// 11. Return the Record { [[Style]]: style, [[Display]]: display }.
|
||||
return DurationUnitOptions { .style = MUST(String::from_utf8(style)), .display = move(display) };
|
||||
return DurationUnitOptions { .style = style, .display = display };
|
||||
}
|
||||
|
||||
// 1.1.7 ComputeFractionalDigits ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-computefractionaldigits
|
||||
|
@ -384,7 +414,7 @@ double compute_fractional_digits(DurationFormat const& duration_format, Duration
|
|||
// b. If style is "fractional", then
|
||||
if (style == DurationFormat::ValueStyle::Fractional) {
|
||||
// i. Assert: The Unit value of the current row is "milliseconds", "microseconds", or "nanoseconds".
|
||||
VERIFY(duration_instances_component.unit.is_one_of("milliseconds"sv, "microseconds"sv, "nanoseconds"sv));
|
||||
VERIFY(first_is_one_of(duration_instances_component.unit, DurationFormat::Unit::Milliseconds, DurationFormat::Unit::Microseconds, DurationFormat::Unit::Nanoseconds));
|
||||
|
||||
// ii. Let value be the value of duration's field whose name is the Value Field value of the current row.
|
||||
auto value = duration.*duration_instances_component.value_slot;
|
||||
|
@ -405,21 +435,21 @@ double compute_fractional_digits(DurationFormat const& duration_format, Duration
|
|||
}
|
||||
|
||||
// 1.1.8 NextUnitFractional ( durationFormat, unit ), https://tc39.es/proposal-intl-duration-format/#sec-nextunitfractional
|
||||
bool next_unit_fractional(DurationFormat const& duration_format, StringView unit)
|
||||
bool next_unit_fractional(DurationFormat const& duration_format, DurationFormat::Unit unit)
|
||||
{
|
||||
// 1. Assert: unit is "seconds", "milliseconds", or "microseconds".
|
||||
VERIFY(unit.is_one_of("seconds"sv, "milliseconds"sv, "microseconds"sv));
|
||||
VERIFY(first_is_one_of(unit, DurationFormat::Unit::Milliseconds, DurationFormat::Unit::Microseconds, DurationFormat::Unit::Nanoseconds));
|
||||
|
||||
// 2. If unit is "seconds" and durationFormat.[[MillisecondsStyle]] is "fractional", return true.
|
||||
if (unit == "seconds"sv && duration_format.milliseconds_style() == DurationFormat::ValueStyle::Fractional)
|
||||
if (unit == DurationFormat::Unit::Seconds && duration_format.milliseconds_style() == DurationFormat::ValueStyle::Fractional)
|
||||
return true;
|
||||
|
||||
// 3. Else if unit is "milliseconds" and durationFormat.[[MicrosecondsStyle]] is "fractional", return true.
|
||||
if (unit == "milliseconds"sv && duration_format.microseconds_style() == DurationFormat::ValueStyle::Fractional)
|
||||
if (unit == DurationFormat::Unit::Milliseconds && duration_format.microseconds_style() == DurationFormat::ValueStyle::Fractional)
|
||||
return true;
|
||||
|
||||
// 4. Else if unit is "microseconds" and durationFormat.[[NanosecondsStyle]] is "fractional", return true.
|
||||
if (unit == "microseconds"sv && duration_format.nanoseconds_style() == DurationFormat::ValueStyle::Fractional)
|
||||
if (unit == DurationFormat::Unit::Microseconds && duration_format.nanoseconds_style() == DurationFormat::ValueStyle::Fractional)
|
||||
return true;
|
||||
|
||||
// 5. Return false.
|
||||
|
@ -642,10 +672,10 @@ Vector<DurationFormatPart> format_numeric_seconds(VM& vm, DurationFormat const&
|
|||
}
|
||||
|
||||
// 1.1.12 FormatNumericUnits ( durationFormat, duration, firstNumericUnit, signDisplayed ), https://tc39.es/proposal-intl-duration-format/#sec-formatnumericunits
|
||||
Vector<DurationFormatPart> format_numeric_units(VM& vm, DurationFormat const& duration_format, DurationRecord const& duration, StringView first_numeric_unit, bool sign_displayed)
|
||||
Vector<DurationFormatPart> format_numeric_units(VM& vm, DurationFormat const& duration_format, DurationRecord const& duration, DurationFormat::Unit first_numeric_unit, bool sign_displayed)
|
||||
{
|
||||
// 1. Assert: firstNumericUnit is "hours", "minutes", or "seconds".
|
||||
VERIFY(first_numeric_unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv));
|
||||
VERIFY(first_is_one_of(first_numeric_unit, DurationFormat::Unit::Hours, DurationFormat::Unit::Minutes, DurationFormat::Unit::Seconds));
|
||||
|
||||
// 2. Let numericPartsList be a new empty List.
|
||||
Vector<DurationFormatPart> numeric_parts_list;
|
||||
|
@ -678,7 +708,7 @@ Vector<DurationFormatPart> format_numeric_units(VM& vm, DurationFormat const& du
|
|||
auto hours_formatted = false;
|
||||
|
||||
// 11. If firstNumericUnit is "hours", then
|
||||
if (first_numeric_unit == "hours"sv) {
|
||||
if (first_numeric_unit == DurationFormat::Unit::Hours) {
|
||||
// a. If hoursValue is not 0 or hoursDisplay is "always", then
|
||||
if (hours_value != 0 || hours_display == DurationFormat::Display::Always) {
|
||||
// i. Set hoursFormatted to true.
|
||||
|
@ -696,7 +726,7 @@ Vector<DurationFormatPart> format_numeric_units(VM& vm, DurationFormat const& du
|
|||
auto minutes_formatted = false;
|
||||
|
||||
// 15. If firstNumericUnit is "hours" or firstNumericUnit is "minutes", then
|
||||
if (first_numeric_unit.is_one_of("hours"sv, "minutes"sv)) {
|
||||
if (first_is_one_of(first_numeric_unit, DurationFormat::Unit::Hours, DurationFormat::Unit::Minutes)) {
|
||||
// a. If hoursFormatted is true and secondsFormatted is true, then
|
||||
if (hours_formatted && seconds_formatted) {
|
||||
// i. Set minutesFormatted to true.
|
||||
|
@ -907,7 +937,7 @@ Vector<DurationFormatPart> partition_duration_format_pattern(VM& vm, DurationFor
|
|||
auto number_format_options = Object::create(realm, nullptr);
|
||||
|
||||
// ii. If unit is "seconds", "milliseconds", or "microseconds", then
|
||||
if (unit.is_one_of("seconds"sv, "milliseconds"sv, "microseconds"sv)) {
|
||||
if (first_is_one_of(unit, DurationFormat::Unit::Milliseconds, DurationFormat::Unit::Microseconds, DurationFormat::Unit::Nanoseconds)) {
|
||||
// 1. If NextUnitFractional(durationFormat, unit) is true, then
|
||||
if (next_unit_fractional(duration_format, unit)) {
|
||||
// a. Set value to value + ComputeFractionalDigits(durationFormat, duration).
|
||||
|
@ -973,13 +1003,13 @@ Vector<DurationFormatPart> partition_duration_format_pattern(VM& vm, DurationFor
|
|||
}
|
||||
|
||||
// 5. Let numberFormatUnit be the NumberFormat Unit value of the current row.
|
||||
auto number_format_unit = duration_instances_component.number_format_unit;
|
||||
auto const& number_format_unit = unit_to_number_format_property_key(vm, duration_instances_component.unit);
|
||||
|
||||
// 6. Perform ! CreateDataPropertyOrThrow(nfOpts, "style", "unit").
|
||||
MUST(number_format_options->create_data_property_or_throw(vm.names.style, PrimitiveString::create(vm, "unit"sv)));
|
||||
|
||||
// 7. Perform ! CreateDataPropertyOrThrow(nfOpts, "unit", numberFormatUnit).
|
||||
MUST(number_format_options->create_data_property_or_throw(vm.names.unit, PrimitiveString::create(vm, number_format_unit)));
|
||||
MUST(number_format_options->create_data_property_or_throw(vm.names.unit, PrimitiveString::create(vm, number_format_unit.as_string())));
|
||||
|
||||
// 8. Perform ! CreateDataPropertyOrThrow(nfOpts, "unitDisplay", style).
|
||||
auto locale_style = Unicode::style_to_string(static_cast<Unicode::Style>(style));
|
||||
|
@ -999,7 +1029,7 @@ Vector<DurationFormatPart> partition_duration_format_pattern(VM& vm, DurationFor
|
|||
|
||||
for (auto& part : parts) {
|
||||
// a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: numberFormatUnit } to list.
|
||||
list.unchecked_append({ .type = part.type, .value = move(part.value), .unit = number_format_unit });
|
||||
list.unchecked_append({ .type = part.type, .value = move(part.value), .unit = number_format_unit.as_string() });
|
||||
}
|
||||
|
||||
// 13. Append list to result.
|
||||
|
|
|
@ -24,8 +24,10 @@ public:
|
|||
Long,
|
||||
Short,
|
||||
Narrow,
|
||||
Digital
|
||||
Digital,
|
||||
};
|
||||
static Style style_from_string(StringView style);
|
||||
static StringView style_to_string(Style);
|
||||
|
||||
enum class ValueStyle {
|
||||
Long,
|
||||
|
@ -35,13 +37,31 @@ public:
|
|||
TwoDigit,
|
||||
Fractional,
|
||||
};
|
||||
static ValueStyle value_style_from_string(StringView);
|
||||
static StringView value_style_to_string(ValueStyle);
|
||||
|
||||
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,
|
||||
Always
|
||||
Always,
|
||||
};
|
||||
static Display display_from_string(StringView display);
|
||||
static StringView display_to_string(Display);
|
||||
|
||||
enum class Unit {
|
||||
Years,
|
||||
Months,
|
||||
Weeks,
|
||||
Days,
|
||||
Hours,
|
||||
Minutes,
|
||||
Seconds,
|
||||
Milliseconds,
|
||||
Microseconds,
|
||||
Nanoseconds,
|
||||
};
|
||||
|
||||
static constexpr auto relevant_extension_keys()
|
||||
|
@ -69,83 +89,83 @@ public:
|
|||
Style style() const { return m_style; }
|
||||
StringView 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); }
|
||||
void set_years_style(ValueStyle years_style) { m_years_style = 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); }
|
||||
void set_years_display(Display years_display) { m_years_display = 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); }
|
||||
void set_months_style(ValueStyle months_style) { m_months_style = 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); }
|
||||
void set_months_display(Display months_display) { m_months_display = 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); }
|
||||
void set_weeks_style(ValueStyle weeks_style) { m_weeks_style = 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); }
|
||||
void set_weeks_display(Display weeks_display) { m_weeks_display = 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); }
|
||||
void set_days_style(ValueStyle days_style) { m_days_style = 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); }
|
||||
void set_days_display(Display days_display) { m_days_display = 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); }
|
||||
void set_hours_style(ValueStyle hours_style) { m_hours_style = 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); }
|
||||
void set_hours_display(Display hours_display) { m_hours_display = 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); }
|
||||
void set_minutes_style(ValueStyle minutes_style) { m_minutes_style = 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); }
|
||||
void set_minutes_display(Display minutes_display) { m_minutes_display = 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); }
|
||||
void set_seconds_style(ValueStyle seconds_style) { m_seconds_style = 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); }
|
||||
void set_seconds_display(Display seconds_display) { m_seconds_display = 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); }
|
||||
void set_milliseconds_style(ValueStyle milliseconds_style) { m_milliseconds_style = 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); }
|
||||
void set_milliseconds_display(Display milliseconds_display) { m_milliseconds_display = 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); }
|
||||
void set_microseconds_style(ValueStyle microseconds_style) { m_microseconds_style = 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); }
|
||||
void set_microseconds_display(Display microseconds_display) { m_microseconds_display = 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); }
|
||||
void set_nanoseconds_style(ValueStyle nanoseconds_style) { m_nanoseconds_style = 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); }
|
||||
void set_nanoseconds_display(Display nanoseconds_display) { m_nanoseconds_display = nanoseconds_display; }
|
||||
Display nanoseconds_display() const { return m_nanoseconds_display; }
|
||||
StringView nanoseconds_display_string() const { return display_to_string(m_nanoseconds_display); }
|
||||
|
||||
|
@ -156,15 +176,6 @@ public:
|
|||
private:
|
||||
explicit DurationFormat(Object& prototype);
|
||||
|
||||
static Style style_from_string(StringView style);
|
||||
static StringView style_to_string(Style);
|
||||
static ValueStyle date_style_from_string(StringView date_style);
|
||||
static ValueStyle time_style_from_string(StringView time_style);
|
||||
static ValueStyle sub_second_style_from_string(StringView sub_second_style);
|
||||
static StringView value_style_to_string(ValueStyle);
|
||||
static Display display_from_string(StringView display);
|
||||
static StringView display_to_string(Display);
|
||||
|
||||
String m_locale; // [[Locale]]
|
||||
String m_numbering_system; // [[NumberingSystem]]
|
||||
String m_hour_minute_separator; // [[HourMinutesSeparator]]
|
||||
|
@ -211,13 +222,12 @@ struct DurationRecord {
|
|||
struct DurationInstanceComponent {
|
||||
double DurationRecord::*value_slot;
|
||||
DurationFormat::ValueStyle (DurationFormat::*get_style_slot)() const;
|
||||
void (DurationFormat::*set_style_slot)(StringView);
|
||||
void (DurationFormat::*set_style_slot)(DurationFormat::ValueStyle);
|
||||
DurationFormat::Display (DurationFormat::*get_display_slot)() const;
|
||||
void (DurationFormat::*set_display_slot)(StringView);
|
||||
StringView unit;
|
||||
StringView number_format_unit;
|
||||
void (DurationFormat::*set_display_slot)(DurationFormat::Display);
|
||||
DurationFormat::Unit unit;
|
||||
ReadonlySpan<StringView> values;
|
||||
StringView digital_default;
|
||||
DurationFormat::ValueStyle digital_default;
|
||||
};
|
||||
|
||||
// Table 2: DurationFormat instance internal slots and properties relevant to PartitionDurationFormatPattern, https://tc39.es/proposal-intl-duration-format/#table-partition-duration-format-pattern
|
||||
|
@ -225,22 +235,23 @@ struct DurationInstanceComponent {
|
|||
static constexpr auto date_values = AK::Array { "long"sv, "short"sv, "narrow"sv };
|
||||
static constexpr auto time_values = AK::Array { "long"sv, "short"sv, "narrow"sv, "numeric"sv, "2-digit"sv };
|
||||
static constexpr auto sub_second_values = AK::Array { "long"sv, "short"sv, "narrow"sv, "numeric"sv };
|
||||
|
||||
static constexpr auto duration_instances_components = to_array<DurationInstanceComponent>({
|
||||
{ &DurationRecord::years, &DurationFormat::years_style, &DurationFormat::set_years_style, &DurationFormat::years_display, &DurationFormat::set_years_display, "years"sv, "year"sv, date_values, "short"sv },
|
||||
{ &DurationRecord::months, &DurationFormat::months_style, &DurationFormat::set_months_style, &DurationFormat::months_display, &DurationFormat::set_months_display, "months"sv, "month"sv, date_values, "short"sv },
|
||||
{ &DurationRecord::weeks, &DurationFormat::weeks_style, &DurationFormat::set_weeks_style, &DurationFormat::weeks_display, &DurationFormat::set_weeks_display, "weeks"sv, "week"sv, date_values, "short"sv },
|
||||
{ &DurationRecord::days, &DurationFormat::days_style, &DurationFormat::set_days_style, &DurationFormat::days_display, &DurationFormat::set_days_display, "days"sv, "day"sv, date_values, "short"sv },
|
||||
{ &DurationRecord::hours, &DurationFormat::hours_style, &DurationFormat::set_hours_style, &DurationFormat::hours_display, &DurationFormat::set_hours_display, "hours"sv, "hour"sv, time_values, "numeric"sv },
|
||||
{ &DurationRecord::minutes, &DurationFormat::minutes_style, &DurationFormat::set_minutes_style, &DurationFormat::minutes_display, &DurationFormat::set_minutes_display, "minutes"sv, "minute"sv, time_values, "numeric"sv },
|
||||
{ &DurationRecord::seconds, &DurationFormat::seconds_style, &DurationFormat::set_seconds_style, &DurationFormat::seconds_display, &DurationFormat::set_seconds_display, "seconds"sv, "second"sv, time_values, "numeric"sv },
|
||||
{ &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 },
|
||||
{ &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 },
|
||||
{ &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 },
|
||||
{ &DurationRecord::years, &DurationFormat::years_style, &DurationFormat::set_years_style, &DurationFormat::years_display, &DurationFormat::set_years_display, DurationFormat::Unit::Years, date_values, DurationFormat::ValueStyle::Short },
|
||||
{ &DurationRecord::months, &DurationFormat::months_style, &DurationFormat::set_months_style, &DurationFormat::months_display, &DurationFormat::set_months_display, DurationFormat::Unit::Months, date_values, DurationFormat::ValueStyle::Short },
|
||||
{ &DurationRecord::weeks, &DurationFormat::weeks_style, &DurationFormat::set_weeks_style, &DurationFormat::weeks_display, &DurationFormat::set_weeks_display, DurationFormat::Unit::Weeks, date_values, DurationFormat::ValueStyle::Short },
|
||||
{ &DurationRecord::days, &DurationFormat::days_style, &DurationFormat::set_days_style, &DurationFormat::days_display, &DurationFormat::set_days_display, DurationFormat::Unit::Days, date_values, DurationFormat::ValueStyle::Short },
|
||||
{ &DurationRecord::hours, &DurationFormat::hours_style, &DurationFormat::set_hours_style, &DurationFormat::hours_display, &DurationFormat::set_hours_display, DurationFormat::Unit::Hours, time_values, DurationFormat::ValueStyle::Numeric },
|
||||
{ &DurationRecord::minutes, &DurationFormat::minutes_style, &DurationFormat::set_minutes_style, &DurationFormat::minutes_display, &DurationFormat::set_minutes_display, DurationFormat::Unit::Minutes, time_values, DurationFormat::ValueStyle::Numeric },
|
||||
{ &DurationRecord::seconds, &DurationFormat::seconds_style, &DurationFormat::set_seconds_style, &DurationFormat::seconds_display, &DurationFormat::set_seconds_display, DurationFormat::Unit::Seconds, time_values, DurationFormat::ValueStyle::Numeric },
|
||||
{ &DurationRecord::milliseconds, &DurationFormat::milliseconds_style, &DurationFormat::set_milliseconds_style, &DurationFormat::milliseconds_display, &DurationFormat::set_milliseconds_display, DurationFormat::Unit::Milliseconds, sub_second_values, DurationFormat::ValueStyle::Numeric },
|
||||
{ &DurationRecord::microseconds, &DurationFormat::microseconds_style, &DurationFormat::set_microseconds_style, &DurationFormat::microseconds_display, &DurationFormat::set_microseconds_display, DurationFormat::Unit::Microseconds, sub_second_values, DurationFormat::ValueStyle::Numeric },
|
||||
{ &DurationRecord::nanoseconds, &DurationFormat::nanoseconds_style, &DurationFormat::set_nanoseconds_style, &DurationFormat::nanoseconds_display, &DurationFormat::set_nanoseconds_display, DurationFormat::Unit::Nanoseconds, sub_second_values, DurationFormat::ValueStyle::Numeric },
|
||||
});
|
||||
|
||||
struct DurationUnitOptions {
|
||||
String style;
|
||||
String display;
|
||||
DurationFormat::ValueStyle style;
|
||||
DurationFormat::Display display;
|
||||
};
|
||||
|
||||
struct DurationFormatPart {
|
||||
|
@ -251,13 +262,13 @@ struct DurationFormatPart {
|
|||
|
||||
ThrowCompletionOr<DurationRecord> to_duration_record(VM&, Value input);
|
||||
i8 duration_sign(DurationRecord const&);
|
||||
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM&, String const& unit, Object const& options, StringView base_style, ReadonlySpan<StringView> styles_list, StringView digital_base, StringView previous_style, bool two_digit_hours);
|
||||
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM&, DurationFormat::Unit unit, Object const& options, DurationFormat::Style base_style, ReadonlySpan<StringView> styles_list, DurationFormat::ValueStyle digital_base, Optional<DurationFormat::ValueStyle> previous_style, bool two_digit_hours);
|
||||
double compute_fractional_digits(DurationFormat const&, DurationRecord const&);
|
||||
bool next_unit_fractional(DurationFormat const&, StringView unit);
|
||||
bool next_unit_fractional(DurationFormat const&, DurationFormat::Unit unit);
|
||||
Vector<DurationFormatPart> format_numeric_hours(VM&, DurationFormat const&, double hours_value, bool sign_displayed);
|
||||
Vector<DurationFormatPart> format_numeric_minutes(VM&, DurationFormat const&, double minutes_value, bool hours_displayed, bool sign_displayed);
|
||||
Vector<DurationFormatPart> format_numeric_seconds(VM&, DurationFormat const&, double seconds_value, bool minutes_displayed, bool sign_displayed);
|
||||
Vector<DurationFormatPart> format_numeric_units(VM&, DurationFormat const&, DurationRecord const&, StringView first_numeric_unit, bool sign_displayed);
|
||||
Vector<DurationFormatPart> format_numeric_units(VM&, DurationFormat const&, DurationRecord const&, DurationFormat::Unit first_numeric_unit, bool sign_displayed);
|
||||
Vector<DurationFormatPart> list_format_parts(VM&, DurationFormat const&, Vector<Vector<DurationFormatPart>>& partitioned_parts_list);
|
||||
Vector<DurationFormatPart> partition_duration_format_pattern(VM&, DurationFormat const&, DurationRecord const&);
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/GenericShorthands.h>
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
|
@ -119,7 +120,7 @@ ThrowCompletionOr<GC::Ref<Object>> DurationFormatConstructor::construct(Function
|
|||
duration_format->set_style(style.as_string().utf8_string_view());
|
||||
|
||||
// 24. Let prevStyle be the empty String.
|
||||
String previous_style;
|
||||
Optional<DurationFormat::ValueStyle> previous_style;
|
||||
|
||||
// 25. For each row of Table 3, except the header row, in table order, do
|
||||
for (auto const& duration_instances_component : duration_instances_components) {
|
||||
|
@ -130,7 +131,7 @@ ThrowCompletionOr<GC::Ref<Object>> DurationFormatConstructor::construct(Function
|
|||
auto display_slot = duration_instances_component.set_display_slot;
|
||||
|
||||
// c. Let unit be the Unit value of the current row.
|
||||
auto unit = MUST(String::from_utf8(duration_instances_component.unit));
|
||||
auto unit = duration_instances_component.unit;
|
||||
|
||||
// d. Let valueList be the Values value of the current row.
|
||||
auto value_list = duration_instances_component.values;
|
||||
|
@ -139,7 +140,7 @@ ThrowCompletionOr<GC::Ref<Object>> DurationFormatConstructor::construct(Function
|
|||
auto digital_base = duration_instances_component.digital_default;
|
||||
|
||||
// f. Let unitOptions be ? GetDurationUnitOptions(unit, options, style, valueList, digitalBase, prevStyle, twoDigitHours).
|
||||
auto unit_options = TRY(get_duration_unit_options(vm, unit, *options, duration_format->style_string(), value_list, digital_base, previous_style, two_digit_hours));
|
||||
auto unit_options = TRY(get_duration_unit_options(vm, unit, *options, duration_format->style(), value_list, digital_base, previous_style, two_digit_hours));
|
||||
|
||||
// g. Set the value of the styleSlot slot of durationFormat to unitOptions.[[Style]].
|
||||
(duration_format->*style_slot)(unit_options.style);
|
||||
|
@ -148,9 +149,9 @@ ThrowCompletionOr<GC::Ref<Object>> DurationFormatConstructor::construct(Function
|
|||
(duration_format->*display_slot)(unit_options.display);
|
||||
|
||||
// i. If unit is one of "hours", "minutes", "seconds", "milliseconds", or "microseconds", then
|
||||
if (unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv, "milliseconds"sv, "microseconds"sv)) {
|
||||
if (first_is_one_of(unit, DurationFormat::Unit::Hours, DurationFormat::Unit::Minutes, DurationFormat::Unit::Seconds, DurationFormat::Unit::Milliseconds, DurationFormat::Unit::Microseconds)) {
|
||||
// i. Set prevStyle to unitOptions.[[Style]].
|
||||
previous_style = move(unit_options.style);
|
||||
previous_style = unit_options.style;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue