DisplayNames.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2021-2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  8. #include <LibJS/Runtime/Intl/DisplayNames.h>
  9. namespace JS::Intl {
  10. // 12 DisplayNames Objects, https://tc39.es/ecma402/#intl-displaynames-objects
  11. DisplayNames::DisplayNames(Object& prototype)
  12. : Object(prototype)
  13. {
  14. }
  15. void DisplayNames::set_type(StringView type)
  16. {
  17. if (type == "language"sv)
  18. m_type = Type::Language;
  19. else if (type == "region"sv)
  20. m_type = Type::Region;
  21. else if (type == "script"sv)
  22. m_type = Type::Script;
  23. else if (type == "currency"sv)
  24. m_type = Type::Currency;
  25. else if (type == "calendar"sv)
  26. m_type = Type::Calendar;
  27. else if (type == "dateTimeField"sv)
  28. m_type = Type::DateTimeField;
  29. else
  30. VERIFY_NOT_REACHED();
  31. }
  32. StringView DisplayNames::type_string() const
  33. {
  34. switch (m_type) {
  35. case Type::Language:
  36. return "language"sv;
  37. case Type::Region:
  38. return "region"sv;
  39. case Type::Script:
  40. return "script"sv;
  41. case Type::Currency:
  42. return "currency"sv;
  43. case Type::Calendar:
  44. return "calendar"sv;
  45. case Type::DateTimeField:
  46. return "dateTimeField"sv;
  47. default:
  48. VERIFY_NOT_REACHED();
  49. }
  50. }
  51. void DisplayNames::set_fallback(StringView fallback)
  52. {
  53. if (fallback == "none"sv)
  54. m_fallback = Fallback::None;
  55. else if (fallback == "code"sv)
  56. m_fallback = Fallback::Code;
  57. else
  58. VERIFY_NOT_REACHED();
  59. }
  60. StringView DisplayNames::fallback_string() const
  61. {
  62. switch (m_fallback) {
  63. case Fallback::None:
  64. return "none"sv;
  65. case Fallback::Code:
  66. return "code"sv;
  67. default:
  68. VERIFY_NOT_REACHED();
  69. }
  70. }
  71. void DisplayNames::set_language_display(StringView language_display)
  72. {
  73. if (language_display == "dialect"sv)
  74. m_language_display = LanguageDisplay::Dialect;
  75. else if (language_display == "standard"sv)
  76. m_language_display = LanguageDisplay::Standard;
  77. else
  78. VERIFY_NOT_REACHED();
  79. }
  80. StringView DisplayNames::language_display_string() const
  81. {
  82. VERIFY(m_language_display.has_value());
  83. switch (*m_language_display) {
  84. case LanguageDisplay::Dialect:
  85. return "dialect"sv;
  86. case LanguageDisplay::Standard:
  87. return "standard"sv;
  88. default:
  89. VERIFY_NOT_REACHED();
  90. }
  91. }
  92. // 12.1.1 CanonicalCodeForDisplayNames ( type, code ), https://tc39.es/ecma402/#sec-canonicalcodefordisplaynames
  93. ThrowCompletionOr<Value> canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code)
  94. {
  95. auto& vm = global_object.vm();
  96. // 1. If type is "language", then
  97. if (type == DisplayNames::Type::Language) {
  98. // a. If code does not match the unicode_language_id production, throw a RangeError exception.
  99. if (!Unicode::parse_unicode_language_id(code).has_value())
  100. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "language"sv);
  101. // b. If IsStructurallyValidLanguageTag(code) is false, throw a RangeError exception.
  102. auto locale_id = is_structurally_valid_language_tag(code);
  103. if (!locale_id.has_value())
  104. return vm.throw_completion<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, code);
  105. // c. Set code to CanonicalizeUnicodeLocaleId(code).
  106. // d. Return code.
  107. auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id);
  108. return js_string(vm, move(canonicalized_tag));
  109. }
  110. // 2. If type is "region", then
  111. if (type == DisplayNames::Type::Region) {
  112. // a. If code does not match the unicode_region_subtag production, throw a RangeError exception.
  113. if (!Unicode::is_unicode_region_subtag(code))
  114. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "region"sv);
  115. // b. Let code be the result of mapping code to upper case as described in 6.1.
  116. // c. Return code.
  117. return js_string(vm, code.to_uppercase_string());
  118. }
  119. // 3. If type is "script", then
  120. if (type == DisplayNames::Type::Script) {
  121. // a. If code does not match the unicode_script_subtag production, throw a RangeError exception.
  122. if (!Unicode::is_unicode_script_subtag(code))
  123. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "script"sv);
  124. // b. Let code be the result of mapping the first character in code to upper case, and mapping the second, third, and fourth character in code to lower case, as described in 6.1.
  125. // c. Return code.
  126. return js_string(vm, code.to_titlecase_string());
  127. }
  128. // 4. If type is "calendar", then
  129. if (type == DisplayNames::Type::Calendar) {
  130. // a. If code does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  131. if (!Unicode::is_type_identifier(code))
  132. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "calendar"sv);
  133. // b. Let code be the result of mapping code to lower case as described in 6.1.
  134. // c. Return code.
  135. return js_string(vm, code.to_lowercase_string());
  136. }
  137. // 5. If type is "dateTimeField", then
  138. if (type == DisplayNames::Type::DateTimeField) {
  139. // a. If the result of IsValidDateTimeFieldCode(code) is false, throw a RangeError exception.
  140. if (!is_valid_date_time_field_code(code))
  141. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "dateTimeField"sv);
  142. // b. Return code.
  143. return js_string(vm, code);
  144. }
  145. // 6. Assert: type is "currency".
  146. VERIFY(type == DisplayNames::Type::Currency);
  147. // 7. If ! IsWellFormedCurrencyCode(code) is false, throw a RangeError exception.
  148. if (!is_well_formed_currency_code(code))
  149. return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "currency"sv);
  150. // 8. Let code be the result of mapping code to upper case as described in 6.1.
  151. // 9. Return code.
  152. return js_string(vm, code.to_uppercase_string());
  153. }
  154. // 12.2 IsValidDateTimeFieldCode ( field ), https://tc39.es/ecma402/#sec-isvaliddatetimefieldcode
  155. bool is_valid_date_time_field_code(StringView field)
  156. {
  157. // 1. If field is listed in the Code column of Table 8, return true.
  158. // 2. Return false.
  159. return field.is_one_of("era"sv, "year"sv, "quarter"sv, "month"sv, "weekOfYear"sv, "weekday"sv, "day"sv, "dayPeriod"sv, "hour"sv, "minute"sv, "second"sv, "timeZoneName"sv);
  160. }
  161. }