RelativeTimeFormatConstructor.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2022, 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/NumberFormat.h>
  11. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  12. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  13. #include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
  14. #include <LibUnicode/Locale.h>
  15. namespace JS::Intl {
  16. // 17.1 The Intl.RelativeTimeFormat Constructor, https://tc39.es/ecma402/#sec-intl-relativetimeformat-constructor
  17. RelativeTimeFormatConstructor::RelativeTimeFormatConstructor(GlobalObject& global_object)
  18. : NativeFunction(vm().names.RelativeTimeFormat.as_string(), *global_object.function_prototype())
  19. {
  20. }
  21. void RelativeTimeFormatConstructor::initialize(GlobalObject& global_object)
  22. {
  23. NativeFunction::initialize(global_object);
  24. auto& vm = this->vm();
  25. // 17.2.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype
  26. define_direct_property(vm.names.prototype, global_object.intl_relative_time_format_prototype(), 0);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. u8 attr = Attribute::Writable | Attribute::Configurable;
  29. define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  30. }
  31. // 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  32. ThrowCompletionOr<Value> RelativeTimeFormatConstructor::call()
  33. {
  34. // 1. If NewTarget is undefined, throw a TypeError exception.
  35. return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.RelativeTimeFormat");
  36. }
  37. // 17.1.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat
  38. ThrowCompletionOr<Object*> RelativeTimeFormatConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. auto& global_object = this->global_object();
  42. auto locales = vm.argument(0);
  43. auto options = vm.argument(1);
  44. // 2. Let relativeTimeFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%RelativeTimeFormat.prototype%", « [[InitializedRelativeTimeFormat]], [[Locale]], [[DataLocale]], [[Style]], [[Numeric]], [[NumberFormat]], [[NumberingSystem]], [[PluralRules]] »).
  45. auto* relative_time_format = TRY(ordinary_create_from_constructor<RelativeTimeFormat>(global_object, new_target, &GlobalObject::intl_relative_time_format_prototype));
  46. // 3. Return ? InitializeRelativeTimeFormat(relativeTimeFormat, locales, options).
  47. return TRY(initialize_relative_time_format(global_object, *relative_time_format, locales, options));
  48. }
  49. // 17.2.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf
  50. JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of)
  51. {
  52. auto locales = vm.argument(0);
  53. auto options = vm.argument(1);
  54. // 1. Let availableLocales be %RelativeTimeFormat%.[[AvailableLocales]].
  55. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  56. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
  57. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  58. return TRY(supported_locales(global_object, requested_locales, options));
  59. }
  60. // 17.1.2 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat
  61. ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value)
  62. {
  63. auto& vm = global_object.vm();
  64. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  65. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
  66. // 2. Set options to ? CoerceOptionsToObject(options).
  67. auto* options = TRY(coerce_options_to_object(global_object, options_value));
  68. // 3. Let opt be a new Record.
  69. LocaleOptions opt {};
  70. // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  71. auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
  72. // 5. Set opt.[[LocaleMatcher]] to matcher.
  73. opt.locale_matcher = matcher;
  74. // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
  75. auto numbering_system = TRY(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {}));
  76. // 7. If numberingSystem is not undefined, then
  77. if (!numbering_system.is_undefined()) {
  78. // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  79. if (!Unicode::is_type_identifier(numbering_system.as_string().string()))
  80. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
  81. // 8. Set opt.[[nu]] to numberingSystem.
  82. opt.nu = numbering_system.as_string().string();
  83. }
  84. // 9. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
  85. // 10. Let r be ResolveLocale(%RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %RelativeTimeFormat%.[[RelevantExtensionKeys]], localeData).
  86. auto result = resolve_locale(requested_locales, opt, RelativeTimeFormat::relevant_extension_keys());
  87. // 11. Let locale be r.[[locale]].
  88. auto locale = move(result.locale);
  89. // 12. Set relativeTimeFormat.[[Locale]] to locale.
  90. relative_time_format.set_locale(locale);
  91. // 13. Set relativeTimeFormat.[[DataLocale]] to r.[[dataLocale]].
  92. relative_time_format.set_data_locale(move(result.data_locale));
  93. // 14. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]].
  94. if (result.nu.has_value())
  95. relative_time_format.set_numbering_system(result.nu.release_value());
  96. // 15. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long").
  97. auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
  98. // 16. Set relativeTimeFormat.[[Style]] to style.
  99. relative_time_format.set_style(style.as_string().string());
  100. // 17. Let numeric be ? GetOption(options, "numeric", "string", « "always", "auto" », "always").
  101. auto numeric = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::String, { "always"sv, "auto"sv }, "always"sv));
  102. // 18. Set relativeTimeFormat.[[Numeric]] to numeric.
  103. relative_time_format.set_numeric(numeric.as_string().string());
  104. // 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
  105. auto* number_format = MUST(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, locale)));
  106. relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format));
  107. // 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »).
  108. // FIXME: We do not yet support Intl.PluralRules.
  109. // 21. Return relativeTimeFormat.
  110. return &relative_time_format;
  111. }
  112. }