DurationFormat.cpp 24 KB

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