PluralRules.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/PluralRules.h>
  9. namespace JS::Intl {
  10. // 16 PluralRules Objects, https://tc39.es/ecma402/#pluralrules-objects
  11. PluralRules::PluralRules(Object& prototype)
  12. : NumberFormatBase(prototype)
  13. {
  14. }
  15. void PluralRules::set_type(StringView type)
  16. {
  17. if (type == "cardinal"sv) {
  18. m_type = Type::Cardinal;
  19. } else if (type == "ordinal"sv) {
  20. m_type = Type::Ordinal;
  21. } else {
  22. VERIFY_NOT_REACHED();
  23. }
  24. }
  25. StringView PluralRules::type_string() const
  26. {
  27. switch (m_type) {
  28. case Type::Cardinal:
  29. return "cardinal"sv;
  30. case Type::Ordinal:
  31. return "ordinal"sv;
  32. default:
  33. VERIFY_NOT_REACHED();
  34. }
  35. }
  36. // 16.1.1 InitializePluralRules ( pluralRules, locales, options ), https://tc39.es/ecma402/#sec-initializepluralrules
  37. ThrowCompletionOr<PluralRules*> initialize_plural_rules(GlobalObject& global_object, PluralRules& plural_rules, Value locales_value, Value options_value)
  38. {
  39. auto& vm = global_object.vm();
  40. // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  41. auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
  42. // 2. Set options to ? CoerceOptionsToObject(options).
  43. auto* options = TRY(coerce_options_to_object(global_object, options_value));
  44. // 3. Let opt be a new Record.
  45. LocaleOptions opt {};
  46. // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
  47. auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
  48. // 5. Set opt.[[localeMatcher]] to matcher.
  49. opt.locale_matcher = matcher;
  50. // 6. Let t be ? GetOption(options, "type", "string", « "cardinal", "ordinal" », "cardinal").
  51. auto type = TRY(get_option(global_object, *options, vm.names.type, Value::Type::String, AK::Array { "cardinal"sv, "ordinal"sv }, "cardinal"sv));
  52. // 7. Set pluralRules.[[Type]] to t.
  53. plural_rules.set_type(type.as_string().string());
  54. // 8. Perform ? SetNumberFormatDigitOptions(pluralRules, options, +0𝔽, 3𝔽, "standard").
  55. TRY(set_number_format_digit_options(global_object, plural_rules, *options, 0, 3, NumberFormat::Notation::Standard));
  56. // 9. Let localeData be %PluralRules%.[[LocaleData]].
  57. // 10. Let r be ResolveLocale(%PluralRules%.[[AvailableLocales]], requestedLocales, opt, %PluralRules%.[[RelevantExtensionKeys]], localeData).
  58. auto result = resolve_locale(requested_locales, opt, {});
  59. // 11. Set pluralRules.[[Locale]] to r.[[locale]].
  60. plural_rules.set_locale(move(result.locale));
  61. // Non-standard, the data locale is used by our NumberFormat implementation.
  62. plural_rules.set_data_locale(move(result.data_locale));
  63. // 12. Return pluralRules.
  64. return &plural_rules;
  65. }
  66. }