NumberFormat.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Intl/NumberFormat.h>
  7. namespace JS::Intl {
  8. // 15 NumberFormat Objects, https://tc39.es/ecma402/#numberformat-objects
  9. NumberFormat::NumberFormat(Object& prototype)
  10. : Object(prototype)
  11. {
  12. }
  13. void NumberFormat::set_style(StringView style)
  14. {
  15. if (style == "decimal"sv)
  16. m_style = Style::Decimal;
  17. else if (style == "percent"sv)
  18. m_style = Style::Percent;
  19. else if (style == "currency"sv)
  20. m_style = Style::Currency;
  21. else if (style == "unit"sv)
  22. m_style = Style::Unit;
  23. else
  24. VERIFY_NOT_REACHED();
  25. }
  26. StringView NumberFormat::style_string() const
  27. {
  28. switch (m_style) {
  29. case Style::Decimal:
  30. return "decimal"sv;
  31. case Style::Percent:
  32. return "percent"sv;
  33. case Style::Currency:
  34. return "currency"sv;
  35. case Style::Unit:
  36. return "unit"sv;
  37. default:
  38. VERIFY_NOT_REACHED();
  39. }
  40. }
  41. void NumberFormat::set_currency_display(StringView currency_display)
  42. {
  43. if (currency_display == "code"sv)
  44. m_currency_display = CurrencyDisplay::Code;
  45. else if (currency_display == "symbol"sv)
  46. m_currency_display = CurrencyDisplay::Symbol;
  47. else if (currency_display == "narrowSymbol"sv)
  48. m_currency_display = CurrencyDisplay::NarrowSymbol;
  49. else if (currency_display == "name"sv)
  50. m_currency_display = CurrencyDisplay::Name;
  51. else
  52. VERIFY_NOT_REACHED();
  53. }
  54. StringView NumberFormat::currency_display_string() const
  55. {
  56. VERIFY(m_currency_display.has_value());
  57. switch (*m_currency_display) {
  58. case CurrencyDisplay::Code:
  59. return "code"sv;
  60. case CurrencyDisplay::Symbol:
  61. return "symbol"sv;
  62. case CurrencyDisplay::NarrowSymbol:
  63. return "narrowSymbol"sv;
  64. case CurrencyDisplay::Name:
  65. return "name"sv;
  66. default:
  67. VERIFY_NOT_REACHED();
  68. }
  69. }
  70. void NumberFormat::set_currency_sign(StringView currency_sign)
  71. {
  72. if (currency_sign == "standard"sv)
  73. m_currency_sign = CurrencySign::Standard;
  74. else if (currency_sign == "accounting"sv)
  75. m_currency_sign = CurrencySign::Accounting;
  76. else
  77. VERIFY_NOT_REACHED();
  78. }
  79. StringView NumberFormat::currency_sign_string() const
  80. {
  81. VERIFY(m_currency_sign.has_value());
  82. switch (*m_currency_sign) {
  83. case CurrencySign::Standard:
  84. return "standard"sv;
  85. case CurrencySign::Accounting:
  86. return "accounting"sv;
  87. default:
  88. VERIFY_NOT_REACHED();
  89. }
  90. }
  91. void NumberFormat::set_unit_display(StringView unit_display)
  92. {
  93. if (unit_display == "short"sv)
  94. m_unit_display = UnitDisplay::Short;
  95. else if (unit_display == "narrow"sv)
  96. m_unit_display = UnitDisplay::Narrow;
  97. else if (unit_display == "long"sv)
  98. m_unit_display = UnitDisplay::Long;
  99. else
  100. VERIFY_NOT_REACHED();
  101. }
  102. StringView NumberFormat::unit_display_string() const
  103. {
  104. VERIFY(m_unit_display.has_value());
  105. switch (*m_unit_display) {
  106. case UnitDisplay::Short:
  107. return "short"sv;
  108. case UnitDisplay::Narrow:
  109. return "narrow"sv;
  110. case UnitDisplay::Long:
  111. return "long"sv;
  112. default:
  113. VERIFY_NOT_REACHED();
  114. }
  115. }
  116. StringView NumberFormat::rounding_type_string() const
  117. {
  118. switch (m_rounding_type) {
  119. case RoundingType::SignificantDigits:
  120. return "significantDigits"sv;
  121. case RoundingType::FractionDigits:
  122. return "fractionDigits"sv;
  123. case RoundingType::CompactRounding:
  124. return "compactRounding"sv;
  125. default:
  126. VERIFY_NOT_REACHED();
  127. }
  128. }
  129. void NumberFormat::set_notation(StringView notation)
  130. {
  131. if (notation == "standard"sv)
  132. m_notation = Notation::Standard;
  133. else if (notation == "scientific"sv)
  134. m_notation = Notation::Scientific;
  135. else if (notation == "engineering"sv)
  136. m_notation = Notation::Engineering;
  137. else if (notation == "compact"sv)
  138. m_notation = Notation::Compact;
  139. else
  140. VERIFY_NOT_REACHED();
  141. }
  142. StringView NumberFormat::notation_string() const
  143. {
  144. switch (m_notation) {
  145. case Notation::Standard:
  146. return "standard"sv;
  147. case Notation::Scientific:
  148. return "scientific"sv;
  149. case Notation::Engineering:
  150. return "engineering"sv;
  151. case Notation::Compact:
  152. return "compact"sv;
  153. default:
  154. VERIFY_NOT_REACHED();
  155. }
  156. }
  157. void NumberFormat::set_compact_display(StringView compact_display)
  158. {
  159. if (compact_display == "short"sv)
  160. m_compact_display = CompactDisplay::Short;
  161. else if (compact_display == "long"sv)
  162. m_compact_display = CompactDisplay::Long;
  163. else
  164. VERIFY_NOT_REACHED();
  165. }
  166. StringView NumberFormat::compact_display_string() const
  167. {
  168. VERIFY(m_compact_display.has_value());
  169. switch (*m_compact_display) {
  170. case CompactDisplay::Short:
  171. return "short"sv;
  172. case CompactDisplay::Long:
  173. return "long"sv;
  174. default:
  175. VERIFY_NOT_REACHED();
  176. }
  177. }
  178. void NumberFormat::set_sign_display(StringView sign_display)
  179. {
  180. if (sign_display == "auto"sv)
  181. m_sign_display = SignDisplay::Auto;
  182. else if (sign_display == "never"sv)
  183. m_sign_display = SignDisplay::Never;
  184. else if (sign_display == "always"sv)
  185. m_sign_display = SignDisplay::Always;
  186. else if (sign_display == "exceptZero"sv)
  187. m_sign_display = SignDisplay::ExceptZero;
  188. else
  189. VERIFY_NOT_REACHED();
  190. }
  191. StringView NumberFormat::sign_display_string() const
  192. {
  193. switch (m_sign_display) {
  194. case SignDisplay::Auto:
  195. return "auto"sv;
  196. case SignDisplay::Never:
  197. return "never"sv;
  198. case SignDisplay::Always:
  199. return "always"sv;
  200. case SignDisplay::ExceptZero:
  201. return "exceptZero"sv;
  202. default:
  203. VERIFY_NOT_REACHED();
  204. }
  205. }
  206. }