DurationFormatConstructor.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  11. #include <LibJS/Runtime/Intl/DurationFormat.h>
  12. #include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
  13. #include <LibLocale/DurationFormat.h>
  14. namespace JS::Intl {
  15. JS_DEFINE_ALLOCATOR(DurationFormatConstructor);
  16. // 1.2 The Intl.DurationFormat Constructor, https://tc39.es/proposal-intl-duration-format/#sec-intl-durationformat-constructor
  17. DurationFormatConstructor::DurationFormatConstructor(Realm& realm)
  18. : NativeFunction(realm.vm().names.DurationFormat.as_string(), realm.intrinsics().function_prototype())
  19. {
  20. }
  21. void DurationFormatConstructor::initialize(Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. auto& vm = this->vm();
  25. // 1.3.1 Intl.DurationFormat.prototype, https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.prototype
  26. define_direct_property(vm.names.prototype, realm.intrinsics().intl_duration_format_prototype(), 0);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. u8 attr = Attribute::Writable | Attribute::Configurable;
  29. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  30. }
  31. // 1.2.1 Intl.DurationFormat ( [ locales [ , options ] ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat
  32. ThrowCompletionOr<Value> DurationFormatConstructor::call()
  33. {
  34. // 1. If NewTarget is undefined, throw a TypeError exception.
  35. return vm().throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Intl.DurationFormat");
  36. }
  37. // 1.2.1 Intl.DurationFormat ( [ locales [ , options ] ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat
  38. ThrowCompletionOr<NonnullGCPtr<Object>> DurationFormatConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. auto locales = vm.argument(0);
  42. auto options_value = vm.argument(1);
  43. // 2. Let durationFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%DurationFormatPrototype%", « [[InitializedDurationFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[YearsStyle]], [[YearsDisplay]], [[MonthsStyle]], [[MonthsDisplay]], [[WeeksStyle]], [[WeeksDisplay]], [[DaysStyle]], [[DaysDisplay]], [[HoursStyle]], [[HoursDisplay]], [[MinutesStyle]], [[MinutesDisplay]], [[SecondsStyle]], [[SecondsDisplay]], [[MillisecondsStyle]], [[MillisecondsDisplay]], [[MicrosecondsStyle]], [[MicrosecondsDisplay]], [[NanosecondsStyle]], [[NanosecondsDisplay]], [[HoursMinutesSeparator]], [[MinutesSecondsSeparator]], [[FractionalDigits]], [[TwoDigitHours]] »).
  44. auto duration_format = TRY(ordinary_create_from_constructor<DurationFormat>(vm, new_target, &Intrinsics::intl_duration_format_prototype));
  45. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  46. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  47. // 4. Let options be ? GetOptionsObject(options).
  48. auto* options = TRY(Temporal::get_options_object(vm, options_value));
  49. // 5. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
  50. auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
  51. // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", string, undefined, undefined).
  52. auto numbering_system = TRY(get_option(vm, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {}));
  53. // 7. If numberingSystem is not undefined, then
  54. if (!numbering_system.is_undefined()) {
  55. // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
  56. if (!::Locale::is_type_identifier(numbering_system.as_string().utf8_string_view()))
  57. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
  58. }
  59. // 8. Let opt be the Record { [[localeMatcher]]: matcher, [[nu]]: numberingSystem }.
  60. LocaleOptions opt {};
  61. opt.locale_matcher = matcher;
  62. if (!numbering_system.is_undefined())
  63. opt.nu = numbering_system.as_string().utf8_string();
  64. // 9. Let r be ResolveLocale(%DurationFormat%.[[AvailableLocales]], requestedLocales, opt, %DurationFormat%.[[RelevantExtensionKeys]], %DurationFormat%.[[LocaleData]]).
  65. auto result = resolve_locale(requested_locales, opt, DurationFormat::relevant_extension_keys());
  66. // 10. Let locale be r.[[locale]].
  67. auto locale = move(result.locale);
  68. // 11. Set durationFormat.[[Locale]] to locale.
  69. duration_format->set_locale(move(locale));
  70. // 12. Set durationFormat.[[DataLocale]] to r.[[dataLocale]].
  71. duration_format->set_data_locale(move(result.data_locale));
  72. // 13. Let dataLocale be durationFormat.[[DataLocale]].
  73. // 14. Let dataLocaleData be durationFormat.[[LocaleData]].[[<dataLocale>]].
  74. // 15. Let digitalFormat be dataLocaleData.[[DigitalFormat]].
  75. auto digital_format = ::Locale::digital_format(duration_format->data_locale());
  76. // 16. Let twoDigitHours be digitalFormat.[[TwoDigitHours]].
  77. // 17. Set durationFormat.[[TwoDigitHours]] to twoDigitHours.
  78. duration_format->set_two_digit_hours(digital_format.uses_two_digit_hours);
  79. // 18. Let hoursMinutesSeparator be digitalFormat.[[HoursMinutesSeparator]].
  80. // 19. Set durationFormat.[[HoursMinutesSeparator]] to hoursMinutesSeparator.
  81. duration_format->set_hours_minutes_separator(move(digital_format.hours_minutes_separator));
  82. // 20. Let minutesSecondsSeparator be digitalFormat.[[MinutesSecondsSeparator]].
  83. // 21. Set durationFormat.[[MinutesSecondsSeparator]] to minutesSecondsSeparator.
  84. duration_format->set_minutes_seconds_separator(move(digital_format.minutes_seconds_separator));
  85. // 22. Set durationFormat.[[NumberingSystem]] to r.[[nu]].
  86. if (result.nu.has_value())
  87. duration_format->set_numbering_system(result.nu.release_value());
  88. // 23. Let style be ? GetOption(options, "style", string, « "long", "short", "narrow", "digital" », "short").
  89. auto style = TRY(get_option(vm, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv, "digital"sv }, "short"sv));
  90. // 24. Set durationFormat.[[Style]] to style.
  91. duration_format->set_style(style.as_string().utf8_string_view());
  92. // 25. Let prevStyle be the empty String.
  93. String previous_style {};
  94. // 26. For each row of Table 3, except the header row, in table order, do
  95. for (auto const& duration_instances_component : duration_instances_components) {
  96. // a. Let styleSlot be the Style Slot value of the current row.
  97. auto style_slot = duration_instances_component.set_style_slot;
  98. // b. Let displaySlot be the Display Slot value of the current row.
  99. auto display_slot = duration_instances_component.set_display_slot;
  100. // c. Let unit be the Unit value of the current row.
  101. auto unit = MUST(String::from_utf8(duration_instances_component.unit));
  102. // d. Let valueList be the Values value of the current row.
  103. auto value_list = duration_instances_component.values;
  104. // e. Let digitalBase be the Digital Default value of the current row.
  105. auto digital_base = duration_instances_component.digital_default;
  106. // f. Let unitOptions be ? GetDurationUnitOptions(unit, options, style, valueList, digitalBase, prevStyle, twoDigitHours).
  107. auto unit_options = TRY(get_duration_unit_options(vm, unit, *options, duration_format->style_string(), value_list, digital_base, previous_style, duration_format->two_digit_hours()));
  108. // g. Set the value of the styleSlot slot of durationFormat to unitOptions.[[Style]].
  109. (duration_format->*style_slot)(unit_options.style);
  110. // h. Set the value of the displaySlot slot of durationFormat to unitOptions.[[Display]].
  111. (duration_format->*display_slot)(unit_options.display);
  112. // i. If unit is one of "hours", "minutes", "seconds", "milliseconds", or "microseconds", then
  113. if (unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv, "milliseconds"sv, "microseconds"sv)) {
  114. // i. Set prevStyle to unitOptions.[[Style]].
  115. previous_style = move(unit_options.style);
  116. }
  117. }
  118. // 27. Set durationFormat.[[FractionalDigits]] to ? GetNumberOption(options, "fractionalDigits", 0, 9, undefined).
  119. duration_format->set_fractional_digits(Optional<u8>(TRY(get_number_option(vm, *options, vm.names.fractionalDigits, 0, 9, {}))));
  120. // 28. Return durationFormat.
  121. return duration_format;
  122. }
  123. // 1.3.2 Intl.DurationFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.supportedLocalesOf
  124. JS_DEFINE_NATIVE_FUNCTION(DurationFormatConstructor::supported_locales_of)
  125. {
  126. auto locales = vm.argument(0);
  127. auto options = vm.argument(1);
  128. // 1. Let availableLocales be %DurationFormat%.[[AvailableLocales]].
  129. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  130. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  131. // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
  132. return TRY(supported_locales(vm, requested_locales, options));
  133. }
  134. }