PluralRules.cpp 3.0 KB

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