NumberFormatConstructor.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  10. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  11. #include <LibUnicode/Locale.h>
  12. namespace JS::Intl {
  13. JS_DEFINE_ALLOCATOR(NumberFormatConstructor);
  14. // 15.1 The Intl.NumberFormat Constructor, https://tc39.es/ecma402/#sec-intl-numberformat-constructor
  15. NumberFormatConstructor::NumberFormatConstructor(Realm& realm)
  16. : NativeFunction(realm.vm().names.NumberFormat.as_string(), realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void NumberFormatConstructor::initialize(Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. auto& vm = this->vm();
  23. // 15.2.1 Intl.NumberFormat.prototype, https://tc39.es/ecma402/#sec-intl.numberformat.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().intl_number_format_prototype(), 0);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. }
  29. // 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
  30. ThrowCompletionOr<Value> NumberFormatConstructor::call()
  31. {
  32. // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
  33. return TRY(construct(*this));
  34. }
  35. // 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
  36. ThrowCompletionOr<NonnullGCPtr<Object>> NumberFormatConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto locales_value = vm.argument(0);
  40. auto options_value = vm.argument(1);
  41. // 2. Let numberFormat be ? OrdinaryCreateFromConstructor(newTarget, "%Intl.NumberFormat.prototype%", « [[InitializedNumberFormat]], [[Locale]], [[LocaleData]], [[NumberingSystem]], [[Style]], [[Unit]], [[UnitDisplay]], [[Currency]], [[CurrencyDisplay]], [[CurrencySign]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]], [[Notation]], [[CompactDisplay]], [[UseGrouping]], [[SignDisplay]], [[RoundingIncrement]], [[RoundingMode]], [[ComputedRoundingPriority]], [[TrailingZeroDisplay]], [[BoundFormat]] »).
  42. auto number_format = TRY(ordinary_create_from_constructor<NumberFormat>(vm, new_target, &Intrinsics::intl_number_format_prototype));
  43. // 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  44. auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value));
  45. // 4. Set options to ? CoerceOptionsToObject(options).
  46. auto* options = TRY(coerce_options_to_object(vm, options_value));
  47. // 5. Let opt be a new Record.
  48. LocaleOptions opt {};
  49. // 6. 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. // 7. Set opt.[[localeMatcher]] to matcher.
  52. opt.locale_matcher = matcher;
  53. // 8. Let numberingSystem be ? GetOption(options, "numberingSystem", string, empty, undefined).
  54. auto numbering_system = TRY(get_option(vm, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {}));
  55. // 9. If numberingSystem is not undefined, then
  56. if (!numbering_system.is_undefined()) {
  57. // a. If numberingSystem cannot be matched by the type Unicode locale nonterminal, throw a RangeError exception.
  58. if (!Unicode::is_type_identifier(numbering_system.as_string().utf8_string_view()))
  59. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
  60. }
  61. // 10. Set opt.[[nu]] to numberingSystem.
  62. opt.nu = locale_key_from_value(numbering_system);
  63. // 11. Let r be ResolveLocale(%Intl.NumberFormat%.[[AvailableLocales]], requestedLocales, opt, %Intl.NumberFormat%.[[RelevantExtensionKeys]], %Intl.NumberFormat%.[[LocaleData]]).
  64. auto result = resolve_locale(requested_locales, opt, NumberFormat::relevant_extension_keys());
  65. // 12. Set numberFormat.[[Locale]] to r.[[Locale]].
  66. number_format->set_locale(move(result.locale));
  67. // 13. Set numberFormat.[[LocaleData]] to r.[[LocaleData]].
  68. // 14. Set numberFormat.[[NumberingSystem]] to r.[[nu]].
  69. if (auto* resolved_numbering_system = result.nu.get_pointer<String>())
  70. number_format->set_numbering_system(move(*resolved_numbering_system));
  71. // 15. Perform ? SetNumberFormatUnitOptions(numberFormat, options).
  72. TRY(set_number_format_unit_options(vm, number_format, *options));
  73. // 16. Let style be numberFormat.[[Style]].
  74. auto style = number_format->style();
  75. int default_min_fraction_digits = 0;
  76. int default_max_fraction_digits = 0;
  77. // 17. If style is "currency", then
  78. if (style == Unicode::NumberFormatStyle::Currency) {
  79. // a. Let currency be numberFormat.[[Currency]].
  80. auto const& currency = number_format->currency();
  81. // b. Let cDigits be CurrencyDigits(currency).
  82. int digits = currency_digits(currency);
  83. // c. Let mnfdDefault be cDigits.
  84. default_min_fraction_digits = digits;
  85. // d. Let mxfdDefault be cDigits.
  86. default_max_fraction_digits = digits;
  87. }
  88. // 18. Else,
  89. else {
  90. // a. Let mnfdDefault be 0.
  91. default_min_fraction_digits = 0;
  92. // b. If style is "percent", then
  93. // i. Let mxfdDefault be 0.
  94. // c. Else,
  95. // i. Let mxfdDefault be 3.
  96. default_max_fraction_digits = style == Unicode::NumberFormatStyle::Percent ? 0 : 3;
  97. }
  98. // 19. Let notation be ? GetOption(options, "notation", string, « "standard", "scientific", "engineering", "compact" », "standard").
  99. auto notation = TRY(get_option(vm, *options, vm.names.notation, OptionType::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv));
  100. // 20. Set numberFormat.[[Notation]] to notation.
  101. number_format->set_notation(notation.as_string().utf8_string_view());
  102. // 21. Perform ? SetNumberFormatDigitOptions(numberFormat, options, mnfdDefault, mxfdDefault, notation).
  103. TRY(set_number_format_digit_options(vm, number_format, *options, default_min_fraction_digits, default_max_fraction_digits, number_format->notation()));
  104. // 22. Let compactDisplay be ? GetOption(options, "compactDisplay", string, « "short", "long" », "short").
  105. auto compact_display = TRY(get_option(vm, *options, vm.names.compactDisplay, OptionType::String, { "short"sv, "long"sv }, "short"sv));
  106. // 23. Let defaultUseGrouping be "auto".
  107. auto default_use_grouping = "auto"sv;
  108. // 24. If notation is "compact", then
  109. if (number_format->notation() == Unicode::Notation::Compact) {
  110. // a. Set numberFormat.[[CompactDisplay]] to compactDisplay.
  111. number_format->set_compact_display(compact_display.as_string().utf8_string_view());
  112. // b. Set defaultUseGrouping to "min2".
  113. default_use_grouping = "min2"sv;
  114. }
  115. // 25. NOTE: For historical reasons, the strings "true" and "false" are accepted and replaced with the default value.
  116. // 26. Let useGrouping be ? GetBooleanOrStringNumberFormatOption(options, "useGrouping", « "min2", "auto", "always", "true", "false" », defaultUseGrouping).
  117. auto use_grouping = TRY(get_boolean_or_string_number_format_option(vm, *options, vm.names.useGrouping, { "min2"sv, "auto"sv, "always"sv, "true"sv, "false"sv }, default_use_grouping));
  118. // 27. If useGrouping is "true" or useGrouping is "false", set useGrouping to defaultUseGrouping.
  119. if (auto const* use_grouping_string = use_grouping.get_pointer<StringView>()) {
  120. if (use_grouping_string->is_one_of("true"sv, "false"sv))
  121. use_grouping = default_use_grouping;
  122. }
  123. // 28. If useGrouping is true, set useGrouping to "always".
  124. if (auto const* use_grouping_boolean = use_grouping.get_pointer<bool>()) {
  125. if (*use_grouping_boolean)
  126. use_grouping = "always"sv;
  127. }
  128. // 29. Set numberFormat.[[UseGrouping]] to useGrouping.
  129. number_format->set_use_grouping(use_grouping);
  130. // 30. Let signDisplay be ? GetOption(options, "signDisplay", string, « "auto", "never", "always", "exceptZero", "negative" », "auto").
  131. auto sign_display = TRY(get_option(vm, *options, vm.names.signDisplay, OptionType::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv, "negative"sv }, "auto"sv));
  132. // 31. Set numberFormat.[[SignDisplay]] to signDisplay.
  133. number_format->set_sign_display(sign_display.as_string().utf8_string_view());
  134. // 32. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
  135. // a. Let this be the this value.
  136. // b. Return ? ChainNumberFormat(numberFormat, NewTarget, this).
  137. // Non-standard, create an ICU number formatter for this Intl object.
  138. auto formatter = Unicode::NumberFormat::create(
  139. number_format->locale(),
  140. number_format->numbering_system(),
  141. number_format->display_options(),
  142. number_format->rounding_options());
  143. number_format->set_formatter(move(formatter));
  144. // 33. Return numberFormat.
  145. return number_format;
  146. }
  147. // 15.1.2 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/ecma402/#sec-setnfdigitoptions
  148. ThrowCompletionOr<void> set_number_format_digit_options(VM& vm, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, Unicode::Notation notation)
  149. {
  150. // 1. Let mnid be ? GetNumberOption(options, "minimumIntegerDigits,", 1, 21, 1).
  151. auto min_integer_digits = TRY(get_number_option(vm, options, vm.names.minimumIntegerDigits, 1, 21, 1));
  152. // 2. Let mnfd be ? Get(options, "minimumFractionDigits").
  153. auto min_fraction_digits = TRY(options.get(vm.names.minimumFractionDigits));
  154. // 3. Let mxfd be ? Get(options, "maximumFractionDigits").
  155. auto max_fraction_digits = TRY(options.get(vm.names.maximumFractionDigits));
  156. // 4. Let mnsd be ? Get(options, "minimumSignificantDigits").
  157. auto min_significant_digits = TRY(options.get(vm.names.minimumSignificantDigits));
  158. // 5. Let mxsd be ? Get(options, "maximumSignificantDigits").
  159. auto max_significant_digits = TRY(options.get(vm.names.maximumSignificantDigits));
  160. // 6. Set intlObj.[[MinimumIntegerDigits]] to mnid.
  161. intl_object.set_min_integer_digits(*min_integer_digits);
  162. // 7. Let roundingIncrement be ? GetNumberOption(options, "roundingIncrement", 1, 5000, 1).
  163. auto rounding_increment = TRY(get_number_option(vm, options, vm.names.roundingIncrement, 1, 5000, 1));
  164. // 8. If roundingIncrement is not in « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 », throw a RangeError exception.
  165. static constexpr auto sanctioned_rounding_increments = AK::Array { 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 };
  166. if (!sanctioned_rounding_increments.span().contains_slow(*rounding_increment))
  167. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidRoundingIncrement, *rounding_increment);
  168. // 9. Let roundingMode be ? GetOption(options, "roundingMode", string, « "ceil", "floor", "expand", "trunc", "halfCeil", "halfFloor", "halfExpand", "halfTrunc", "halfEven" », "halfExpand").
  169. auto rounding_mode = TRY(get_option(vm, options, vm.names.roundingMode, OptionType::String, { "ceil"sv, "floor"sv, "expand"sv, "trunc"sv, "halfCeil"sv, "halfFloor"sv, "halfExpand"sv, "halfTrunc"sv, "halfEven"sv }, "halfExpand"sv));
  170. // 10. Let roundingPriority be ? GetOption(options, "roundingPriority", string, « "auto", "morePrecision", "lessPrecision" », "auto").
  171. auto rounding_priority_option = TRY(get_option(vm, options, vm.names.roundingPriority, OptionType::String, { "auto"sv, "morePrecision"sv, "lessPrecision"sv }, "auto"sv));
  172. auto rounding_priority = rounding_priority_option.as_string().utf8_string_view();
  173. // 11. Let trailingZeroDisplay be ? GetOption(options, "trailingZeroDisplay", string, « "auto", "stripIfInteger" », "auto").
  174. auto trailing_zero_display = TRY(get_option(vm, options, vm.names.trailingZeroDisplay, OptionType::String, { "auto"sv, "stripIfInteger"sv }, "auto"sv));
  175. // 12. NOTE: All fields required by SetNumberFormatDigitOptions have now been read from options. The remainder of this AO interprets the options and may throw exceptions.
  176. // 13. If roundingIncrement is not 1, set mxfdDefault to mnfdDefault.
  177. if (rounding_increment != 1)
  178. default_max_fraction_digits = default_min_fraction_digits;
  179. // 14. Set intlObj.[[RoundingIncrement]] to roundingIncrement.
  180. intl_object.set_rounding_increment(*rounding_increment);
  181. // 15. Set intlObj.[[RoundingMode]] to roundingMode.
  182. intl_object.set_rounding_mode(rounding_mode.as_string().utf8_string_view());
  183. // 16. Set intlObj.[[TrailingZeroDisplay]] to trailingZeroDisplay.
  184. intl_object.set_trailing_zero_display(trailing_zero_display.as_string().utf8_string_view());
  185. // 17. If mnsd is undefined and mxsd is undefined, let hasSd be false. Otherwise, let hasSd be true.
  186. bool has_significant_digits = !min_significant_digits.is_undefined() || !max_significant_digits.is_undefined();
  187. // 18. If mnfd is undefined and mxsd is undefined, let hasFd be false. Otherwise, let hasFd be true.
  188. bool has_fraction_digits = !min_fraction_digits.is_undefined() || !max_fraction_digits.is_undefined();
  189. // 19. Let needSd be true.
  190. bool need_significant_digits = true;
  191. // 20. Let needFd be true.
  192. bool need_fraction_digits = true;
  193. // 21. If roundingPriority is "auto", then
  194. if (rounding_priority == "auto"sv) {
  195. // a. Set needSd to hasSd.
  196. need_significant_digits = has_significant_digits;
  197. // b. If hasSd is true, or hasFd is false and notation is "compact", then
  198. if (has_significant_digits || (!has_fraction_digits && notation == Unicode::Notation::Compact)) {
  199. // i. Set needFd to false.
  200. need_fraction_digits = false;
  201. }
  202. }
  203. // 22. If needSd is true, then
  204. if (need_significant_digits) {
  205. // a. If hasSd is true, then
  206. if (has_significant_digits) {
  207. // i. Set intlObj.[[MinimumSignificantDigits]] to ? DefaultNumberOption(mnsd, 1, 21, 1).
  208. auto min_digits = TRY(default_number_option(vm, min_significant_digits, 1, 21, 1));
  209. intl_object.set_min_significant_digits(*min_digits);
  210. // ii. Set intlObj.[[MaximumSignificantDigits]] to ? DefaultNumberOption(mxsd, intlObj.[[MinimumSignificantDigits]], 21, 21).
  211. auto max_digits = TRY(default_number_option(vm, max_significant_digits, *min_digits, 21, 21));
  212. intl_object.set_max_significant_digits(*max_digits);
  213. }
  214. // b. Else,
  215. else {
  216. // i. Set intlObj.[[MinimumSignificantDigits]] to 1.
  217. intl_object.set_min_significant_digits(1);
  218. // ii. Set intlObj.[[MaximumSignificantDigits]] to 21.
  219. intl_object.set_max_significant_digits(21);
  220. }
  221. }
  222. // 23. If needFd is true, then
  223. if (need_fraction_digits) {
  224. // a. If hasFd is true, then
  225. if (has_fraction_digits) {
  226. // i. Set mnfd to ? DefaultNumberOption(mnfd, 0, 100, undefined).
  227. auto min_digits = TRY(default_number_option(vm, min_fraction_digits, 0, 100, {}));
  228. // ii. Set mxfd to ? DefaultNumberOption(mxfd, 0, 100, undefined).
  229. auto max_digits = TRY(default_number_option(vm, max_fraction_digits, 0, 100, {}));
  230. // iii. If mnfd is undefined, set mnfd to min(mnfdDefault, mxfd).
  231. if (!min_digits.has_value())
  232. min_digits = min(default_min_fraction_digits, *max_digits);
  233. // iv. Else if mxfd is undefined, set mxfd to max(mxfdDefault, mnfd).
  234. else if (!max_digits.has_value())
  235. max_digits = max(default_max_fraction_digits, *min_digits);
  236. // v. Else if mnfd is greater than mxfd, throw a RangeError exception.
  237. else if (*min_digits > *max_digits)
  238. return vm.throw_completion<RangeError>(ErrorType::IntlMinimumExceedsMaximum, *min_digits, *max_digits);
  239. // vi. Set intlObj.[[MinimumFractionDigits]] to mnfd.
  240. intl_object.set_min_fraction_digits(*min_digits);
  241. // vii. Set intlObj.[[MaximumFractionDigits]] to mxfd.
  242. intl_object.set_max_fraction_digits(*max_digits);
  243. }
  244. // b. Else,
  245. else {
  246. // i. Set intlObj.[[MinimumFractionDigits]] to mnfdDefault.
  247. intl_object.set_min_fraction_digits(default_min_fraction_digits);
  248. // ii. Set intlObj.[[MaximumFractionDigits]] to mxfdDefault.
  249. intl_object.set_max_fraction_digits(default_max_fraction_digits);
  250. }
  251. }
  252. // 24. If needSd is false and needFd is false, then
  253. if (!need_significant_digits && !need_fraction_digits) {
  254. // a. Set intlObj.[[MinimumFractionDigits]] to 0.
  255. intl_object.set_min_fraction_digits(0);
  256. // b. Set intlObj.[[MaximumFractionDigits]] to 0.
  257. intl_object.set_max_fraction_digits(0);
  258. // c. Set intlObj.[[MinimumSignificantDigits]] to 1.
  259. intl_object.set_min_significant_digits(1);
  260. // d. Set intlObj.[[MaximumSignificantDigits]] to 2.
  261. intl_object.set_max_significant_digits(2);
  262. // e. Set intlObj.[[RoundingType]] to MORE-PRECISION.
  263. intl_object.set_rounding_type(Unicode::RoundingType::MorePrecision);
  264. // f. Set intlObj.[[ComputedRoundingPriority]] to "morePrecision".
  265. intl_object.set_computed_rounding_priority(NumberFormatBase::ComputedRoundingPriority::MorePrecision);
  266. }
  267. // 25. Else if roundingPriority is "morePrecision", then
  268. else if (rounding_priority == "morePrecision"sv) {
  269. // a. Set intlObj.[[RoundingType]] to MORE-PRECISION.
  270. intl_object.set_rounding_type(Unicode::RoundingType::MorePrecision);
  271. // b. Set intlObj.[[ComputedRoundingPriority]] to "morePrecision".
  272. intl_object.set_computed_rounding_priority(NumberFormatBase::ComputedRoundingPriority::MorePrecision);
  273. }
  274. // 26. Else if roundingPriority is "lessPrecision", then
  275. else if (rounding_priority == "lessPrecision"sv) {
  276. // a. Set intlObj.[[RoundingType]] to LESS-PRECISION.
  277. intl_object.set_rounding_type(Unicode::RoundingType::LessPrecision);
  278. // b. Set intlObj.[[ComputedRoundingPriority]] to "lessPrecision".
  279. intl_object.set_computed_rounding_priority(NumberFormatBase::ComputedRoundingPriority::LessPrecision);
  280. }
  281. // 27. Else if hasSd is true, then
  282. else if (has_significant_digits) {
  283. // a. Set intlObj.[[RoundingType]] to SIGNIFICANT-DIGITS.
  284. intl_object.set_rounding_type(Unicode::RoundingType::SignificantDigits);
  285. // b. Set intlObj.[[ComputedRoundingPriority]] to "auto".
  286. intl_object.set_computed_rounding_priority(NumberFormatBase::ComputedRoundingPriority::Auto);
  287. }
  288. // 28. Else,
  289. else {
  290. // a. Set intlObj.[[RoundingType]] to FRACTION-DIGITS.
  291. intl_object.set_rounding_type(Unicode::RoundingType::FractionDigits);
  292. // b. Set intlObj.[[ComputedRoundingPriority]] to "auto".
  293. intl_object.set_computed_rounding_priority(NumberFormatBase::ComputedRoundingPriority::Auto);
  294. }
  295. // 29. If roundingIncrement is not 1, then
  296. if (rounding_increment != 1) {
  297. // a. If intlObj.[[RoundingType]] is not FRACTION-DIGITS, throw a TypeError exception.
  298. if (intl_object.rounding_type() != Unicode::RoundingType::FractionDigits)
  299. return vm.throw_completion<TypeError>(ErrorType::IntlInvalidRoundingIncrementForRoundingType, *rounding_increment, intl_object.rounding_type_string());
  300. // b. If intlObj.[[MaximumFractionDigits]] is not equal to intlObj.[[MinimumFractionDigits]], throw a RangeError exception.
  301. if (intl_object.max_fraction_digits() != intl_object.min_fraction_digits())
  302. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidRoundingIncrementForFractionDigits, *rounding_increment);
  303. }
  304. // 30. Return UNUSED.
  305. return {};
  306. }
  307. // 15.1.3 SetNumberFormatUnitOptions ( intlObj, options ), https://tc39.es/ecma402/#sec-setnumberformatunitoptions
  308. ThrowCompletionOr<void> set_number_format_unit_options(VM& vm, NumberFormat& intl_object, Object const& options)
  309. {
  310. // 1. Let style be ? GetOption(options, "style", string, « "decimal", "percent", "currency", "unit" », "decimal").
  311. auto style = TRY(get_option(vm, options, vm.names.style, OptionType::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv));
  312. // 2. Set intlObj.[[Style]] to style.
  313. intl_object.set_style(style.as_string().utf8_string_view());
  314. // 3. Let currency be ? GetOption(options, "currency", string, empty, undefined).
  315. auto currency = TRY(get_option(vm, options, vm.names.currency, OptionType::String, {}, Empty {}));
  316. // 4. If currency is undefined, then
  317. if (currency.is_undefined()) {
  318. // a. If style is "currency", throw a TypeError exception.
  319. if (intl_object.style() == Unicode::NumberFormatStyle::Currency)
  320. return vm.throw_completion<TypeError>(ErrorType::IntlOptionUndefined, "currency"sv, "style"sv, style);
  321. }
  322. // 5. Else,
  323. // a. If IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
  324. else if (!is_well_formed_currency_code(currency.as_string().utf8_string_view())) {
  325. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, currency, "currency"sv);
  326. }
  327. // 6. Let currencyDisplay be ? GetOption(options, "currencyDisplay", string, « "code", "symbol", "narrowSymbol", "name" », "symbol").
  328. auto currency_display = TRY(get_option(vm, options, vm.names.currencyDisplay, OptionType::String, { "code"sv, "symbol"sv, "narrowSymbol"sv, "name"sv }, "symbol"sv));
  329. // 7. Let currencySign be ? GetOption(options, "currencySign", string, « "standard", "accounting" », "standard").
  330. auto currency_sign = TRY(get_option(vm, options, vm.names.currencySign, OptionType::String, { "standard"sv, "accounting"sv }, "standard"sv));
  331. // 8. Let unit be ? GetOption(options, "unit", string, empty, undefined).
  332. auto unit = TRY(get_option(vm, options, vm.names.unit, OptionType::String, {}, Empty {}));
  333. // 9. If unit is undefined, then
  334. if (unit.is_undefined()) {
  335. // a. If style is "unit", throw a TypeError exception.
  336. if (intl_object.style() == Unicode::NumberFormatStyle::Unit)
  337. return vm.throw_completion<TypeError>(ErrorType::IntlOptionUndefined, "unit"sv, "style"sv, style);
  338. }
  339. // 10. Else,
  340. // a. If IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
  341. else if (!is_well_formed_unit_identifier(unit.as_string().utf8_string_view())) {
  342. return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, unit, "unit"sv);
  343. }
  344. // 11. Let unitDisplay be ? GetOption(options, "unitDisplay", string, « "short", "narrow", "long" », "short").
  345. auto unit_display = TRY(get_option(vm, options, vm.names.unitDisplay, OptionType::String, { "short"sv, "narrow"sv, "long"sv }, "short"sv));
  346. // 12. If style is "currency", then
  347. if (intl_object.style() == Unicode::NumberFormatStyle::Currency) {
  348. // a. Set intlObj.[[Currency]] to the ASCII-uppercase of currency.
  349. intl_object.set_currency(MUST(currency.as_string().utf8_string().to_uppercase()));
  350. // c. Set intlObj.[[CurrencyDisplay]] to currencyDisplay.
  351. intl_object.set_currency_display(currency_display.as_string().utf8_string_view());
  352. // d. Set intlObj.[[CurrencySign]] to currencySign.
  353. intl_object.set_currency_sign(currency_sign.as_string().utf8_string_view());
  354. }
  355. // 13. If style is "unit", then
  356. if (intl_object.style() == Unicode::NumberFormatStyle::Unit) {
  357. // a. Set intlObj.[[Unit]] to unit.
  358. intl_object.set_unit(unit.as_string().utf8_string());
  359. // b. Set intlObj.[[UnitDisplay]] to unitDisplay.
  360. intl_object.set_unit_display(unit_display.as_string().utf8_string_view());
  361. }
  362. // 14. Return UNUSED.
  363. return {};
  364. }
  365. // 15.2.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.numberformat.supportedlocalesof
  366. JS_DEFINE_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of)
  367. {
  368. auto locales = vm.argument(0);
  369. auto options = vm.argument(1);
  370. // 1. Let availableLocales be %NumberFormat%.[[AvailableLocales]].
  371. // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
  372. auto requested_locales = TRY(canonicalize_locale_list(vm, locales));
  373. // 3. Return ? FilterLocales(availableLocales, requestedLocales, options).
  374. return TRY(filter_locales(vm, requested_locales, options));
  375. }
  376. }