DurationFormat.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2022-2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/DurationFormat.h>
  10. #include <LibJS/Runtime/Intl/ListFormat.h>
  11. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  12. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  13. #include <LibJS/Runtime/Intl/PluralRules.h>
  14. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  15. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  16. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  17. #include <LibJS/Runtime/ThrowableStringBuilder.h>
  18. namespace JS::Intl {
  19. // 1 DurationFormat Objects, https://tc39.es/proposal-intl-duration-format/#durationformat-objects
  20. DurationFormat::DurationFormat(Object& prototype)
  21. : Object(ConstructWithPrototypeTag::Tag, prototype)
  22. {
  23. }
  24. DurationFormat::Style DurationFormat::style_from_string(StringView style)
  25. {
  26. if (style == "long"sv)
  27. return Style::Long;
  28. if (style == "short"sv)
  29. return Style::Short;
  30. if (style == "narrow"sv)
  31. return Style::Narrow;
  32. if (style == "digital"sv)
  33. return Style::Digital;
  34. VERIFY_NOT_REACHED();
  35. }
  36. StringView DurationFormat::style_to_string(Style style)
  37. {
  38. switch (style) {
  39. case Style::Long:
  40. return "long"sv;
  41. case Style::Short:
  42. return "short"sv;
  43. case Style::Narrow:
  44. return "narrow"sv;
  45. case Style::Digital:
  46. return "digital"sv;
  47. default:
  48. VERIFY_NOT_REACHED();
  49. }
  50. }
  51. DurationFormat::ValueStyle DurationFormat::date_style_from_string(StringView date_style)
  52. {
  53. if (date_style == "long"sv)
  54. return ValueStyle::Long;
  55. if (date_style == "short"sv)
  56. return ValueStyle::Short;
  57. if (date_style == "narrow"sv)
  58. return ValueStyle::Narrow;
  59. VERIFY_NOT_REACHED();
  60. }
  61. DurationFormat::ValueStyle DurationFormat::time_style_from_string(StringView time_style)
  62. {
  63. if (time_style == "long"sv)
  64. return ValueStyle::Long;
  65. if (time_style == "short"sv)
  66. return ValueStyle::Short;
  67. if (time_style == "narrow"sv)
  68. return ValueStyle::Narrow;
  69. if (time_style == "numeric"sv)
  70. return ValueStyle::Numeric;
  71. if (time_style == "2-digit"sv)
  72. return ValueStyle::TwoDigit;
  73. VERIFY_NOT_REACHED();
  74. }
  75. DurationFormat::ValueStyle DurationFormat::sub_second_style_from_string(StringView sub_second_style)
  76. {
  77. if (sub_second_style == "long"sv)
  78. return ValueStyle::Long;
  79. if (sub_second_style == "short"sv)
  80. return ValueStyle::Short;
  81. if (sub_second_style == "narrow"sv)
  82. return ValueStyle::Narrow;
  83. if (sub_second_style == "numeric"sv)
  84. return ValueStyle::Numeric;
  85. VERIFY_NOT_REACHED();
  86. }
  87. DurationFormat::Display DurationFormat::display_from_string(StringView display)
  88. {
  89. if (display == "auto"sv)
  90. return Display::Auto;
  91. if (display == "always"sv)
  92. return Display::Always;
  93. VERIFY_NOT_REACHED();
  94. }
  95. StringView DurationFormat::value_style_to_string(ValueStyle value_style)
  96. {
  97. switch (value_style) {
  98. case ValueStyle::Long:
  99. return "long"sv;
  100. case ValueStyle::Short:
  101. return "short"sv;
  102. case ValueStyle::Narrow:
  103. return "narrow"sv;
  104. case ValueStyle::Numeric:
  105. return "numeric"sv;
  106. case ValueStyle::TwoDigit:
  107. return "2-digit"sv;
  108. default:
  109. VERIFY_NOT_REACHED();
  110. }
  111. }
  112. StringView DurationFormat::display_to_string(Display display)
  113. {
  114. switch (display) {
  115. case Display::Auto:
  116. return "auto"sv;
  117. case Display::Always:
  118. return "always"sv;
  119. default:
  120. VERIFY_NOT_REACHED();
  121. }
  122. }
  123. // 1.1.3 ToDurationRecord ( input ), https://tc39.es/proposal-intl-duration-format/#sec-todurationrecord
  124. ThrowCompletionOr<Temporal::DurationRecord> to_duration_record(VM& vm, Value input)
  125. {
  126. // 1. If Type(input) is not Object, then
  127. if (!input.is_object()) {
  128. // a. If Type(input) is String, throw a RangeError exception.
  129. if (input.is_string())
  130. return vm.throw_completion<RangeError>(ErrorType::NotAnObject, input);
  131. // b. Throw a TypeError exception.
  132. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, input);
  133. }
  134. auto& input_object = input.as_object();
  135. // 2. Let result be a new Duration Record with each field set to 0.
  136. Temporal::DurationRecord result = {};
  137. bool any_defined = false;
  138. auto set_duration_record_value = [&](auto const& name, auto& value_slot) -> ThrowCompletionOr<void> {
  139. auto value = TRY(input_object.get(name));
  140. if (!value.is_undefined()) {
  141. value_slot = TRY(Temporal::to_integer_if_integral(vm, value, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, name, value));
  142. any_defined = true;
  143. }
  144. return {};
  145. };
  146. // 3. Let days be ? Get(input, "days").
  147. // 4. If days is not undefined, set result.[[Days]] to ? ToIntegerIfIntegral(days).
  148. TRY(set_duration_record_value(vm.names.days, result.days));
  149. // 5. Let hours be ? Get(input, "hours").
  150. // 6. If hours is not undefined, set result.[[Hours]] to ? ToIntegerIfIntegral(hours).
  151. TRY(set_duration_record_value(vm.names.hours, result.hours));
  152. // 7. Let microseconds be ? Get(input, "microseconds").
  153. // 8. If microseconds is not undefined, set result.[[Microseconds]] to ? ToIntegerIfIntegral(microseconds).
  154. TRY(set_duration_record_value(vm.names.microseconds, result.microseconds));
  155. // 9. Let milliseconds be ? Get(input, "milliseconds").
  156. // 10. If milliseconds is not undefined, set result.[[Milliseconds]] to ? ToIntegerIfIntegral(milliseconds).
  157. TRY(set_duration_record_value(vm.names.milliseconds, result.milliseconds));
  158. // 11. Let minutes be ? Get(input, "minutes").
  159. // 12. If minutes is not undefined, set result.[[Minutes]] to ? ToIntegerIfIntegral(minutes).
  160. TRY(set_duration_record_value(vm.names.minutes, result.minutes));
  161. // 13. Let months be ? Get(input, "months").
  162. // 14. If months is not undefined, set result.[[Months]] to ? ToIntegerIfIntegral(months).
  163. TRY(set_duration_record_value(vm.names.months, result.months));
  164. // 15. Let nanoseconds be ? Get(input, "nanoseconds").
  165. // 16. If nanoseconds is not undefined, set result.[[Nanoseconds]] to ? ToIntegerIfIntegral(nanoseconds).
  166. TRY(set_duration_record_value(vm.names.nanoseconds, result.nanoseconds));
  167. // 17. Let seconds be ? Get(input, "seconds").
  168. // 18. If seconds is not undefined, set result.[[Seconds]] to ? ToIntegerIfIntegral(seconds).
  169. TRY(set_duration_record_value(vm.names.seconds, result.seconds));
  170. // 19. Let weeks be ? Get(input, "weeks").
  171. // 20. If weeks is not undefined, set result.[[Weeks]] to ? ToIntegerIfIntegral(weeks).
  172. TRY(set_duration_record_value(vm.names.weeks, result.weeks));
  173. // 21. Let years be ? Get(input, "years").
  174. // 22. If years is not undefined, set result.[[Years]] to ? ToIntegerIfIntegral(years).
  175. TRY(set_duration_record_value(vm.names.years, result.years));
  176. // 23. If years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds are all undefined, throw a TypeError exception.
  177. if (!any_defined)
  178. return vm.throw_completion<TypeError>(ErrorType::TemporalInvalidDurationLikeObject);
  179. // 24. If IsValidDurationRecord(result) is false, throw a RangeError exception.
  180. if (!is_valid_duration_record(result))
  181. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidDurationLikeObject);
  182. // 25. Return result.
  183. return result;
  184. }
  185. // 1.1.4 DurationRecordSign ( record ), https://tc39.es/proposal-intl-duration-format/#sec-durationrecordsign
  186. i8 duration_record_sign(Temporal::DurationRecord const& record)
  187. {
  188. // 1. For each row of Table 1, except the header row, in table order, do
  189. for (auto const& duration_instances_component : duration_instances_components) {
  190. // a. Let valueSlot be the Value Slot value of the current row.
  191. auto value_slot = duration_instances_component.value_slot;
  192. // b. Let v be record.[[<valueSlot>]].
  193. auto value = record.*value_slot;
  194. // c. If v < 0, return -1.
  195. if (value < 0)
  196. return -1;
  197. // d. If v > 0, return 1.
  198. if (value > 0)
  199. return 1;
  200. }
  201. // 2. Return 0.
  202. return 0;
  203. }
  204. // 1.1.5 IsValidDurationRecord ( record ), https://tc39.es/proposal-intl-duration-format/#sec-isvaliddurationrecord
  205. bool is_valid_duration_record(Temporal::DurationRecord const& record)
  206. {
  207. // 1. Let sign be DurationRecordSign(record).
  208. auto sign = duration_record_sign(record);
  209. // 2. For each row of Table 1, except the header row, in table order, do
  210. for (auto const& duration_instances_component : duration_instances_components) {
  211. // a. Let valueSlot be the Value Slot value of the current row.
  212. auto value_slot = duration_instances_component.value_slot;
  213. // b. Let v be record.[[<valueSlot>]].
  214. auto value = record.*value_slot;
  215. // c. Assert: 𝔽(v) is finite.
  216. VERIFY(isfinite(value));
  217. // d. If v < 0 and sign > 0, return false.
  218. if (value < 0 && sign > 0)
  219. return false;
  220. // e. If v > 0 and sign < 0, return false.
  221. if (value > 0 && sign < 0)
  222. return false;
  223. }
  224. // 3. Return true.
  225. return true;
  226. }
  227. // 1.1.6 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions
  228. ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String const& unit, Object const& options, StringView base_style, ReadonlySpan<StringView> styles_list, StringView digital_base, StringView previous_style)
  229. {
  230. // 1. Let style be ? GetOption(options, unit, string, stylesList, undefined).
  231. auto style_value = TRY(get_option(vm, options, unit.to_deprecated_string(), OptionType::String, styles_list, Empty {}));
  232. // 2. Let displayDefault be "always".
  233. auto display_default = "always"sv;
  234. StringView style;
  235. // 3. If style is undefined, then
  236. if (style_value.is_undefined()) {
  237. // a. If baseStyle is "digital", then
  238. if (base_style == "digital"sv) {
  239. // i. If unit is not one of "hours", "minutes", or "seconds", then
  240. if (!unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv)) {
  241. // 1. Set displayDefault to "auto".
  242. display_default = "auto"sv;
  243. }
  244. // ii. Set style to digitalBase.
  245. style = digital_base;
  246. }
  247. // b. Else,
  248. else {
  249. // i. Set displayDefault to "auto".
  250. display_default = "auto"sv;
  251. // ii. If prevStyle is "numeric" or "2-digit", then
  252. if (previous_style == "numeric"sv || previous_style == "2-digit"sv) {
  253. // 1. Set style to "numeric".
  254. style = "numeric"sv;
  255. }
  256. // iii. Else,
  257. else {
  258. // 1. Set style to baseStyle.
  259. style = base_style;
  260. }
  261. }
  262. } else {
  263. style = style_value.as_string().utf8_string_view();
  264. }
  265. // 4. Let displayField be the string-concatenation of unit and "Display".
  266. auto display_field = TRY_OR_THROW_OOM(vm, String::formatted("{}Display", unit));
  267. // 5. Let display be ? GetOption(options, displayField, string, « "auto", "always" », displayDefault).
  268. auto display = TRY(get_option(vm, options, display_field.to_deprecated_string(), OptionType::String, { "auto"sv, "always"sv }, display_default));
  269. // 6. If prevStyle is "numeric" or "2-digit", then
  270. if (previous_style == "numeric"sv || previous_style == "2-digit"sv) {
  271. // a. If style is not "numeric" or "2-digit", then
  272. if (style != "numeric"sv && style != "2-digit"sv) {
  273. // i. Throw a RangeError exception.
  274. return vm.throw_completion<RangeError>(ErrorType::IntlNonNumericOr2DigitAfterNumericOr2Digit);
  275. }
  276. // b. Else if unit is "minutes" or "seconds", then
  277. else if (unit == "minutes"sv || unit == "seconds"sv) {
  278. // i. Set style to "2-digit".
  279. style = "2-digit"sv;
  280. }
  281. }
  282. // 7. Return the Record { [[Style]]: style, [[Display]]: display }.
  283. return DurationUnitOptions { .style = TRY_OR_THROW_OOM(vm, String::from_utf8(style)), .display = display.as_string().utf8_string() };
  284. }
  285. // 1.1.7 PartitionDurationFormatPattern ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-partitiondurationformatpattern
  286. ThrowCompletionOr<Vector<PatternPartition>> partition_duration_format_pattern(VM& vm, DurationFormat const& duration_format, Temporal::DurationRecord const& duration)
  287. {
  288. auto& realm = *vm.current_realm();
  289. // 1. Let result be a new empty List.
  290. Vector<PatternPartition> result;
  291. // 2. Let done be false.
  292. bool done = false;
  293. // 3. While done is false, repeat for each row in Table 1 in order, except the header row:
  294. for (size_t i = 0; !done && i < duration_instances_components.size(); ++i) {
  295. auto const& duration_instances_component = duration_instances_components[i];
  296. // a. Let styleSlot be the Style Slot value.
  297. auto style_slot = duration_instances_component.get_style_slot;
  298. // b. Let displaySlot be the Display Slot value.
  299. auto display_slot = duration_instances_component.get_display_slot;
  300. // c. Let valueSlot be the Value Slot value.
  301. auto value_slot = duration_instances_component.value_slot;
  302. // d. Let unit be the Unit value.
  303. auto unit = duration_instances_component.unit;
  304. // e. Let numberFormatUnit be the NumberFormat Unit value.
  305. auto number_format_unit = duration_instances_component.number_format_unit;
  306. // f. Let style be durationFormat.[[<styleSlot>]].
  307. auto style = (duration_format.*style_slot)();
  308. // g. Let display be durationFormat.[[<displaySlot>]].
  309. auto display = (duration_format.*display_slot)();
  310. // h. Let value be duration.[[<valueSlot>]].
  311. auto value = duration.*value_slot;
  312. // i. Let nfOpts be OrdinaryObjectCreate(null).
  313. auto number_format_options = Object::create(realm, nullptr);
  314. // j. If unit is "seconds", "milliseconds", or "microseconds", then
  315. if (unit.is_one_of("seconds"sv, "milliseconds"sv, "microseconds"sv)) {
  316. DurationFormat::ValueStyle next_style;
  317. // i. If unit is "seconds", then
  318. if (unit == "seconds"sv) {
  319. // 1. Let nextStyle be durationFormat.[[MillisecondsStyle]].
  320. next_style = duration_format.milliseconds_style();
  321. }
  322. // ii. Else if unit is "milliseconds", then
  323. else if (unit == "milliseconds"sv) {
  324. // 1. Let nextStyle be durationFormat.[[MicrosecondsStyle]].
  325. next_style = duration_format.microseconds_style();
  326. }
  327. // iii. Else,
  328. else {
  329. // 1. Let nextStyle be durationFormat.[[NanosecondsStyle]].
  330. next_style = duration_format.nanoseconds_style();
  331. }
  332. // iv. If nextStyle is "numeric", then
  333. if (next_style == DurationFormat::ValueStyle::Numeric) {
  334. // 1. If unit is "seconds", then
  335. if (unit == "seconds"sv) {
  336. // a. Set value to value + duration.[[Milliseconds]] / 10^3 + duration.[[Microseconds]] / 10^6 + duration.[[Nanoseconds]] / 10^9.
  337. value += duration.milliseconds / 1'000.0 + duration.microseconds / 1'000'000.0 + duration.nanoseconds / 1'000'000'000.0;
  338. }
  339. // 2. Else if unit is "milliseconds", then
  340. else if (unit == "milliseconds"sv) {
  341. // a. Set value to value + duration.[[Microseconds]] / 10^3 + duration.[[Nanoseconds]] / 10^6.
  342. value += duration.microseconds / 1'000.0 + duration.nanoseconds / 1'000'000.0;
  343. }
  344. // 3. Else,
  345. else {
  346. // a. Set value to value + duration.[[Nanoseconds]] / 10^3.
  347. value += duration.nanoseconds / 1'000.0;
  348. }
  349. // 4. Perform ! CreateDataPropertyOrThrow(nfOpts, "maximumFractionDigits", durationFormat.[[FractionalDigits]]).
  350. MUST(number_format_options->create_data_property_or_throw(vm.names.maximumFractionDigits, duration_format.has_fractional_digits() ? Value(duration_format.fractional_digits()) : js_undefined()));
  351. // 5. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumFractionDigits", durationFormat.[[FractionalDigits]]).
  352. MUST(number_format_options->create_data_property_or_throw(vm.names.minimumFractionDigits, duration_format.has_fractional_digits() ? Value(duration_format.fractional_digits()) : js_undefined()));
  353. // 6. Set done to true.
  354. done = true;
  355. }
  356. }
  357. // k. If style is "2-digit", then
  358. if (style == DurationFormat::ValueStyle::TwoDigit) {
  359. // i. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumIntegerDigits", 2𝔽).
  360. MUST(number_format_options->create_data_property_or_throw(vm.names.minimumIntegerDigits, Value(2)));
  361. }
  362. // l. If value is not 0 or display is not "auto", then
  363. if (value != 0.0 || display != DurationFormat::Display::Auto) {
  364. // i. If style is "2-digit" or "numeric", then
  365. if (style == DurationFormat::ValueStyle::TwoDigit || style == DurationFormat::ValueStyle::Numeric) {
  366. // 1. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
  367. auto* number_format = static_cast<NumberFormat*>(MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), number_format_options)).ptr());
  368. // 2. Let dataLocale be durationFormat.[[DataLocale]].
  369. auto const& data_locale = duration_format.data_locale();
  370. // 3. Let dataLocaleData be %DurationFormat%.[[LocaleData]].[[<dataLocale>]].
  371. // 4. Let num be ! FormatNumeric(nf, 𝔽(value)).
  372. auto number = MUST_OR_THROW_OOM(format_numeric(vm, *number_format, MathematicalValue(value)));
  373. // 5. Append the new Record { [[Type]]: unit, [[Value]]: num} to the end of result.
  374. TRY_OR_THROW_OOM(vm, result.try_append({ unit, move(number) }));
  375. // 6. If unit is "hours" or "minutes", then
  376. if (unit.is_one_of("hours"sv, "minutes"sv)) {
  377. double next_value = 0.0;
  378. DurationFormat::Display next_display;
  379. // a. If unit is "hours", then
  380. if (unit == "hours"sv) {
  381. // i. Let nextValue be duration.[[Minutes]].
  382. next_value = duration.minutes;
  383. // ii. Let nextDisplay be durationFormat.[[MinutesDisplay]].
  384. next_display = duration_format.minutes_display();
  385. }
  386. // b. Else,
  387. else {
  388. // i. Let nextValue be duration.[[Seconds]].
  389. next_value = duration.seconds;
  390. // ii. Let nextDisplay be durationFormat.[[SecondsDisplay]].
  391. next_display = duration_format.seconds_display();
  392. // iii. If durationFormat.[[MillisecondsStyle]] is "numeric", then
  393. if (duration_format.milliseconds_style() == DurationFormat::ValueStyle::Numeric) {
  394. // i. Set nextValue to nextValue + duration.[[Milliseconds]] / 10^3 + duration.[[Microseconds]] / 10^6 + duration.[[Nanoseconds]] / 10^9.
  395. next_value += duration.milliseconds / 1'000.0 + duration.microseconds / 1'000'000.0 + duration.nanoseconds / 1'000'000'000.0;
  396. }
  397. }
  398. // c. If nextValue is not 0 or nextDisplay is not "auto", then
  399. if (next_value != 0.0 || next_display != DurationFormat::Display::Auto) {
  400. // i. Let separator be dataLocaleData.[[formats]].[[digital]].[[separator]].
  401. auto separator = TRY_OR_THROW_OOM(vm, ::Locale::get_number_system_symbol(data_locale, duration_format.numbering_system(), ::Locale::NumericSymbol::TimeSeparator)).value_or(":"sv);
  402. // ii. Append the new Record { [[Type]]: "literal", [[Value]]: separator} to the end of result.
  403. TRY_OR_THROW_OOM(vm, result.try_append({ "literal"sv, TRY_OR_THROW_OOM(vm, String::from_utf8(separator)) }));
  404. }
  405. }
  406. }
  407. // ii. Else,
  408. else {
  409. // 1. Perform ! CreateDataPropertyOrThrow(nfOpts, "style", "unit").
  410. MUST(number_format_options->create_data_property_or_throw(vm.names.style, PrimitiveString::create(vm, "unit"_string)));
  411. // 2. Perform ! CreateDataPropertyOrThrow(nfOpts, "unit", numberFormatUnit).
  412. MUST(number_format_options->create_data_property_or_throw(vm.names.unit, PrimitiveString::create(vm, number_format_unit)));
  413. // 3. Perform ! CreateDataPropertyOrThrow(nfOpts, "unitDisplay", style).
  414. auto unicode_style = ::Locale::style_to_string(static_cast<::Locale::Style>(style));
  415. MUST(number_format_options->create_data_property_or_throw(vm.names.unitDisplay, PrimitiveString::create(vm, unicode_style)));
  416. // 4. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
  417. auto* number_format = static_cast<NumberFormat*>(MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), number_format_options)).ptr());
  418. // 5. Let parts be ! PartitionNumberPattern(nf, 𝔽(value)).
  419. auto parts = MUST_OR_THROW_OOM(partition_number_pattern(vm, *number_format, MathematicalValue(value)));
  420. // 6. Let concat be an empty String.
  421. ThrowableStringBuilder concat(vm);
  422. // 7. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
  423. for (auto const& part : parts) {
  424. // a. Set concat to the string-concatenation of concat and part.[[Value]].
  425. TRY(concat.append(part.value));
  426. }
  427. // 8. Append the new Record { [[Type]]: unit, [[Value]]: concat } to the end of result.
  428. TRY_OR_THROW_OOM(vm, result.try_append({ unit, MUST_OR_THROW_OOM(concat.to_string()) }));
  429. }
  430. }
  431. }
  432. // 4. Let lfOpts be OrdinaryObjectCreate(null).
  433. auto list_format_options = Object::create(realm, nullptr);
  434. // 5. Perform ! CreateDataPropertyOrThrow(lfOpts, "type", "unit").
  435. MUST(list_format_options->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, "unit"_string)));
  436. // 6. Let listStyle be durationFormat.[[Style]].
  437. auto list_style = duration_format.style();
  438. // 7. If listStyle is "digital", then
  439. if (list_style == DurationFormat::Style::Digital) {
  440. // a. Set listStyle to "short".
  441. list_style = DurationFormat::Style::Short;
  442. }
  443. auto unicode_list_style = ::Locale::style_to_string(static_cast<::Locale::Style>(list_style));
  444. // 8. Perform ! CreateDataPropertyOrThrow(lfOpts, "style", listStyle).
  445. MUST(list_format_options->create_data_property_or_throw(vm.names.style, PrimitiveString::create(vm, unicode_list_style)));
  446. // 9. Let lf be ! Construct(%ListFormat%, « durationFormat.[[Locale]], lfOpts »).
  447. auto* list_format = static_cast<ListFormat*>(MUST(construct(vm, realm.intrinsics().intl_list_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), list_format_options)).ptr());
  448. // FIXME: CreatePartsFromList expects a list of strings and creates a list of Pattern Partition records, but we already created a list of Pattern Partition records
  449. // so we try to hack something together from it that looks mostly right
  450. Vector<String> string_result;
  451. bool merge = false;
  452. for (size_t i = 0; i < result.size(); ++i) {
  453. auto const& part = result[i];
  454. if (part.type == "literal") {
  455. string_result.last() = TRY_OR_THROW_OOM(vm, String::formatted("{}{}", string_result.last(), part.value));
  456. merge = true;
  457. continue;
  458. }
  459. if (merge) {
  460. string_result.last() = TRY_OR_THROW_OOM(vm, String::formatted("{}{}", string_result.last(), part.value));
  461. merge = false;
  462. continue;
  463. }
  464. TRY_OR_THROW_OOM(vm, string_result.try_append(part.value));
  465. }
  466. // 10. Set result to ! CreatePartsFromList(lf, result).
  467. auto final_result = MUST_OR_THROW_OOM(create_parts_from_list(vm, *list_format, string_result));
  468. // 11. Return result.
  469. return final_result;
  470. }
  471. }