DurationFormat.cpp 24 KB

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