DisplayNames.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021, 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/DisplayNames.h>
  8. namespace JS::Intl {
  9. // 12 DisplayNames Objects, https://tc39.es/ecma402/#intl-displaynames-objects
  10. DisplayNames::DisplayNames(Object& prototype)
  11. : Object(prototype)
  12. {
  13. }
  14. void DisplayNames::set_style(StringView style)
  15. {
  16. if (style == "narrow"sv) {
  17. m_style = Style::Narrow;
  18. } else if (style == "short"sv) {
  19. m_style = Style::Short;
  20. } else if (style == "long"sv) {
  21. m_style = Style::Long;
  22. } else {
  23. VERIFY_NOT_REACHED();
  24. }
  25. }
  26. StringView DisplayNames::style_string() const
  27. {
  28. switch (m_style) {
  29. case Style::Narrow:
  30. return "narrow"sv;
  31. case Style::Short:
  32. return "short"sv;
  33. case Style::Long:
  34. return "long"sv;
  35. default:
  36. VERIFY_NOT_REACHED();
  37. }
  38. }
  39. void DisplayNames::set_type(StringView type)
  40. {
  41. if (type == "language"sv) {
  42. m_type = Type::Language;
  43. } else if (type == "region"sv) {
  44. m_type = Type::Region;
  45. } else if (type == "script"sv) {
  46. m_type = Type::Script;
  47. } else if (type == "currency"sv) {
  48. m_type = Type::Currency;
  49. } else {
  50. VERIFY_NOT_REACHED();
  51. }
  52. }
  53. StringView DisplayNames::type_string() const
  54. {
  55. switch (m_type) {
  56. case Type::Language:
  57. return "language"sv;
  58. case Type::Region:
  59. return "region"sv;
  60. case Type::Script:
  61. return "script"sv;
  62. case Type::Currency:
  63. return "currency"sv;
  64. default:
  65. VERIFY_NOT_REACHED();
  66. }
  67. }
  68. void DisplayNames::set_fallback(StringView fallback)
  69. {
  70. if (fallback == "none"sv) {
  71. m_fallback = Fallback::None;
  72. } else if (fallback == "code"sv) {
  73. m_fallback = Fallback::Code;
  74. } else {
  75. VERIFY_NOT_REACHED();
  76. }
  77. }
  78. StringView DisplayNames::fallback_string() const
  79. {
  80. switch (m_fallback) {
  81. case Fallback::None:
  82. return "none"sv;
  83. case Fallback::Code:
  84. return "code"sv;
  85. default:
  86. VERIFY_NOT_REACHED();
  87. }
  88. }
  89. }