1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:28:12 +00:00

LibJS: Reorganize spec steps for Intl.RelativeTimeFormat

This is an editorial change in the Intl spec:
a3ae343
This commit is contained in:
Timothy Flynn 2022-03-15 11:09:11 -04:00 committed by Andreas Kling
parent 8941d0b49e
commit 04bb17ca94
5 changed files with 94 additions and 90 deletions

View file

@ -51,80 +51,7 @@ StringView RelativeTimeFormat::numeric_string() const
}
}
// 17.1.1 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat
ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value)
{
auto& vm = global_object.vm();
// 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
// 2. Set options to ? CoerceOptionsToObject(options).
auto* options = TRY(coerce_options_to_object(global_object, options_value));
// 3. Let opt be a new Record.
LocaleOptions opt {};
// 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
// 5. Set opt.[[LocaleMatcher]] to matcher.
opt.locale_matcher = matcher;
// 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
auto numbering_system = TRY(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {}));
// 7. If numberingSystem is not undefined, then
if (!numbering_system.is_undefined()) {
// a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
if (!Unicode::is_type_identifier(numbering_system.as_string().string()))
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
// 8. Set opt.[[nu]] to numberingSystem.
opt.nu = numbering_system.as_string().string();
}
// 9. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
// 10. Let r be ResolveLocale(%RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %RelativeTimeFormat%.[[RelevantExtensionKeys]], localeData).
auto result = resolve_locale(requested_locales, opt, RelativeTimeFormat::relevant_extension_keys());
// 11. Let locale be r.[[locale]].
auto locale = move(result.locale);
// 12. Set relativeTimeFormat.[[Locale]] to locale.
relative_time_format.set_locale(locale);
// 13. Set relativeTimeFormat.[[DataLocale]] to r.[[dataLocale]].
relative_time_format.set_data_locale(move(result.data_locale));
// 14. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]].
if (result.nu.has_value())
relative_time_format.set_numbering_system(result.nu.release_value());
// 15. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long").
auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
// 16. Set relativeTimeFormat.[[Style]] to style.
relative_time_format.set_style(style.as_string().string());
// 17. Let numeric be ? GetOption(options, "numeric", "string", « "always", "auto" », "always").
auto numeric = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::String, { "always"sv, "auto"sv }, "always"sv));
// 18. Set relativeTimeFormat.[[Numeric]] to numeric.
relative_time_format.set_numeric(numeric.as_string().string());
// 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
auto* number_format = MUST(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, locale)));
relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format));
// 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »).
// FIXME: We do not yet support Intl.PluralRules.
// 21. Return relativeTimeFormat.
return &relative_time_format;
}
// 17.1.2 SingularRelativeTimeUnit ( unit ), https://tc39.es/ecma402/#sec-singularrelativetimeunit
// 17.5.1 SingularRelativeTimeUnit ( unit ), https://tc39.es/ecma402/#sec-singularrelativetimeunit
ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(GlobalObject& global_object, StringView unit)
{
auto& vm = global_object.vm();
@ -163,7 +90,7 @@ ThrowCompletionOr<Unicode::TimeUnit> singular_relative_time_unit(GlobalObject& g
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidUnit, unit);
}
// 17.1.3 PartitionRelativeTimePattern ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern
// 17.5.2 PartitionRelativeTimePattern ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern
ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_pattern(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{
auto& vm = global_object.vm();
@ -262,7 +189,7 @@ ThrowCompletionOr<Vector<PatternPartitionWithUnit>> partition_relative_time_patt
return make_parts_list(pattern->pattern, Unicode::time_unit_to_string(time_unit), move(value_partitions));
}
// 17.1.4 MakePartsList ( pattern, unit, parts ), https://tc39.es/ecma402/#sec-makepartslist
// 17.5.3 MakePartsList ( pattern, unit, parts ), https://tc39.es/ecma402/#sec-makepartslist
Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView unit, Vector<PatternPartition> parts)
{
// 1. Let patternParts be PartitionPattern(pattern).
@ -295,7 +222,7 @@ Vector<PatternPartitionWithUnit> make_parts_list(StringView pattern, StringView
return result;
}
// 17.1.5 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime
// 17.5.4 FormatRelativeTime ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTime
ThrowCompletionOr<String> format_relative_time(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{
// 1. Let parts be ? PartitionRelativeTimePattern(relativeTimeFormat, value, unit).
@ -314,7 +241,7 @@ ThrowCompletionOr<String> format_relative_time(GlobalObject& global_object, Rela
return result.build();
}
// 17.1.6 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts
// 17.5.5 FormatRelativeTimeToParts ( relativeTimeFormat, value, unit ), https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts
ThrowCompletionOr<Array*> format_relative_time_to_parts(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, double value, StringView unit)
{
auto& vm = global_object.vm();