mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:27:35 +00:00
LibJS: Modernize InitializeDateTimeFormat and simplify error handling
This is an editorial change in the Intl spec. See:
4cf1d2c
This commit is contained in:
parent
066352c9aa
commit
41df4c6353
1 changed files with 46 additions and 38 deletions
|
@ -90,7 +90,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
// 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
|
// 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
|
||||||
auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
|
auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
|
||||||
|
|
||||||
// 2. Let options be ? ToDateTimeOptions(options, "any", "date").
|
// 2. Set options to ? ToDateTimeOptions(options, "any", "date").
|
||||||
auto* options = TRY(to_date_time_options(global_object, options_value, OptionRequired::Any, OptionDefaults::Date));
|
auto* options = TRY(to_date_time_options(global_object, options_value, OptionRequired::Any, OptionDefaults::Date));
|
||||||
|
|
||||||
// 3. Let opt be a new Record.
|
// 3. Let opt be a new Record.
|
||||||
|
@ -136,7 +136,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
|
|
||||||
// 14. If hour12 is not undefined, then
|
// 14. If hour12 is not undefined, then
|
||||||
if (!hour12.is_undefined()) {
|
if (!hour12.is_undefined()) {
|
||||||
// a. Let hourCycle be null.
|
// a. Set hourCycle to null.
|
||||||
hour_cycle = js_null();
|
hour_cycle = js_null();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +151,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
// 18. Set dateTimeFormat.[[Locale]] to r.[[locale]].
|
// 18. Set dateTimeFormat.[[Locale]] to r.[[locale]].
|
||||||
date_time_format.set_locale(move(result.locale));
|
date_time_format.set_locale(move(result.locale));
|
||||||
|
|
||||||
// 19. Let calendar be r.[[ca]].
|
// 19. Set calendar to r.[[ca]].
|
||||||
// 20. Set dateTimeFormat.[[Calendar]] to calendar.
|
// 20. Set dateTimeFormat.[[Calendar]] to calendar.
|
||||||
if (result.ca.has_value())
|
if (result.ca.has_value())
|
||||||
date_time_format.set_calendar(result.ca.release_value());
|
date_time_format.set_calendar(result.ca.release_value());
|
||||||
|
@ -234,7 +234,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
|
|
||||||
// 33. If timeZone is undefined, then
|
// 33. If timeZone is undefined, then
|
||||||
if (time_zone_value.is_undefined()) {
|
if (time_zone_value.is_undefined()) {
|
||||||
// a. Set timeZone to DefaultTimeZone().
|
// a. Set timeZone to ! DefaultTimeZone().
|
||||||
time_zone = Temporal::default_time_zone();
|
time_zone = Temporal::default_time_zone();
|
||||||
}
|
}
|
||||||
// 34. Else,
|
// 34. Else,
|
||||||
|
@ -242,21 +242,25 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
// a. Set timeZone to ? ToString(timeZone).
|
// a. Set timeZone to ? ToString(timeZone).
|
||||||
time_zone = TRY(time_zone_value.to_string(global_object));
|
time_zone = TRY(time_zone_value.to_string(global_object));
|
||||||
|
|
||||||
// b. If the result of IsValidTimeZoneName(timeZone) is false, then
|
// b. If the result of ! IsValidTimeZoneName(timeZone) is false, then
|
||||||
if (!Temporal::is_valid_time_zone_name(time_zone)) {
|
if (!Temporal::is_valid_time_zone_name(time_zone)) {
|
||||||
// i. Throw a RangeError exception.
|
// i. Throw a RangeError exception.
|
||||||
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, time_zone, vm.names.timeZone);
|
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, time_zone, vm.names.timeZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
// c. Set timeZone to CanonicalizeTimeZoneName(timeZone).
|
// c. Set timeZone to ! CanonicalizeTimeZoneName(timeZone).
|
||||||
time_zone = Temporal::canonicalize_time_zone_name(time_zone);
|
time_zone = Temporal::canonicalize_time_zone_name(time_zone);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 35. Set dateTimeFormat.[[TimeZone]] to timeZone.
|
// 35. Set dateTimeFormat.[[TimeZone]] to timeZone.
|
||||||
date_time_format.set_time_zone(move(time_zone));
|
date_time_format.set_time_zone(move(time_zone));
|
||||||
|
|
||||||
// 36. For each row of Table 6, except the header row, in table order, do
|
// 36. Let hasExplicitFormatComponents be false.
|
||||||
TRY(for_each_calendar_field(global_object, format_options, [&](auto& option, auto const& property, auto const& defaults) -> ThrowCompletionOr<void> {
|
// NOTE: Instead of using a boolean, we track any explicitly provided component name for nicer exception messages.
|
||||||
|
PropertyKey const* explicit_format_component = nullptr;
|
||||||
|
|
||||||
|
// 37. For each row of Table 6, except the header row, in table order, do
|
||||||
|
TRY(for_each_calendar_field(global_object, format_options, [&](auto& option, auto const& property, auto const& values) -> ThrowCompletionOr<void> {
|
||||||
using ValueType = typename RemoveReference<decltype(option)>::ValueType;
|
using ValueType = typename RemoveReference<decltype(option)>::ValueType;
|
||||||
|
|
||||||
// a. Let prop be the name given in the Property column of the row.
|
// a. Let prop be the name given in the Property column of the row.
|
||||||
|
@ -267,61 +271,65 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
auto value = TRY(get_number_option(global_object, *options, property, 1, 3, {}));
|
auto value = TRY(get_number_option(global_object, *options, property, 1, 3, {}));
|
||||||
|
|
||||||
// d. Set formatOptions.[[<prop>]] to value.
|
// d. Set formatOptions.[[<prop>]] to value.
|
||||||
if (value.has_value())
|
if (value.has_value()) {
|
||||||
option = static_cast<ValueType>(value.value());
|
option = static_cast<ValueType>(value.value());
|
||||||
|
|
||||||
|
// e. If value is not undefined, then
|
||||||
|
// i. Set hasExplicitFormatComponents to true.
|
||||||
|
explicit_format_component = &property;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// c. Else,
|
// c. Else,
|
||||||
else {
|
else {
|
||||||
// i. Let value be ? GetOption(options, prop, "string", « the strings given in the Values column of the row », undefined).
|
// i. Let values be a List whose elements are the strings given in the Values column of the row.
|
||||||
auto value = TRY(get_option(global_object, *options, property, Value::Type::String, defaults, Empty {}));
|
// ii. Let value be ? GetOption(options, prop, "string", values, undefined).
|
||||||
|
auto value = TRY(get_option(global_object, *options, property, Value::Type::String, values, Empty {}));
|
||||||
|
|
||||||
// d. Set formatOptions.[[<prop>]] to value.
|
// d. Set formatOptions.[[<prop>]] to value.
|
||||||
if (!value.is_undefined())
|
if (!value.is_undefined()) {
|
||||||
option = Unicode::calendar_pattern_style_from_string(value.as_string().string());
|
option = Unicode::calendar_pattern_style_from_string(value.as_string().string());
|
||||||
|
|
||||||
|
// e. If value is not undefined, then
|
||||||
|
// i. Set hasExplicitFormatComponents to true.
|
||||||
|
explicit_format_component = &property;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// 37. Let matcher be ? GetOption(options, "formatMatcher", "string", « "basic", "best fit" », "best fit").
|
// 38. Let matcher be ? GetOption(options, "formatMatcher", "string", « "basic", "best fit" », "best fit").
|
||||||
matcher = TRY(get_option(global_object, *options, vm.names.formatMatcher, Value::Type::String, AK::Array { "basic"sv, "best fit"sv }, "best fit"sv));
|
matcher = TRY(get_option(global_object, *options, vm.names.formatMatcher, Value::Type::String, AK::Array { "basic"sv, "best fit"sv }, "best fit"sv));
|
||||||
|
|
||||||
// 38. Let dateStyle be ? GetOption(options, "dateStyle", "string", « "full", "long", "medium", "short" », undefined).
|
// 39. Let dateStyle be ? GetOption(options, "dateStyle", "string", « "full", "long", "medium", "short" », undefined).
|
||||||
auto date_style = TRY(get_option(global_object, *options, vm.names.dateStyle, Value::Type::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
|
auto date_style = TRY(get_option(global_object, *options, vm.names.dateStyle, Value::Type::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
|
||||||
|
|
||||||
// 39. Set dateTimeFormat.[[DateStyle]] to dateStyle.
|
// 40. Set dateTimeFormat.[[DateStyle]] to dateStyle.
|
||||||
if (!date_style.is_undefined())
|
if (!date_style.is_undefined())
|
||||||
date_time_format.set_date_style(date_style.as_string().string());
|
date_time_format.set_date_style(date_style.as_string().string());
|
||||||
|
|
||||||
// 40. Let timeStyle be ? GetOption(options, "timeStyle", "string", « "full", "long", "medium", "short" », undefined).
|
// 41. Let timeStyle be ? GetOption(options, "timeStyle", "string", « "full", "long", "medium", "short" », undefined).
|
||||||
auto time_style = TRY(get_option(global_object, *options, vm.names.timeStyle, Value::Type::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
|
auto time_style = TRY(get_option(global_object, *options, vm.names.timeStyle, Value::Type::String, AK::Array { "full"sv, "long"sv, "medium"sv, "short"sv }, Empty {}));
|
||||||
|
|
||||||
// 41. Set dateTimeFormat.[[TimeStyle]] to timeStyle.
|
// 42. Set dateTimeFormat.[[TimeStyle]] to timeStyle.
|
||||||
if (!time_style.is_undefined())
|
if (!time_style.is_undefined())
|
||||||
date_time_format.set_time_style(time_style.as_string().string());
|
date_time_format.set_time_style(time_style.as_string().string());
|
||||||
|
|
||||||
Optional<Unicode::CalendarPattern> best_format {};
|
Optional<Unicode::CalendarPattern> best_format {};
|
||||||
|
|
||||||
// 42. If dateStyle is not undefined or timeStyle is not undefined, then
|
// 43. If dateStyle is not undefined or timeStyle is not undefined, then
|
||||||
if (date_time_format.has_date_style() || date_time_format.has_time_style()) {
|
if (date_time_format.has_date_style() || date_time_format.has_time_style()) {
|
||||||
// a. For each row in Table 6, except the header row, do
|
// a. If hasExplicitFormatComponents is true, then
|
||||||
TRY(for_each_calendar_field(global_object, format_options, [&](auto const& option, auto const& property, auto const&) -> ThrowCompletionOr<void> {
|
if (explicit_format_component != nullptr) {
|
||||||
// i. Let prop be the name given in the Property column of the row.
|
// i. Throw a TypeError exception.
|
||||||
// ii. Let p be formatOptions.[[<prop>]].
|
return vm.throw_completion<TypeError>(global_object, ErrorType::IntlInvalidDateTimeFormatOption, *explicit_format_component, "dateStyle or timeStyle"sv);
|
||||||
// iii. If p is not undefined, then
|
}
|
||||||
if (option.has_value()) {
|
|
||||||
// 1. Throw a TypeError exception.
|
|
||||||
return vm.throw_completion<TypeError>(global_object, ErrorType::IntlInvalidDateTimeFormatOption, property, "dateStyle or timeStyle"sv);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}));
|
|
||||||
|
|
||||||
// b. Let styles be dataLocaleData.[[styles]].[[<calendar>]].
|
// b. Let styles be dataLocaleData.[[styles]].[[<calendar>]].
|
||||||
// c. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles).
|
// c. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles).
|
||||||
best_format = date_time_style_format(data_locale, date_time_format);
|
best_format = date_time_style_format(data_locale, date_time_format);
|
||||||
}
|
}
|
||||||
// 43. Else,
|
// 44. Else,
|
||||||
else {
|
else {
|
||||||
// a. Let formats be dataLocaleData.[[formats]].[[<calendar>]].
|
// a. Let formats be dataLocaleData.[[formats]].[[<calendar>]].
|
||||||
auto formats = Unicode::get_calendar_available_formats(data_locale, date_time_format.calendar());
|
auto formats = Unicode::get_calendar_available_formats(data_locale, date_time_format.calendar());
|
||||||
|
@ -338,7 +346,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 44. For each row in Table 6, except the header row, in table order, do
|
// 45. For each row in Table 6, except the header row, in table order, do
|
||||||
date_time_format.for_each_calendar_field_zipped_with(*best_format, [&](auto& date_time_format_field, auto const& best_format_field, auto) {
|
date_time_format.for_each_calendar_field_zipped_with(*best_format, [&](auto& date_time_format_field, auto const& best_format_field, auto) {
|
||||||
// a. Let prop be the name given in the Property column of the row.
|
// a. Let prop be the name given in the Property column of the row.
|
||||||
// b. If bestFormat has a field [[<prop>]], then
|
// b. If bestFormat has a field [[<prop>]], then
|
||||||
|
@ -352,13 +360,13 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
String pattern;
|
String pattern;
|
||||||
Vector<Unicode::CalendarRangePattern> range_patterns;
|
Vector<Unicode::CalendarRangePattern> range_patterns;
|
||||||
|
|
||||||
// 45. If dateTimeFormat.[[Hour]] is undefined, then
|
// 46. If dateTimeFormat.[[Hour]] is undefined, then
|
||||||
if (!date_time_format.has_hour()) {
|
if (!date_time_format.has_hour()) {
|
||||||
// a. Set dateTimeFormat.[[HourCycle]] to undefined.
|
// a. Set dateTimeFormat.[[HourCycle]] to undefined.
|
||||||
date_time_format.clear_hour_cycle();
|
date_time_format.clear_hour_cycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 46. If dateTimeformat.[[HourCycle]] is "h11" or "h12", then
|
// 47. If dateTimeformat.[[HourCycle]] is "h11" or "h12", then
|
||||||
if ((hour_cycle_value == Unicode::HourCycle::H11) || (hour_cycle_value == Unicode::HourCycle::H12)) {
|
if ((hour_cycle_value == Unicode::HourCycle::H11) || (hour_cycle_value == Unicode::HourCycle::H12)) {
|
||||||
// a. Let pattern be bestFormat.[[pattern12]].
|
// a. Let pattern be bestFormat.[[pattern12]].
|
||||||
if (best_format->pattern12.has_value()) {
|
if (best_format->pattern12.has_value()) {
|
||||||
|
@ -372,7 +380,7 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
// b. Let rangePatterns be bestFormat.[[rangePatterns12]].
|
// b. Let rangePatterns be bestFormat.[[rangePatterns12]].
|
||||||
range_patterns = Unicode::get_calendar_range12_formats(data_locale, date_time_format.calendar(), best_format->skeleton);
|
range_patterns = Unicode::get_calendar_range12_formats(data_locale, date_time_format.calendar(), best_format->skeleton);
|
||||||
}
|
}
|
||||||
// 47. Else,
|
// 48. Else,
|
||||||
else {
|
else {
|
||||||
// a. Let pattern be bestFormat.[[pattern]].
|
// a. Let pattern be bestFormat.[[pattern]].
|
||||||
pattern = move(best_format->pattern);
|
pattern = move(best_format->pattern);
|
||||||
|
@ -381,13 +389,13 @@ ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& glo
|
||||||
range_patterns = Unicode::get_calendar_range_formats(data_locale, date_time_format.calendar(), best_format->skeleton);
|
range_patterns = Unicode::get_calendar_range_formats(data_locale, date_time_format.calendar(), best_format->skeleton);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 48. Set dateTimeFormat.[[Pattern]] to pattern.
|
// 49. Set dateTimeFormat.[[Pattern]] to pattern.
|
||||||
date_time_format.set_pattern(move(pattern));
|
date_time_format.set_pattern(move(pattern));
|
||||||
|
|
||||||
// 49. Set dateTimeFormat.[[RangePatterns]] to rangePatterns.
|
// 50. Set dateTimeFormat.[[RangePatterns]] to rangePatterns.
|
||||||
date_time_format.set_range_patterns(move(range_patterns));
|
date_time_format.set_range_patterns(move(range_patterns));
|
||||||
|
|
||||||
// 50. Return dateTimeFormat.
|
// 51. Return dateTimeFormat.
|
||||||
return &date_time_format;
|
return &date_time_format;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue