DurationFormat.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/Intl/DurationFormat.h>
  8. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  9. namespace JS::Intl {
  10. // 1 DurationFormat Objects, https://tc39.es/proposal-intl-duration-format/#durationformat-objects
  11. DurationFormat::DurationFormat(Object& prototype)
  12. : Object(prototype)
  13. {
  14. }
  15. DurationFormat::Style DurationFormat::style_from_string(StringView style)
  16. {
  17. if (style == "long"sv)
  18. return Style::Long;
  19. if (style == "short"sv)
  20. return Style::Short;
  21. if (style == "narrow"sv)
  22. return Style::Narrow;
  23. if (style == "digital"sv)
  24. return Style::Digital;
  25. VERIFY_NOT_REACHED();
  26. }
  27. StringView DurationFormat::style_to_string(Style style)
  28. {
  29. switch (style) {
  30. case Style::Long:
  31. return "long"sv;
  32. case Style::Short:
  33. return "short"sv;
  34. case Style::Narrow:
  35. return "narrow"sv;
  36. case Style::Digital:
  37. return "digital"sv;
  38. default:
  39. VERIFY_NOT_REACHED();
  40. }
  41. }
  42. DurationFormat::ValueStyle DurationFormat::date_style_from_string(StringView date_style)
  43. {
  44. if (date_style == "long"sv)
  45. return ValueStyle::Long;
  46. if (date_style == "short"sv)
  47. return ValueStyle::Short;
  48. if (date_style == "narrow"sv)
  49. return ValueStyle::Narrow;
  50. VERIFY_NOT_REACHED();
  51. }
  52. DurationFormat::ValueStyle DurationFormat::time_style_from_string(StringView time_style)
  53. {
  54. if (time_style == "long"sv)
  55. return ValueStyle::Long;
  56. if (time_style == "short"sv)
  57. return ValueStyle::Short;
  58. if (time_style == "narrow"sv)
  59. return ValueStyle::Narrow;
  60. if (time_style == "numeric"sv)
  61. return ValueStyle::Numeric;
  62. if (time_style == "2-digit"sv)
  63. return ValueStyle::TwoDigit;
  64. VERIFY_NOT_REACHED();
  65. }
  66. DurationFormat::ValueStyle DurationFormat::sub_second_style_from_string(StringView sub_second_style)
  67. {
  68. if (sub_second_style == "long"sv)
  69. return ValueStyle::Long;
  70. if (sub_second_style == "short"sv)
  71. return ValueStyle::Short;
  72. if (sub_second_style == "narrow"sv)
  73. return ValueStyle::Narrow;
  74. if (sub_second_style == "numeric"sv)
  75. return ValueStyle::Numeric;
  76. VERIFY_NOT_REACHED();
  77. }
  78. DurationFormat::Display DurationFormat::display_from_string(StringView display)
  79. {
  80. if (display == "auto"sv)
  81. return Display::Auto;
  82. if (display == "always"sv)
  83. return Display::Always;
  84. VERIFY_NOT_REACHED();
  85. }
  86. StringView DurationFormat::value_style_to_string(ValueStyle value_style)
  87. {
  88. switch (value_style) {
  89. case ValueStyle::Long:
  90. return "long"sv;
  91. case ValueStyle::Short:
  92. return "short"sv;
  93. case ValueStyle::Narrow:
  94. return "narrow"sv;
  95. case ValueStyle::Numeric:
  96. return "numeric"sv;
  97. case ValueStyle::TwoDigit:
  98. return "2-digit"sv;
  99. default:
  100. VERIFY_NOT_REACHED();
  101. }
  102. }
  103. StringView DurationFormat::display_to_string(Display display)
  104. {
  105. switch (display) {
  106. case Display::Auto:
  107. return "auto"sv;
  108. case Display::Always:
  109. return "always"sv;
  110. default:
  111. VERIFY_NOT_REACHED();
  112. }
  113. }
  114. // 1.1.1 ToDurationRecord ( input ), https://tc39.es/proposal-intl-duration-format/#sec-todurationrecord
  115. ThrowCompletionOr<Temporal::DurationRecord> to_duration_record(GlobalObject& global_object, Value input)
  116. {
  117. auto& vm = global_object.vm();
  118. // 1. If Type(input) is not Object, throw a TypeError exception.
  119. if (!input.is_object())
  120. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, input);
  121. auto& input_object = input.as_object();
  122. // 2. Let result be a new Record.
  123. Temporal::DurationRecord result;
  124. // 3. Let any be false.
  125. auto any = false;
  126. // 4. For each row in Table 1, except the header row, in table order, do
  127. for (auto const& duration_instances_component : duration_instances_components) {
  128. // a. Let valueSlot be the Value Slot value.
  129. auto value_slot = duration_instances_component.value_slot;
  130. // b. Let unit be the Unit value.
  131. auto unit = duration_instances_component.unit;
  132. // c. Let value be ? Get(input, unit).
  133. auto value = TRY(input_object.get(FlyString(unit)));
  134. double value_number;
  135. // d. If value is not undefined, then
  136. if (!value.is_undefined()) {
  137. // i. Set any to true.
  138. any = true;
  139. // ii. Set value to ? ToIntegerWithoutRounding(value).
  140. value_number = TRY(Temporal::to_integer_without_rounding(global_object, value, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, unit, value));
  141. }
  142. // e. Else,
  143. else {
  144. // i. Set value to 0.
  145. value_number = 0;
  146. }
  147. // f. Set the field of result whose name is valueSlot to value.
  148. result.*value_slot = value_number;
  149. }
  150. // 5. If any is false, throw a TypeError exception.
  151. if (!any)
  152. return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
  153. // 6. Return result.
  154. return result;
  155. }
  156. // 1.1.2 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions
  157. ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(GlobalObject& global_object, String const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, Optional<String> const& previous_style)
  158. {
  159. auto& vm = global_object.vm();
  160. // 1. Let style be ? GetOption(options, unit, "string", stylesList, undefined).
  161. auto style_value = TRY(get_option(global_object, options, unit, OptionType::String, styles_list, Empty {}));
  162. // 2. Let displayDefault be "always".
  163. auto display_default = "always"sv;
  164. String style;
  165. // 3. If style is undefined, then
  166. if (style_value.is_undefined()) {
  167. // a. Set displayDefault to "auto".
  168. display_default = "auto"sv;
  169. // b. If baseStyle is "digital", then
  170. if (base_style == "digital"sv) {
  171. // i. Set style to digitalBase.
  172. style = digital_base;
  173. }
  174. // c. Else,
  175. else {
  176. // i. Set style to baseStyle.
  177. style = base_style;
  178. }
  179. } else {
  180. style = style_value.as_string().string();
  181. }
  182. // 4. Let displayField be the string-concatenation of unit and "Display".
  183. auto display_field = String::formatted("{}Display", unit);
  184. // 5. Let display be ? GetOption(options, displayField, "string", « "auto", "always" », displayDefault).
  185. auto display = TRY(get_option(global_object, options, display_field, OptionType::String, { "auto"sv, "always"sv }, display_default));
  186. // 6. If prevStyle is "numeric" or "2-digit", then
  187. if (previous_style == "numeric"sv || previous_style == "2-digit"sv) {
  188. // a. If style is not "numeric" or "2-digit", then
  189. if (style != "numeric"sv && style != "2-digit"sv) {
  190. // i. Throw a RangeError exception.
  191. return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNonNumericOr2DigitAfterNumericOr2Digit);
  192. }
  193. // b. Else if unit is "minutes" or "seconds", then
  194. else if (unit == "minutes"sv || unit == "seconds"sv) {
  195. // i. Set style to "2-digit".
  196. style = "2-digit"sv;
  197. }
  198. }
  199. // 7. Return the Record { [[Style]]: style, [[Display]]: display }.
  200. return DurationUnitOptions { .style = move(style), .display = display.as_string().string() };
  201. }
  202. }