| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |  * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-10 11:12:05 -04:00
										 |  |  | #include <LibJS/Runtime/Array.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-10 11:04:54 -04:00
										 |  |  | #include <LibJS/Runtime/Intl/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-02 12:11:30 -04:00
										 |  |  | #include <LibLocale/Locale.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS::Intl { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | // 15.1 The Intl.NumberFormat Constructor, https://tc39.es/ecma402/#sec-intl-numberformat-constructor
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  | NumberFormatConstructor::NumberFormatConstructor(Realm& realm) | 
					
						
							| 
									
										
										
										
											2022-09-14 19:10:27 -04:00
										 |  |  |     : NativeFunction(realm.vm().names.NumberFormat.as_string(), *realm.intrinsics().function_prototype()) | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  | void NumberFormatConstructor::initialize(Realm& realm) | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  |     NativeFunction::initialize(realm); | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     // 15.2.1 Intl.NumberFormat.prototype, https://tc39.es/ecma402/#sec-intl.numberformat.prototype
 | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  |     define_direct_property(vm.names.prototype, realm.intrinsics().intl_number_format_prototype(), 0); | 
					
						
							| 
									
										
										
										
											2021-09-10 11:12:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     u8 attr = Attribute::Writable | Attribute::Configurable; | 
					
						
							| 
									
										
										
										
											2022-08-22 21:47:35 +01:00
										 |  |  |     define_native_function(realm, vm.names.supportedLocalesOf, supported_locales_of, 1, attr); | 
					
						
							| 
									
										
										
										
											2021-09-10 11:12:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  |     define_direct_property(vm.names.length, Value(0), Attribute::Configurable); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | // 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | ThrowCompletionOr<Value> NumberFormatConstructor::call() | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     return TRY(construct(*this)); | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | // 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
 | 
					
						
							| 
									
										
										
										
											2022-12-14 19:18:10 +00:00
										 |  |  | ThrowCompletionOr<NonnullGCPtr<Object>> NumberFormatConstructor::construct(FunctionObject& new_target) | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 11:04:54 -04:00
										 |  |  |     auto locales = vm.argument(0); | 
					
						
							|  |  |  |     auto options = vm.argument(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  |     // 2. Let numberFormat be ? OrdinaryCreateFromConstructor(newTarget, "%NumberFormat.prototype%", « [[InitializedNumberFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[Unit]], [[UnitDisplay]], [[Currency]], [[CurrencyDisplay]], [[CurrencySign]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]], [[Notation]], [[CompactDisplay]], [[UseGrouping]], [[SignDisplay]], [[BoundFormat]] »).
 | 
					
						
							| 
									
										
										
										
											2022-12-14 18:34:32 +00:00
										 |  |  |     auto number_format = TRY(ordinary_create_from_constructor<NumberFormat>(vm, new_target, &Intrinsics::intl_number_format_prototype)); | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 11:04:54 -04:00
										 |  |  |     // 3. Perform ? InitializeNumberFormat(numberFormat, locales, options).
 | 
					
						
							| 
									
										
										
										
											2022-12-14 18:34:32 +00:00
										 |  |  |     TRY(initialize_number_format(vm, number_format, locales, options)); | 
					
						
							| 
									
										
										
										
											2021-09-10 11:04:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. If the implementation supports the normative optional constructor mode of 4.3 Note 1, then
 | 
					
						
							|  |  |  |     //     a. Let this be the this value.
 | 
					
						
							|  |  |  |     //     b. Return ? ChainNumberFormat(numberFormat, NewTarget, this).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  |     // 5. Return numberFormat.
 | 
					
						
							| 
									
										
										
										
											2022-12-14 19:18:10 +00:00
										 |  |  |     return number_format; | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | // 15.2.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.numberformat.supportedlocalesof
 | 
					
						
							| 
									
										
										
										
											2021-10-22 22:49:43 +01:00
										 |  |  | JS_DEFINE_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of) | 
					
						
							| 
									
										
										
										
											2021-09-10 11:12:05 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     auto locales = vm.argument(0); | 
					
						
							|  |  |  |     auto options = vm.argument(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Let availableLocales be %NumberFormat%.[[AvailableLocales]].
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     auto requested_locales = TRY(canonicalize_locale_list(vm, locales)); | 
					
						
							| 
									
										
										
										
											2021-09-10 11:12:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     return TRY(supported_locales(vm, requested_locales, options)); | 
					
						
							| 
									
										
										
										
											2021-09-10 11:12:05 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | // 15.1.2 InitializeNumberFormat ( numberFormat, locales, options ), https://tc39.es/ecma402/#sec-initializenumberformat
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | // 1.1.2 InitializeNumberFormat ( numberFormat, locales, options ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-initializenumberformat
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  | ThrowCompletionOr<NumberFormat*> initialize_number_format(VM& vm, NumberFormat& number_format, Value locales_value, Value options_value) | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     auto requested_locales = TRY(canonicalize_locale_list(vm, locales_value)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Set options to ? CoerceOptionsToObject(options).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     auto* options = TRY(coerce_options_to_object(vm, options_value)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Let opt be a new Record.
 | 
					
						
							|  |  |  |     LocaleOptions opt {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 4. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto matcher = TRY(get_option(vm, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 5. Set opt.[[localeMatcher]] to matcher.
 | 
					
						
							|  |  |  |     opt.locale_matcher = matcher; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", string, empty, undefined).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto numbering_system = TRY(get_option(vm, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {})); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 7. If numberingSystem is not undefined, then
 | 
					
						
							|  |  |  |     if (!numbering_system.is_undefined()) { | 
					
						
							|  |  |  |         // a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         if (!::Locale::is_type_identifier(TRY(numbering_system.as_string().utf8_string_view()))) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |             return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // 8. Set opt.[[nu]] to numberingSystem.
 | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |         opt.nu = TRY(numbering_system.as_string().utf8_string()); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 9. Let localeData be %NumberFormat%.[[LocaleData]].
 | 
					
						
							|  |  |  |     // 10. Let r be ResolveLocale(%NumberFormat%.[[AvailableLocales]], requestedLocales, opt, %NumberFormat%.[[RelevantExtensionKeys]], localeData).
 | 
					
						
							| 
									
										
										
										
											2023-01-19 11:39:05 -05:00
										 |  |  |     auto result = TRY(resolve_locale(vm, requested_locales, opt, NumberFormat::relevant_extension_keys())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 11. Set numberFormat.[[Locale]] to r.[[locale]].
 | 
					
						
							|  |  |  |     number_format.set_locale(move(result.locale)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 12. Set numberFormat.[[DataLocale]] to r.[[dataLocale]].
 | 
					
						
							|  |  |  |     number_format.set_data_locale(move(result.data_locale)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 13. Set numberFormat.[[NumberingSystem]] to r.[[nu]].
 | 
					
						
							|  |  |  |     if (result.nu.has_value()) | 
					
						
							|  |  |  |         number_format.set_numbering_system(result.nu.release_value()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 14. Perform ? SetNumberFormatUnitOptions(numberFormat, options).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     TRY(set_number_format_unit_options(vm, number_format, *options)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 15. Let style be numberFormat.[[Style]].
 | 
					
						
							|  |  |  |     auto style = number_format.style(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int default_min_fraction_digits = 0; | 
					
						
							|  |  |  |     int default_max_fraction_digits = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 16. If style is "currency", then
 | 
					
						
							|  |  |  |     if (style == NumberFormat::Style::Currency) { | 
					
						
							|  |  |  |         // a. Let currency be numberFormat.[[Currency]].
 | 
					
						
							|  |  |  |         auto const& currency = number_format.currency(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // b. Let cDigits be CurrencyDigits(currency).
 | 
					
						
							|  |  |  |         int digits = currency_digits(currency); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // c. Let mnfdDefault be cDigits.
 | 
					
						
							|  |  |  |         default_min_fraction_digits = digits; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // d. Let mxfdDefault be cDigits.
 | 
					
						
							|  |  |  |         default_max_fraction_digits = digits; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // 17. Else,
 | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         // a. Let mnfdDefault be 0.
 | 
					
						
							|  |  |  |         default_min_fraction_digits = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // b. If style is "percent", then
 | 
					
						
							|  |  |  |         //     i. Let mxfdDefault be 0.
 | 
					
						
							|  |  |  |         // c. Else,
 | 
					
						
							|  |  |  |         //     i. Let mxfdDefault be 3.
 | 
					
						
							|  |  |  |         default_max_fraction_digits = style == NumberFormat::Style::Percent ? 0 : 3; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 12:09:59 -05:00
										 |  |  |     // 18. Let roundingIncrement be ? GetNumberOption(options, "roundingIncrement", 1, 5000, 1).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     auto rounding_increment = TRY(get_number_option(vm, *options, vm.names.roundingIncrement, 1, 5000, 1)); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 12:09:59 -05:00
										 |  |  |     // 19. If roundingIncrement is not in « 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 », throw a RangeError exception.
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     static constexpr auto sanctioned_rounding_increments = AK::Array { 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!sanctioned_rounding_increments.span().contains_slow(*rounding_increment)) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |         return vm.throw_completion<RangeError>(ErrorType::IntlInvalidRoundingIncrement, *rounding_increment); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 12:09:59 -05:00
										 |  |  |     // 20. If roundingIncrement is not 1, set mxfdDefault to mnfdDefault.
 | 
					
						
							|  |  |  |     if (rounding_increment != 1) | 
					
						
							|  |  |  |         default_max_fraction_digits = default_min_fraction_digits; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 21. Let notation be ? GetOption(options, "notation", string, « "standard", "scientific", "engineering", "compact" », "standard").
 | 
					
						
							| 
									
										
										
										
											2022-11-28 12:09:59 -05:00
										 |  |  |     auto notation = TRY(get_option(vm, *options, vm.names.notation, OptionType::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 22. Set numberFormat.[[Notation]] to notation.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     number_format.set_notation(TRY(notation.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 12:09:59 -05:00
										 |  |  |     // 23. Perform ? SetNumberFormatDigitOptions(numberFormat, options, mnfdDefault, mxfdDefault, notation).
 | 
					
						
							|  |  |  |     TRY(set_number_format_digit_options(vm, number_format, *options, default_min_fraction_digits, default_max_fraction_digits, number_format.notation())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 24. If roundingIncrement is not 1, then
 | 
					
						
							|  |  |  |     if (rounding_increment != 1) { | 
					
						
							|  |  |  |         // a. If numberFormat.[[RoundingType]] is not fractionDigits, throw a TypeError exception.
 | 
					
						
							|  |  |  |         if (number_format.rounding_type() != NumberFormatBase::RoundingType::FractionDigits) | 
					
						
							|  |  |  |             return vm.throw_completion<TypeError>(ErrorType::IntlInvalidRoundingIncrementForRoundingType, *rounding_increment, number_format.rounding_type_string()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // b. If numberFormat.[[MaximumFractionDigits]] is not equal to numberFormat.[[MinimumFractionDigits]], throw a RangeError exception.
 | 
					
						
							|  |  |  |         if (number_format.max_fraction_digits() != number_format.min_fraction_digits()) | 
					
						
							|  |  |  |             return vm.throw_completion<RangeError>(ErrorType::IntlInvalidRoundingIncrementForFractionDigits, *rounding_increment); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 25. Set numberFormat.[[RoundingIncrement]] to roundingIncrement.
 | 
					
						
							|  |  |  |     number_format.set_rounding_increment(*rounding_increment); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 26. Let trailingZeroDisplay be ? GetOption(options, "trailingZeroDisplay", string, « "auto", "stripIfInteger" », "auto").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto trailing_zero_display = TRY(get_option(vm, *options, vm.names.trailingZeroDisplay, OptionType::String, { "auto"sv, "stripIfInteger"sv }, "auto"sv)); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 27. Set numberFormat.[[TrailingZeroDisplay]] to trailingZeroDisplay.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     number_format.set_trailing_zero_display(TRY(trailing_zero_display.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 28. Let compactDisplay be ? GetOption(options, "compactDisplay", string, « "short", "long" », "short").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto compact_display = TRY(get_option(vm, *options, vm.names.compactDisplay, OptionType::String, { "short"sv, "long"sv }, "short"sv)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 29. Let defaultUseGrouping be "auto".
 | 
					
						
							|  |  |  |     auto default_use_grouping = "auto"sv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 30. If notation is "compact", then
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     if (number_format.notation() == NumberFormat::Notation::Compact) { | 
					
						
							|  |  |  |         // a. Set numberFormat.[[CompactDisplay]] to compactDisplay.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         number_format.set_compact_display(TRY(compact_display.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // b. Set defaultUseGrouping to "min2".
 | 
					
						
							|  |  |  |         default_use_grouping = "min2"sv; | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 31. Let useGrouping be ? GetStringOrBooleanOption(options, "useGrouping", « "min2", "auto", "always" », "always", false, defaultUseGrouping).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     auto use_grouping = TRY(get_string_or_boolean_option(vm, *options, vm.names.useGrouping, { "min2"sv, "auto"sv, "always"sv }, "always"sv, false, default_use_grouping)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 32. Set numberFormat.[[UseGrouping]] to useGrouping.
 | 
					
						
							|  |  |  |     number_format.set_use_grouping(use_grouping); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 33. Let signDisplay be ? GetOption(options, "signDisplay", string, « "auto", "never", "always", "exceptZero, "negative" », "auto").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     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)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 34. Set numberFormat.[[SignDisplay]] to signDisplay.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     number_format.set_sign_display(TRY(sign_display.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 35. Let roundingMode be ? GetOption(options, "roundingMode", string, « "ceil", "floor", "expand", "trunc", "halfCeil", "halfFloor", "halfExpand", "halfTrunc", "halfEven" », "halfExpand").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     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)); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 36. Set numberFormat.[[RoundingMode]] to roundingMode.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     number_format.set_rounding_mode(TRY(rounding_mode.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 37. Return numberFormat.
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     return &number_format; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 15.1.3 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/ecma402/#sec-setnfdigitoptions
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | // 1.1.1 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-setnfdigitoptions
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  | 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, NumberFormat::Notation notation) | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let mnid be ? GetNumberOption(options, "minimumIntegerDigits,", 1, 21, 1).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |     auto min_integer_digits = TRY(get_number_option(vm, options, vm.names.minimumIntegerDigits, 1, 21, 1)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let mnfd be ? Get(options, "minimumFractionDigits").
 | 
					
						
							|  |  |  |     auto min_fraction_digits = TRY(options.get(vm.names.minimumFractionDigits)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Let mxfd be ? Get(options, "maximumFractionDigits").
 | 
					
						
							|  |  |  |     auto max_fraction_digits = TRY(options.get(vm.names.maximumFractionDigits)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. Let mnsd be ? Get(options, "minimumSignificantDigits").
 | 
					
						
							|  |  |  |     auto min_significant_digits = TRY(options.get(vm.names.minimumSignificantDigits)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. Let mxsd be ? Get(options, "maximumSignificantDigits").
 | 
					
						
							|  |  |  |     auto max_significant_digits = TRY(options.get(vm.names.maximumSignificantDigits)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. Set intlObj.[[MinimumIntegerDigits]] to mnid.
 | 
					
						
							|  |  |  |     intl_object.set_min_integer_digits(*min_integer_digits); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 7. Let roundingPriority be ? GetOption(options, "roundingPriority", string, « "auto", "morePrecision", "lessPrecision" », "auto").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto rounding_priority = TRY(get_option(vm, options, vm.names.roundingPriority, OptionType::String, { "auto"sv, "morePrecision"sv, "lessPrecision"sv }, "auto"sv)); | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 8. If mnsd is not undefined or mxsd is not undefined, then
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     //     a. Let hasSd be true.
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 9. Else,
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     //     a. Let hasSd be false.
 | 
					
						
							|  |  |  |     bool has_significant_digits = !min_significant_digits.is_undefined() || !max_significant_digits.is_undefined(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 10. If mnfd is not undefined or mxfd is not undefined, then
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     //     a. Let hasFd be true.
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 11. Else,
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     //     a. Let hasFd be false.
 | 
					
						
							|  |  |  |     bool has_fraction_digits = !min_fraction_digits.is_undefined() || !max_fraction_digits.is_undefined(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 12. Let needSd be true.
 | 
					
						
							|  |  |  |     bool need_significant_digits = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 13. Let needFd be true.
 | 
					
						
							|  |  |  |     bool need_fraction_digits = true; | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 14. If roundingPriority is "auto", then
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     if (TRY(rounding_priority.as_string().utf8_string_view()) == "auto"sv) { | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |         // a. Set needSd to hasSd.
 | 
					
						
							|  |  |  |         need_significant_digits = has_significant_digits; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // b. If hasSd is true, or hasFd is false and notation is "compact", then
 | 
					
						
							|  |  |  |         if (has_significant_digits || (!has_fraction_digits && notation == NumberFormat::Notation::Compact)) { | 
					
						
							|  |  |  |             // i. Set needFd to false.
 | 
					
						
							|  |  |  |             need_fraction_digits = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 15. If needSd is true, then
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     if (need_significant_digits) { | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |         // a. If hasSd is true, then
 | 
					
						
							|  |  |  |         if (has_significant_digits) { | 
					
						
							|  |  |  |             // i. Set mnsd to ? DefaultNumberOption(mnsd, 1, 21, 1).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |             auto min_digits = TRY(default_number_option(vm, min_significant_digits, 1, 21, 1)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |             // ii. Set mxsd to ? DefaultNumberOption(mxsd, mnsd, 21, 21).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |             auto max_digits = TRY(default_number_option(vm, max_significant_digits, *min_digits, 21, 21)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |             // iii. Set intlObj.[[MinimumSignificantDigits]] to mnsd.
 | 
					
						
							|  |  |  |             intl_object.set_min_significant_digits(*min_digits); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |             // iv. Set intlObj.[[MaximumSignificantDigits]] to mxsd.
 | 
					
						
							|  |  |  |             intl_object.set_max_significant_digits(*max_digits); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // b. Else,
 | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // i. Set intlObj.[[MinimumSignificantDigits]] to 1.
 | 
					
						
							|  |  |  |             intl_object.set_min_significant_digits(1); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |             // ii. Set intlObj.[[MaximumSignificantDigits]] to 21.
 | 
					
						
							|  |  |  |             intl_object.set_max_significant_digits(21); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 16. If needFd is true, then
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     if (need_fraction_digits) { | 
					
						
							|  |  |  |         // a. If hasFd is true, then
 | 
					
						
							|  |  |  |         if (has_fraction_digits) { | 
					
						
							|  |  |  |             // i. Set mnfd to ? DefaultNumberOption(mnfd, 0, 20, undefined).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |             auto min_digits = TRY(default_number_option(vm, min_fraction_digits, 0, 20, {})); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // ii. Set mxfd to ? DefaultNumberOption(mxfd, 0, 20, undefined).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  |             auto max_digits = TRY(default_number_option(vm, max_fraction_digits, 0, 20, {})); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // iii. If mnfd is undefined, set mnfd to min(mnfdDefault, mxfd).
 | 
					
						
							|  |  |  |             if (!min_digits.has_value()) | 
					
						
							|  |  |  |                 min_digits = min(default_min_fraction_digits, *max_digits); | 
					
						
							|  |  |  |             // iv. Else if mxfd is undefined, set mxfd to max(mxfdDefault, mnfd).
 | 
					
						
							|  |  |  |             else if (!max_digits.has_value()) | 
					
						
							|  |  |  |                 max_digits = max(default_max_fraction_digits, *min_digits); | 
					
						
							|  |  |  |             // v. Else if mnfd is greater than mxfd, throw a RangeError exception.
 | 
					
						
							|  |  |  |             else if (*min_digits > *max_digits) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |                 return vm.throw_completion<RangeError>(ErrorType::IntlMinimumExceedsMaximum, *min_digits, *max_digits); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // vi. Set intlObj.[[MinimumFractionDigits]] to mnfd.
 | 
					
						
							|  |  |  |             intl_object.set_min_fraction_digits(*min_digits); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // vii. Set intlObj.[[MaximumFractionDigits]] to mxfd.
 | 
					
						
							|  |  |  |             intl_object.set_max_fraction_digits(*max_digits); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // b. Else,
 | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // i. Set intlObj.[[MinimumFractionDigits]] to mnfdDefault.
 | 
					
						
							|  |  |  |             intl_object.set_min_fraction_digits(default_min_fraction_digits); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // ii. Set intlObj.[[MaximumFractionDigits]] to mxfdDefault.
 | 
					
						
							|  |  |  |             intl_object.set_max_fraction_digits(default_max_fraction_digits); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |     // 17. If needSd is true or needFd is true, then
 | 
					
						
							|  |  |  |     if (need_significant_digits || need_fraction_digits) { | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         auto rounding_priority_string = TRY(rounding_priority.as_string().utf8_string_view()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |         // a. If roundingPriority is "morePrecision", then
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         if (rounding_priority_string == "morePrecision"sv) { | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |             // i. Set intlObj.[[RoundingType]] to morePrecision.
 | 
					
						
							|  |  |  |             intl_object.set_rounding_type(NumberFormatBase::RoundingType::MorePrecision); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // b. Else if roundingPriority is "lessPrecision", then
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         else if (rounding_priority_string == "lessPrecision"sv) { | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |             // i. Set intlObj.[[RoundingType]] to lessPrecision.
 | 
					
						
							|  |  |  |             intl_object.set_rounding_type(NumberFormatBase::RoundingType::LessPrecision); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // c. Else if hasSd is true, then
 | 
					
						
							|  |  |  |         else if (has_significant_digits) { | 
					
						
							|  |  |  |             // i. Set intlObj.[[RoundingType]] to significantDigits.
 | 
					
						
							|  |  |  |             intl_object.set_rounding_type(NumberFormatBase::RoundingType::SignificantDigits); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // d. Else,
 | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // i. Set intlObj.[[RoundingType]] to fractionDigits.
 | 
					
						
							|  |  |  |             intl_object.set_rounding_type(NumberFormatBase::RoundingType::FractionDigits); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     // 18. Else,
 | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2022-07-12 13:18:23 -04:00
										 |  |  |         // a. Set intlObj.[[RoundingType]] to morePrecision.
 | 
					
						
							|  |  |  |         intl_object.set_rounding_type(NumberFormatBase::RoundingType::MorePrecision); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // b. Set intlObj.[[MinimumFractionDigits]] to 0.
 | 
					
						
							|  |  |  |         intl_object.set_min_fraction_digits(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // c. Set intlObj.[[MaximumFractionDigits]] to 0.
 | 
					
						
							|  |  |  |         intl_object.set_max_fraction_digits(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // d. Set intlObj.[[MinimumSignificantDigits]] to 1.
 | 
					
						
							|  |  |  |         intl_object.set_min_significant_digits(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // e. Set intlObj.[[MaximumSignificantDigits]] to 2.
 | 
					
						
							|  |  |  |         intl_object.set_max_significant_digits(2); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 15.1.4 SetNumberFormatUnitOptions ( intlObj, options ), https://tc39.es/ecma402/#sec-setnumberformatunitoptions
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:25:24 +01:00
										 |  |  | ThrowCompletionOr<void> set_number_format_unit_options(VM& vm, NumberFormat& intl_object, Object const& options) | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Assert: Type(intlObj) is Object.
 | 
					
						
							|  |  |  |     // 2. Assert: Type(options) is Object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 3. Let style be ? GetOption(options, "style", string, « "decimal", "percent", "currency", "unit" », "decimal").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto style = TRY(get_option(vm, options, vm.names.style, OptionType::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. Set intlObj.[[Style]] to style.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     intl_object.set_style(TRY(style.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 5. Let currency be ? GetOption(options, "currency", string, empty, undefined).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto currency = TRY(get_option(vm, options, vm.names.currency, OptionType::String, {}, Empty {})); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 6. If currency is undefined, then
 | 
					
						
							|  |  |  |     if (currency.is_undefined()) { | 
					
						
							|  |  |  |         // a. If style is "currency", throw a TypeError exception.
 | 
					
						
							|  |  |  |         if (intl_object.style() == NumberFormat::Style::Currency) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |             return vm.throw_completion<TypeError>(ErrorType::IntlOptionUndefined, "currency"sv, "style"sv, style); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     // 7. Else,
 | 
					
						
							| 
									
										
										
										
											2022-03-28 09:53:06 -04:00
										 |  |  |     //     a. If ! IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     else if (!is_well_formed_currency_code(TRY(currency.as_string().utf8_string_view()))) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |         return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, currency, "currency"sv); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 8. Let currencyDisplay be ? GetOption(options, "currencyDisplay", string, « "code", "symbol", "narrowSymbol", "name" », "symbol").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto currency_display = TRY(get_option(vm, options, vm.names.currencyDisplay, OptionType::String, { "code"sv, "symbol"sv, "narrowSymbol"sv, "name"sv }, "symbol"sv)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 9. Let currencySign be ? GetOption(options, "currencySign", string, « "standard", "accounting" », "standard").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto currency_sign = TRY(get_option(vm, options, vm.names.currencySign, OptionType::String, { "standard"sv, "accounting"sv }, "standard"sv)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 10. Let unit be ? GetOption(options, "unit", string, empty, undefined).
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto unit = TRY(get_option(vm, options, vm.names.unit, OptionType::String, {}, Empty {})); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 11. If unit is undefined, then
 | 
					
						
							|  |  |  |     if (unit.is_undefined()) { | 
					
						
							|  |  |  |         // a. If style is "unit", throw a TypeError exception.
 | 
					
						
							|  |  |  |         if (intl_object.style() == NumberFormat::Style::Unit) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |             return vm.throw_completion<TypeError>(ErrorType::IntlOptionUndefined, "unit"sv, "style"sv, style); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     // 12. Else,
 | 
					
						
							| 
									
										
										
										
											2022-03-28 09:53:06 -04:00
										 |  |  |     //     a. If ! IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |     else if (!is_well_formed_unit_identifier(TRY(unit.as_string().utf8_string_view()))) | 
					
						
							| 
									
										
										
										
											2022-08-16 20:33:17 +01:00
										 |  |  |         return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, unit, "unit"sv); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-12 10:22:37 -05:00
										 |  |  |     // 13. Let unitDisplay be ? GetOption(options, "unitDisplay", string, « "short", "narrow", "long" », "short").
 | 
					
						
							| 
									
										
										
										
											2022-08-20 08:52:42 +01:00
										 |  |  |     auto unit_display = TRY(get_option(vm, options, vm.names.unitDisplay, OptionType::String, { "short"sv, "narrow"sv, "long"sv }, "short"sv)); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 14. If style is "currency", then
 | 
					
						
							|  |  |  |     if (intl_object.style() == NumberFormat::Style::Currency) { | 
					
						
							| 
									
										
										
										
											2022-03-28 09:53:06 -04:00
										 |  |  |         // a. Set intlObj.[[Currency]] to the ASCII-uppercase of currency.
 | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |         intl_object.set_currency(TRY_OR_THROW_OOM(vm, TRY(currency.as_string().utf8_string()).to_uppercase())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // c. Set intlObj.[[CurrencyDisplay]] to currencyDisplay.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         intl_object.set_currency_display(TRY(currency_display.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // d. Set intlObj.[[CurrencySign]] to currencySign.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         intl_object.set_currency_sign(TRY(currency_sign.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 15. If style is "unit", then
 | 
					
						
							|  |  |  |     if (intl_object.style() == NumberFormat::Style::Unit) { | 
					
						
							|  |  |  |         // a. Set intlObj.[[Unit]] to unit.
 | 
					
						
							| 
									
										
										
										
											2023-01-19 13:13:57 -05:00
										 |  |  |         intl_object.set_unit(TRY(unit.as_string().utf8_string())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // b. Set intlObj.[[UnitDisplay]] to unitDisplay.
 | 
					
						
							| 
									
										
										
										
											2023-01-15 10:31:39 -05:00
										 |  |  |         intl_object.set_unit_display(TRY(unit_display.as_string().utf8_string_view())); | 
					
						
							| 
									
										
										
										
											2022-03-15 10:58:47 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 21:34:27 -04:00
										 |  |  | } |