DisplayNames.cpp 7.2 KB

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