RelativeTimeFormat.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  9. #include <LibJS/Runtime/Intl/NumberFormat.h>
  10. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  11. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  12. namespace JS::Intl {
  13. // 17 RelativeTimeFormat Objects, https://tc39.es/ecma402/#relativetimeformat-objects
  14. RelativeTimeFormat::RelativeTimeFormat(Object& prototype)
  15. : Object(prototype)
  16. {
  17. }
  18. void RelativeTimeFormat::visit_edges(Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. if (m_number_format)
  22. visitor.visit(m_number_format);
  23. }
  24. void RelativeTimeFormat::set_numeric(StringView numeric)
  25. {
  26. if (numeric == "always"sv) {
  27. m_numeric = Numeric::Always;
  28. } else if (numeric == "auto"sv) {
  29. m_numeric = Numeric::Auto;
  30. } else {
  31. VERIFY_NOT_REACHED();
  32. }
  33. }
  34. StringView RelativeTimeFormat::numeric_string() const
  35. {
  36. switch (m_numeric) {
  37. case Numeric::Always:
  38. return "always"sv;
  39. case Numeric::Auto:
  40. return "auto"sv;
  41. default:
  42. VERIFY_NOT_REACHED();
  43. }
  44. }
  45. // 17.1.1 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options ), https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat
  46. ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObject& global_object, RelativeTimeFormat& relative_time_format, Value locales_value, Value options_value)
  47. {
  48. auto& vm = global_object.vm();
  49. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  50. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
  51. // 2. Set options to ? CoerceOptionsToObject(options).
  52. auto* options = TRY(coerce_options_to_object(global_object, options_value));
  53. // 3. Let opt be a new Record.
  54. LocaleOptions opt {};
  55. // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  56. auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
  57. // 5. Set opt.[[LocaleMatcher]] to matcher.
  58. opt.locale_matcher = matcher;
  59. // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
  60. auto numbering_system = TRY(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {}));
  61. // 7. If numberingSystem is not undefined, then
  62. if (!numbering_system.is_undefined()) {
  63. // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  64. if (!Unicode::is_type_identifier(numbering_system.as_string().string()))
  65. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
  66. // 8. Set opt.[[nu]] to numberingSystem.
  67. opt.nu = numbering_system.as_string().string();
  68. }
  69. // 9. Let localeData be %RelativeTimeFormat%.[[LocaleData]].
  70. // 10. Let r be ResolveLocale(%RelativeTimeFormat%.[[AvailableLocales]], requestedLocales, opt, %RelativeTimeFormat%.[[RelevantExtensionKeys]], localeData).
  71. auto result = resolve_locale(requested_locales, opt, RelativeTimeFormat::relevant_extension_keys());
  72. // 11. Let locale be r.[[locale]].
  73. auto locale = move(result.locale);
  74. // 12. Set relativeTimeFormat.[[Locale]] to locale.
  75. relative_time_format.set_locale(locale);
  76. // 13. Set relativeTimeFormat.[[DataLocale]] to r.[[dataLocale]].
  77. relative_time_format.set_data_locale(move(result.data_locale));
  78. // 14. Set relativeTimeFormat.[[NumberingSystem]] to r.[[nu]].
  79. if (result.nu.has_value())
  80. relative_time_format.set_numbering_system(result.nu.release_value());
  81. // 15. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow" », "long").
  82. auto style = TRY(get_option(global_object, *options, vm.names.style, Value::Type::String, { "long"sv, "short"sv, "narrow"sv }, "long"sv));
  83. // 16. Set relativeTimeFormat.[[Style]] to style.
  84. relative_time_format.set_style(style.as_string().string());
  85. // 17. Let numeric be ? GetOption(options, "numeric", "string", « "always", "auto" », "always").
  86. auto numeric = TRY(get_option(global_object, *options, vm.names.numeric, Value::Type::String, { "always"sv, "auto"sv }, "always"sv));
  87. // 18. Set relativeTimeFormat.[[Numeric]] to numeric.
  88. relative_time_format.set_numeric(numeric.as_string().string());
  89. // 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
  90. auto* number_format = MUST(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, locale)));
  91. relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format));
  92. // 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »).
  93. // FIXME: We do not yet support Intl.PluralRules.
  94. // 21. Return relativeTimeFormat.
  95. return &relative_time_format;
  96. }
  97. }