RelativeTimeFormatConstructor.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  10. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  11. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  12. #include <LibUnicode/Locale.h>
  13. namespace JS::Intl {
  14. JS_DEFINE_ALLOCATOR(RelativeTimeFormatConstructor);
  15. // 17.1 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor
  16. RelativeTimeFormatConstructor::RelativeTimeFormatConstructor(Realm& realm)
  17. : NativeFunction(realm.vm().names.RelativeTimeFormat.as_string(), realm.intrinsics().function_prototype())
  18. {
  19. }
  20. void RelativeTimeFormatConstructor::initialize(Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. auto& vm = this->vm();
  24. // 17.2.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype
  25. define_direct_property(vm.names.prototype, realm.intrinsics().intl_relative_time_format_prototype(), 0);
  26. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  27. u8 attr = Attribute::Writable | Attribute::Configurable;
  28. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  29. }
  30. // 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  31. ThrowCompletionOr<Value> RelativeTimeFormatConstructor::call()
  32. {
  33. // 1. If NewTarget is undefined, throw a TypeError exception.
  34. return vm().throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Intl.RelativeTimeFormat");
  35. }
  36. // 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  37. ThrowCompletionOr<NonnullGCPtr<Object>> RelativeTimeFormatConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto locales_value = vm.argument(0);
  41. auto options_value = vm.argument(1);
  42. // 2. Let relativeTimeFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%Intl.RelativeTimeFormat.prototype%", « [[InitializedRelativeTimeFormat]], [[Locale]], [[LocaleData]], [[Style]], [[Numeric]], [[NumberFormat]], [[NumberingSystem]], [[PluralRules]] »).
  43. auto relative_time_format = TRY(ordinary_create_from_constructor<RelativeTimeFormat>(vm, new_target, &Intrinsics::intl_relative_time_format_prototype));
  44. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  45. auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
  46. // 4. Set options to ? CoerceOptionsToObject(options).
  47. auto* options = TRY(coerce_options_to_object(vm, options_value));
  48. // 5. Let opt be a new Record.
  49. LocaleOptions opt {};
  50. // 6. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  51. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
  52. // 7. Set opt.[[LocaleMatcher]] to matcher.
  53. opt.locale_matcher = matcher;
  54. // 8. Let numberingSystem be ? GetOption(options, "numberingSystem", string, empty, undefined).
  55. auto numbering_system = TRY(get_option(vm, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {}));
  56. // 9. If numberingSystem is not undefined, then
  57. if (!numbering_system.is_undefined()) {
  58. // a. If numberingSystem cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception.
  59. if (!Unicode::is_type_identifier(numbering_system.as_string().utf8_string_view()))
  60. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
  61. }
  62. // 10. Set opt.[[nu]] to numberingSystem.
  63. opt.nu = locale_key_from_value(numbering_system);
  64. // 11. Let r be ResolveLocale(%Intl.RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %Intl.RelativeTimeFormat%.[[RelevantExtensionKeys]], %Intl.RelativeTimeFormat%.[[LocaleData]]).
  65. auto result = resolve_locale(requested_locales, opt, RelativeTimeFormat::relevant_extension_keys());
  66. // 12. Let locale be r.[[Locale]].
  67. auto locale = move(result.locale);
  68. // 13. Set relativeTimeFormat.[[Locale]] to locale.
  69. relative_time_format->set_locale(locale);
  70. // 14. Set relativeTimeFormat.[[LocaleData]] to r.[[LocaleData]].
  71. // 15. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]].
  72. if (auto* resolved_numbering_system = result.nu.get_pointer<String>())
  73. relative_time_format->set_numbering_system(move(*resolved_numbering_system));
  74. // 16. Let style be ? GetOption(options, "style", string, « "long", "short", "narrow" », "long").
  75. auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
  76. // 17. Set relativeTimeFormat.[[Style]] to style.
  77. relative_time_format->set_style(style.as_string().utf8_string_view());
  78. // 18. Let numeric be ? GetOption(options, "numeric", string, « "always", "auto" », "always").
  79. auto numeric = TRY(get_option(vm, *options, vm.names.numeric, OptionType::String, { "always"sv, "auto"sv }, "always"sv));
  80. // 19. Set relativeTimeFormat.[[Numeric]] to numeric.
  81. relative_time_format->set_numeric(numeric.as_string().utf8_string_view());
  82. // 20. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%Intl.NumberFormat%, « locale »).
  83. // 21. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%Intl.PluralRules%, « locale »).
  84. auto formatter = Unicode::RelativeTimeFormat::create(
  85. relative_time_format->locale(),
  86. relative_time_format->style());
  87. relative_time_format->set_formatter(move(formatter));
  88. // 22. Return relativeTimeFormat.
  89. return relative_time_format;
  90. }
  91. // 17.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf
  92. JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of)
  93. {
  94. auto locales = vm.argument(0);
  95. auto options = vm.argument(1);
  96. // 1. Let availableLocales be %RelativeTimeFormat%.[[AvailableLocales]].
  97. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  98. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  99. // 3. Return ? FilterLocales(availableLocales, requestedLocales, options).
  100. return TRY(filter_locales(vm, requested_locales, options));
  101. }
  102. }