summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-03-15 10:58:47 -0400
committerAndreas Kling <kling@serenityos.org>2022-03-15 17:30:58 +0100
commit812d3a7ef8dc331a312b0ecd56ffd39709dae300 (patch)
treeb0ac06720750ec7124ee83be82cba362ce42ef69
parentd6868d1e9deb7c376c87b750c854f1088c125421 (diff)
downloadserenity-812d3a7ef8dc331a312b0ecd56ffd39709dae300.zip
LibJS: Reorganize spec steps for Intl.NumberFormat
This is an editorial change in the Intl spec: https://github.com/tc39/ecma402/commit/110cb1f
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp337
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h7
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp323
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.h7
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp12
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp1
7 files changed, 347 insertions, 344 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
index 46f10f7512..894c578fde 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp
@@ -333,240 +333,7 @@ static ALWAYS_INLINE String number_to_string(Value number)
return number.as_bigint().big_integer().to_base(10);
}
-// 15.1.1 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/ecma402/#sec-setnfdigitoptions
-ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation)
-{
- auto& vm = global_object.vm();
-
- // 1. Let mnid be ? GetNumberOption(options, "minimumIntegerDigits,", 1, 21, 1).
- auto min_integer_digits = TRY(get_number_option(global_object, options, vm.names.minimumIntegerDigits, 1, 21, 1));
-
- // 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);
-
- // 7. If mnsd is not undefined or mxsd is not undefined, then
- // a. Let hasSd be true.
- // 8. Else,
- // a. Let hasSd be false.
- bool has_significant_digits = !min_significant_digits.is_undefined() || !max_significant_digits.is_undefined();
-
- // 9. If mnfd is not undefined or mxfd is not undefined, then
- // a. Let hasFd be true.
- // 10. Else,
- // a. Let hasFd be false.
- bool has_fraction_digits = !min_fraction_digits.is_undefined() || !max_fraction_digits.is_undefined();
-
- // 11. Let needSd be hasSd.
- bool need_significant_digits = has_significant_digits;
-
- // 12. If hasSd is true, or hasFd is false and notation is "compact", then
- // a. Let needFd be false.
- // 13. Else,
- // a. Let needFd be true.
- bool need_fraction_digits = !has_significant_digits && (has_fraction_digits || (notation != NumberFormat::Notation::Compact));
-
- // 14. If needSd is true, then
- if (need_significant_digits) {
- // a. Assert: hasSd is true.
- VERIFY(has_significant_digits);
-
- // b. Set mnsd to ? DefaultNumberOption(mnsd, 1, 21, 1).
- auto min_digits = TRY(default_number_option(global_object, min_significant_digits, 1, 21, 1));
-
- // c. Set mxsd to ? DefaultNumberOption(mxsd, mnsd, 21, 21).
- auto max_digits = TRY(default_number_option(global_object, max_significant_digits, *min_digits, 21, 21));
-
- // d. Set intlObj.[[MinimumSignificantDigits]] to mnsd.
- intl_object.set_min_significant_digits(*min_digits);
-
- // e. Set intlObj.[[MaximumSignificantDigits]] to mxsd.
- intl_object.set_max_significant_digits(*max_digits);
- }
-
- // 15. If needFd is true, then
- if (need_fraction_digits) {
- // a. If hasFd is true, then
- if (has_fraction_digits) {
- // i. Set mnfd to ? DefaultNumberOption(mnfd, 0, 20, undefined).
- auto min_digits = TRY(default_number_option(global_object, min_fraction_digits, 0, 20, {}));
-
- // ii. Set mxfd to ? DefaultNumberOption(mxfd, 0, 20, undefined).
- auto max_digits = TRY(default_number_option(global_object, max_fraction_digits, 0, 20, {}));
-
- // 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)
- return vm.throw_completion<RangeError>(global_object, ErrorType::IntlMinimumExceedsMaximum, *min_digits, *max_digits);
-
- // 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);
- }
- }
-
- // 16. If needSd is false and needFd is false, then
- if (!need_significant_digits && !need_fraction_digits) {
- // a. Set intlObj.[[RoundingType]] to compactRounding.
- intl_object.set_rounding_type(NumberFormatBase::RoundingType::CompactRounding);
- }
- // 17. Else if hasSd is true, then
- else if (has_significant_digits) {
- // a. Set intlObj.[[RoundingType]] to significantDigits.
- intl_object.set_rounding_type(NumberFormatBase::RoundingType::SignificantDigits);
- }
- // 18. Else,
- else {
- // a. Set intlObj.[[RoundingType]] to fractionDigits.
- intl_object.set_rounding_type(NumberFormatBase::RoundingType::FractionDigits);
- }
-
- return {};
-}
-
-// 15.1.2 InitializeNumberFormat ( numberFormat, locales, options ), https://tc39.es/ecma402/#sec-initializenumberformat
-ThrowCompletionOr<NumberFormat*> initialize_number_format(GlobalObject& global_object, NumberFormat& number_format, Value locales_value, Value options_value)
-{
- auto& vm = global_object.vm();
-
- // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
- auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
-
- // 2. Set options to ? CoerceOptionsToObject(options).
- auto* options = TRY(coerce_options_to_object(global_object, options_value));
-
- // 3. Let opt be a new Record.
- LocaleOptions opt {};
-
- // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
- auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
-
- // 5. Set opt.[[localeMatcher]] to matcher.
- opt.locale_matcher = matcher;
-
- // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
- auto numbering_system = TRY(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {}));
-
- // 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.
- if (!Unicode::is_type_identifier(numbering_system.as_string().string()))
- return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
-
- // 8. Set opt.[[nu]] to numberingSystem.
- opt.nu = numbering_system.as_string().string();
- }
-
- // 9. Let localeData be %NumberFormat%.[[LocaleData]].
- // 10. Let r be ResolveLocale(%NumberFormat%.[[AvailableLocales]], requestedLocales, opt, %NumberFormat%.[[RelevantExtensionKeys]], localeData).
- auto result = resolve_locale(requested_locales, opt, NumberFormat::relevant_extension_keys());
-
- // 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).
- TRY(set_number_format_unit_options(global_object, number_format, *options));
-
- // 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;
- }
-
- // 18. Let notation be ? GetOption(options, "notation", "string", « "standard", "scientific", "engineering", "compact" », "standard").
- auto notation = TRY(get_option(global_object, *options, vm.names.notation, Value::Type::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv));
-
- // 19. Set numberFormat.[[Notation]] to notation.
- number_format.set_notation(notation.as_string().string());
-
- // 20. Perform ? SetNumberFormatDigitOptions(numberFormat, options, mnfdDefault, mxfdDefault, notation).
- TRY(set_number_format_digit_options(global_object, number_format, *options, default_min_fraction_digits, default_max_fraction_digits, number_format.notation()));
-
- // 21. Let compactDisplay be ? GetOption(options, "compactDisplay", "string", « "short", "long" », "short").
- auto compact_display = TRY(get_option(global_object, *options, vm.names.compactDisplay, Value::Type::String, { "short"sv, "long"sv }, "short"sv));
-
- // 22. If notation is "compact", then
- if (number_format.notation() == NumberFormat::Notation::Compact) {
- // a. Set numberFormat.[[CompactDisplay]] to compactDisplay.
- number_format.set_compact_display(compact_display.as_string().string());
- }
-
- // 23. Let useGrouping be ? GetOption(options, "useGrouping", "boolean", undefined, true).
- auto use_grouping = TRY(get_option(global_object, *options, vm.names.useGrouping, Value::Type::Boolean, {}, true));
-
- // 24. Set numberFormat.[[UseGrouping]] to useGrouping.
- number_format.set_use_grouping(use_grouping.as_bool());
-
- // 25. Let signDisplay be ? GetOption(options, "signDisplay", "string", « "auto", "never", "always", "exceptZero" », "auto").
- auto sign_display = TRY(get_option(global_object, *options, vm.names.signDisplay, Value::Type::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv }, "auto"sv));
-
- // 26. Set numberFormat.[[SignDisplay]] to signDisplay.
- number_format.set_sign_display(sign_display.as_string().string());
-
- // 27. Return numberFormat.
- return &number_format;
-}
-
-// 15.1.3 CurrencyDigits ( currency ), https://tc39.es/ecma402/#sec-currencydigits
+// 15.5.1 CurrencyDigits ( currency ), https://tc39.es/ecma402/#sec-currencydigits
int currency_digits(StringView currency)
{
// 1. If the ISO 4217 currency and funds code list contains currency as an alphabetic code, return the minor
@@ -576,7 +343,7 @@ int currency_digits(StringView currency)
return 2;
}
-// 15.1.5 FormatNumericToString ( intlObject, x ), https://tc39.es/ecma402/#sec-formatnumberstring
+// 15.5.3 FormatNumericToString ( intlObject, x ), https://tc39.es/ecma402/#sec-formatnumberstring
FormatResult format_numeric_to_string(GlobalObject& global_object, NumberFormatBase& intl_object, Value number)
{
// 1. If x < 0 or x is -0𝔽, let isNegative be true; else let isNegative be false.
@@ -652,7 +419,7 @@ FormatResult format_numeric_to_string(GlobalObject& global_object, NumberFormatB
return { move(string), number };
}
-// 15.1.6 PartitionNumberPattern ( numberFormat, x ), https://tc39.es/ecma402/#sec-partitionnumberpattern
+// 15.5.4 PartitionNumberPattern ( numberFormat, x ), https://tc39.es/ecma402/#sec-partitionnumberpattern
Vector<PatternPartition> partition_number_pattern(GlobalObject& global_object, NumberFormat& number_format, Value number)
{
// 1. Let exponent be 0.
@@ -826,7 +593,7 @@ static Vector<StringView> separate_integer_into_groups(Unicode::NumberGroupings
return groups;
}
-// 15.1.7 PartitionNotationSubPattern ( numberFormat, x, n, exponent ), https://tc39.es/ecma402/#sec-partitionnotationsubpattern
+// 15.5.5 PartitionNotationSubPattern ( numberFormat, x, n, exponent ), https://tc39.es/ecma402/#sec-partitionnotationsubpattern
Vector<PatternPartition> partition_notation_sub_pattern(GlobalObject& global_object, NumberFormat& number_format, Value number, String formatted_string, int exponent)
{
// 1. Let result be a new empty List.
@@ -868,8 +635,8 @@ Vector<PatternPartition> partition_notation_sub_pattern(GlobalObject& global_obj
}
// iii. Else if p is equal to "number", then
else if (part == "number"sv) {
- // 1. If the numberFormat.[[NumberingSystem]] matches one of the values in the "Numbering System" column of Table 10 below, then
- // a. Let digits be a List whose 10 String valued elements are the UTF-16 string representations of the 10 digits specified in the "Digits" column of the matching row in Table 10.
+ // 1. If the numberFormat.[[NumberingSystem]] matches one of the values in the "Numbering System" column of Table 12 below, then
+ // a. Let digits be a List whose 10 String valued elements are the UTF-16 string representations of the 10 digits specified in the "Digits" column of the matching row in Table 12.
// b. Replace each digit in n with the value of digits[digit].
// 2. Else use an implementation dependent algorithm to map n to the appropriate representation of n in the given numbering system.
formatted_string = Unicode::replace_digits_for_number_system(number_format.numbering_system(), formatted_string);
@@ -1013,7 +780,7 @@ Vector<PatternPartition> partition_notation_sub_pattern(GlobalObject& global_obj
return result;
}
-// 15.1.8 FormatNumeric ( numberFormat, x ), https://tc39.es/ecma402/#sec-formatnumber
+// 15.5.6 FormatNumeric ( numberFormat, x ), https://tc39.es/ecma402/#sec-formatnumber
String format_numeric(GlobalObject& global_object, NumberFormat& number_format, Value number)
{
// 1. Let parts be ? PartitionNumberPattern(numberFormat, x).
@@ -1033,7 +800,7 @@ String format_numeric(GlobalObject& global_object, NumberFormat& number_format,
return result.build();
}
-// 15.1.9 FormatNumericToParts ( numberFormat, x ), https://tc39.es/ecma402/#sec-formatnumbertoparts
+// 15.5.7 FormatNumericToParts ( numberFormat, x ), https://tc39.es/ecma402/#sec-formatnumbertoparts
Array* format_numeric_to_parts(GlobalObject& global_object, NumberFormat& number_format, Value number)
{
auto& vm = global_object.vm();
@@ -1092,7 +859,7 @@ static String cut_trailing_zeroes(StringView string, int cut)
return string.to_string();
}
-// 15.1.10 ToRawPrecision ( x, minPrecision, maxPrecision ), https://tc39.es/ecma402/#sec-torawprecision
+// 15.5.8 ToRawPrecision ( x, minPrecision, maxPrecision ), https://tc39.es/ecma402/#sec-torawprecision
RawFormatResult to_raw_precision(GlobalObject& global_object, Value number, int min_precision, int max_precision)
{
RawFormatResult result {};
@@ -1197,7 +964,7 @@ RawFormatResult to_raw_precision(GlobalObject& global_object, Value number, int
return result;
}
-// 15.1.11 ToRawFixed ( x, minInteger, minFraction, maxFraction ), https://tc39.es/ecma402/#sec-torawfixed
+// 15.5.9 ToRawFixed ( x, minInteger, minFraction, maxFraction ), https://tc39.es/ecma402/#sec-torawfixed
// NOTE: The spec has a mistake here. The minInteger parameter is unused and is not provided by FormatNumericToString.
RawFormatResult to_raw_fixed(GlobalObject& global_object, Value number, int min_fraction, int max_fraction)
{
@@ -1259,83 +1026,7 @@ RawFormatResult to_raw_fixed(GlobalObject& global_object, Value number, int min_
return result;
}
-// 15.1.13 SetNumberFormatUnitOptions ( intlObj, options ), https://tc39.es/ecma402/#sec-setnumberformatunitoptions
-ThrowCompletionOr<void> set_number_format_unit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options)
-{
- auto& vm = global_object.vm();
-
- // 1. Assert: Type(intlObj) is Object.
- // 2. Assert: Type(options) is Object.
-
- // 3. Let style be ? GetOption(options, "style", "string", « "decimal", "percent", "currency", "unit" », "decimal").
- auto style = TRY(get_option(global_object, options, vm.names.style, Value::Type::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv));
-
- // 4. Set intlObj.[[Style]] to style.
- intl_object.set_style(style.as_string().string());
-
- // 5. Let currency be ? GetOption(options, "currency", "string", undefined, undefined).
- auto currency = TRY(get_option(global_object, options, vm.names.currency, Value::Type::String, {}, Empty {}));
-
- // 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)
- return vm.throw_completion<TypeError>(global_object, ErrorType::IntlOptionUndefined, "currency"sv, "style"sv, style);
- }
- // 7. Else,
- // a. If the result of IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
- else if (!is_well_formed_currency_code(currency.as_string().string()))
- return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, currency, "currency"sv);
-
- // 8. Let currencyDisplay be ? GetOption(options, "currencyDisplay", "string", « "code", "symbol", "narrowSymbol", "name" », "symbol").
- auto currency_display = TRY(get_option(global_object, options, vm.names.currencyDisplay, Value::Type::String, { "code"sv, "symbol"sv, "narrowSymbol"sv, "name"sv }, "symbol"sv));
-
- // 9. Let currencySign be ? GetOption(options, "currencySign", "string", « "standard", "accounting" », "standard").
- auto currency_sign = TRY(get_option(global_object, options, vm.names.currencySign, Value::Type::String, { "standard"sv, "accounting"sv }, "standard"sv));
-
- // 10. Let unit be ? GetOption(options, "unit", "string", undefined, undefined).
- auto unit = TRY(get_option(global_object, options, vm.names.unit, Value::Type::String, {}, Empty {}));
-
- // 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)
- return vm.throw_completion<TypeError>(global_object, ErrorType::IntlOptionUndefined, "unit"sv, "style"sv, style);
- }
- // 12. Else,
- // a. If the result of IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
- else if (!is_well_formed_unit_identifier(unit.as_string().string()))
- return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, unit, "unit"sv);
-
- // 13. Let unitDisplay be ? GetOption(options, "unitDisplay", "string", « "short", "narrow", "long" », "short").
- auto unit_display = TRY(get_option(global_object, options, vm.names.unitDisplay, Value::Type::String, { "short"sv, "narrow"sv, "long"sv }, "short"sv));
-
- // 14. If style is "currency", then
- if (intl_object.style() == NumberFormat::Style::Currency) {
- // a. Let currency be the result of converting currency to upper case as specified in 6.1.
- // b. Set intlObj.[[Currency]] to currency.
- intl_object.set_currency(currency.as_string().string().to_uppercase());
-
- // c. Set intlObj.[[CurrencyDisplay]] to currencyDisplay.
- intl_object.set_currency_display(currency_display.as_string().string());
-
- // d. Set intlObj.[[CurrencySign]] to currencySign.
- intl_object.set_currency_sign(currency_sign.as_string().string());
- }
-
- // 15. If style is "unit", then
- if (intl_object.style() == NumberFormat::Style::Unit) {
- // a. Set intlObj.[[Unit]] to unit.
- intl_object.set_unit(unit.as_string().string());
-
- // b. Set intlObj.[[UnitDisplay]] to unitDisplay.
- intl_object.set_unit_display(unit_display.as_string().string());
- }
-
- return {};
-}
-
-// 15.1.14 GetNumberFormatPattern ( numberFormat, x ), https://tc39.es/ecma402/#sec-getnumberformatpattern
+// 15.5.11 GetNumberFormatPattern ( numberFormat, x ), https://tc39.es/ecma402/#sec-getnumberformatpattern
Optional<Variant<StringView, String>> get_number_format_pattern(NumberFormat& number_format, Value number, Unicode::NumberFormat& found_pattern)
{
auto as_number = [&]() {
@@ -1505,7 +1196,7 @@ Optional<Variant<StringView, String>> get_number_format_pattern(NumberFormat& nu
return pattern;
}
-// 15.1.15 GetNotationSubPattern ( numberFormat, exponent ), https://tc39.es/ecma402/#sec-getnotationsubpattern
+// 15.5.12 GetNotationSubPattern ( numberFormat, exponent ), https://tc39.es/ecma402/#sec-getnotationsubpattern
Optional<StringView> get_notation_sub_pattern(NumberFormat& number_format, int exponent)
{
// 1. Let localeData be %NumberFormat%.[[LocaleData]].
@@ -1543,7 +1234,7 @@ Optional<StringView> get_notation_sub_pattern(NumberFormat& number_format, int e
return "{number}"sv;
}
-// 15.1.16 ComputeExponent ( numberFormat, x ), https://tc39.es/ecma402/#sec-computeexponent
+// 15.5.13 ComputeExponent ( numberFormat, x ), https://tc39.es/ecma402/#sec-computeexponent
int compute_exponent(GlobalObject& global_object, NumberFormat& number_format, Value number)
{
// 1. If x = 0, then
@@ -1589,7 +1280,7 @@ int compute_exponent(GlobalObject& global_object, NumberFormat& number_format, V
return compute_exponent_for_magnitude(number_format, magnitude + 1);
}
-// 15.1.17 ComputeExponentForMagnitude ( numberFormat, magnitude ), https://tc39.es/ecma402/#sec-computeexponentformagnitude
+// 15.5.14 ComputeExponentForMagnitude ( numberFormat, magnitude ), https://tc39.es/ecma402/#sec-computeexponentformagnitude
int compute_exponent_for_magnitude(NumberFormat& number_format, int magnitude)
{
// 1. Let notation be numberFormat.[[Notation]].
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
index 6b1e1958d8..171a24455c 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
+ * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -124,7 +124,7 @@ public:
static constexpr auto relevant_extension_keys()
{
- // 15.3.3 Internal slots, https://tc39.es/ecma402/#sec-intl.numberformat-internal-slots
+ // 15.2.3 Internal slots, https://tc39.es/ecma402/#sec-intl.numberformat-internal-slots
// The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
return AK::Array { "nu"sv };
}
@@ -220,8 +220,6 @@ struct RawFormatResult : public FormatResult {
int digits { 0 }; // [[IntegerDigitsCount]]
};
-ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation);
-ThrowCompletionOr<NumberFormat*> initialize_number_format(GlobalObject& global_object, NumberFormat& number_format, Value locales_value, Value options_value);
int currency_digits(StringView currency);
FormatResult format_numeric_to_string(GlobalObject& global_object, NumberFormatBase& intl_object, Value number);
Vector<PatternPartition> partition_number_pattern(GlobalObject& global_object, NumberFormat& number_format, Value number);
@@ -230,7 +228,6 @@ String format_numeric(GlobalObject& global_object, NumberFormat& number_format,
Array* format_numeric_to_parts(GlobalObject& global_object, NumberFormat& number_format, Value number);
RawFormatResult to_raw_precision(GlobalObject& global_object, Value number, int min_precision, int max_precision);
RawFormatResult to_raw_fixed(GlobalObject& global_object, Value number, int min_fraction, int max_fraction);
-ThrowCompletionOr<void> set_number_format_unit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options);
Optional<Variant<StringView, String>> get_number_format_pattern(NumberFormat& number_format, Value number, Unicode::NumberFormat& found_pattern);
Optional<StringView> get_notation_sub_pattern(NumberFormat& number_format, int exponent);
int compute_exponent(GlobalObject& global_object, NumberFormat& number_format, Value number);
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp
index f759c914df..44b28c30e9 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
+ * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -8,12 +8,12 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
-#include <LibJS/Runtime/Intl/NumberFormat.h>
#include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
+#include <LibUnicode/Locale.h>
namespace JS::Intl {
-// 15.2 The Intl.NumberFormat Constructor, https://tc39.es/ecma402/#sec-intl-numberformat-constructor
+// 15.1 The Intl.NumberFormat Constructor, https://tc39.es/ecma402/#sec-intl-numberformat-constructor
NumberFormatConstructor::NumberFormatConstructor(GlobalObject& global_object)
: NativeFunction(vm().names.NumberFormat.as_string(), *global_object.function_prototype())
{
@@ -25,7 +25,7 @@ void NumberFormatConstructor::initialize(GlobalObject& global_object)
auto& vm = this->vm();
- // 15.3.1 Intl.NumberFormat.prototype, https://tc39.es/ecma402/#sec-intl.numberformat.prototype
+ // 15.2.1 Intl.NumberFormat.prototype, https://tc39.es/ecma402/#sec-intl.numberformat.prototype
define_direct_property(vm.names.prototype, global_object.intl_number_format_prototype(), 0);
u8 attr = Attribute::Writable | Attribute::Configurable;
@@ -34,14 +34,14 @@ void NumberFormatConstructor::initialize(GlobalObject& global_object)
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
}
-// 15.2.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
+// 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
ThrowCompletionOr<Value> NumberFormatConstructor::call()
{
// 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
return TRY(construct(*this));
}
-// 15.2.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
+// 15.1.1 Intl.NumberFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.numberformat
ThrowCompletionOr<Object*> NumberFormatConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
@@ -64,7 +64,7 @@ ThrowCompletionOr<Object*> NumberFormatConstructor::construct(FunctionObject& ne
return number_format;
}
-// 15.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.numberformat.supportedlocalesof
+// 15.2.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.numberformat.supportedlocalesof
JS_DEFINE_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of)
{
auto locales = vm.argument(0);
@@ -79,4 +79,313 @@ JS_DEFINE_NATIVE_FUNCTION(NumberFormatConstructor::supported_locales_of)
return TRY(supported_locales(global_object, requested_locales, options));
}
+// 15.1.2 InitializeNumberFormat ( numberFormat, locales, options ), https://tc39.es/ecma402/#sec-initializenumberformat
+ThrowCompletionOr<NumberFormat*> initialize_number_format(GlobalObject& global_object, NumberFormat& number_format, Value locales_value, Value options_value)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
+ auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
+
+ // 2. Set options to ? CoerceOptionsToObject(options).
+ auto* options = TRY(coerce_options_to_object(global_object, options_value));
+
+ // 3. Let opt be a new Record.
+ LocaleOptions opt {};
+
+ // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
+ auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
+
+ // 5. Set opt.[[localeMatcher]] to matcher.
+ opt.locale_matcher = matcher;
+
+ // 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
+ auto numbering_system = TRY(get_option(global_object, *options, vm.names.numberingSystem, Value::Type::String, {}, Empty {}));
+
+ // 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.
+ if (!Unicode::is_type_identifier(numbering_system.as_string().string()))
+ return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
+
+ // 8. Set opt.[[nu]] to numberingSystem.
+ opt.nu = numbering_system.as_string().string();
+ }
+
+ // 9. Let localeData be %NumberFormat%.[[LocaleData]].
+ // 10. Let r be ResolveLocale(%NumberFormat%.[[AvailableLocales]], requestedLocales, opt, %NumberFormat%.[[RelevantExtensionKeys]], localeData).
+ auto result = resolve_locale(requested_locales, opt, NumberFormat::relevant_extension_keys());
+
+ // 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).
+ TRY(set_number_format_unit_options(global_object, number_format, *options));
+
+ // 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;
+ }
+
+ // 18. Let notation be ? GetOption(options, "notation", "string", « "standard", "scientific", "engineering", "compact" », "standard").
+ auto notation = TRY(get_option(global_object, *options, vm.names.notation, Value::Type::String, { "standard"sv, "scientific"sv, "engineering"sv, "compact"sv }, "standard"sv));
+
+ // 19. Set numberFormat.[[Notation]] to notation.
+ number_format.set_notation(notation.as_string().string());
+
+ // 20. Perform ? SetNumberFormatDigitOptions(numberFormat, options, mnfdDefault, mxfdDefault, notation).
+ TRY(set_number_format_digit_options(global_object, number_format, *options, default_min_fraction_digits, default_max_fraction_digits, number_format.notation()));
+
+ // 21. Let compactDisplay be ? GetOption(options, "compactDisplay", "string", « "short", "long" », "short").
+ auto compact_display = TRY(get_option(global_object, *options, vm.names.compactDisplay, Value::Type::String, { "short"sv, "long"sv }, "short"sv));
+
+ // 22. If notation is "compact", then
+ if (number_format.notation() == NumberFormat::Notation::Compact) {
+ // a. Set numberFormat.[[CompactDisplay]] to compactDisplay.
+ number_format.set_compact_display(compact_display.as_string().string());
+ }
+
+ // 23. Let useGrouping be ? GetOption(options, "useGrouping", "boolean", undefined, true).
+ auto use_grouping = TRY(get_option(global_object, *options, vm.names.useGrouping, Value::Type::Boolean, {}, true));
+
+ // 24. Set numberFormat.[[UseGrouping]] to useGrouping.
+ number_format.set_use_grouping(use_grouping.as_bool());
+
+ // 25. Let signDisplay be ? GetOption(options, "signDisplay", "string", « "auto", "never", "always", "exceptZero" », "auto").
+ auto sign_display = TRY(get_option(global_object, *options, vm.names.signDisplay, Value::Type::String, { "auto"sv, "never"sv, "always"sv, "exceptZero"sv }, "auto"sv));
+
+ // 26. Set numberFormat.[[SignDisplay]] to signDisplay.
+ number_format.set_sign_display(sign_display.as_string().string());
+
+ // 27. Return numberFormat.
+ return &number_format;
+}
+
+// 15.1.3 SetNumberFormatDigitOptions ( intlObj, options, mnfdDefault, mxfdDefault, notation ), https://tc39.es/ecma402/#sec-setnfdigitoptions
+ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Let mnid be ? GetNumberOption(options, "minimumIntegerDigits,", 1, 21, 1).
+ auto min_integer_digits = TRY(get_number_option(global_object, options, vm.names.minimumIntegerDigits, 1, 21, 1));
+
+ // 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);
+
+ // 7. If mnsd is not undefined or mxsd is not undefined, then
+ // a. Let hasSd be true.
+ // 8. Else,
+ // a. Let hasSd be false.
+ bool has_significant_digits = !min_significant_digits.is_undefined() || !max_significant_digits.is_undefined();
+
+ // 9. If mnfd is not undefined or mxfd is not undefined, then
+ // a. Let hasFd be true.
+ // 10. Else,
+ // a. Let hasFd be false.
+ bool has_fraction_digits = !min_fraction_digits.is_undefined() || !max_fraction_digits.is_undefined();
+
+ // 11. Let needSd be hasSd.
+ bool need_significant_digits = has_significant_digits;
+
+ // 12. If hasSd is true, or hasFd is false and notation is "compact", then
+ // a. Let needFd be false.
+ // 13. Else,
+ // a. Let needFd be true.
+ bool need_fraction_digits = !has_significant_digits && (has_fraction_digits || (notation != NumberFormat::Notation::Compact));
+
+ // 14. If needSd is true, then
+ if (need_significant_digits) {
+ // a. Assert: hasSd is true.
+ VERIFY(has_significant_digits);
+
+ // b. Set mnsd to ? DefaultNumberOption(mnsd, 1, 21, 1).
+ auto min_digits = TRY(default_number_option(global_object, min_significant_digits, 1, 21, 1));
+
+ // c. Set mxsd to ? DefaultNumberOption(mxsd, mnsd, 21, 21).
+ auto max_digits = TRY(default_number_option(global_object, max_significant_digits, *min_digits, 21, 21));
+
+ // d. Set intlObj.[[MinimumSignificantDigits]] to mnsd.
+ intl_object.set_min_significant_digits(*min_digits);
+
+ // e. Set intlObj.[[MaximumSignificantDigits]] to mxsd.
+ intl_object.set_max_significant_digits(*max_digits);
+ }
+
+ // 15. If needFd is true, then
+ if (need_fraction_digits) {
+ // a. If hasFd is true, then
+ if (has_fraction_digits) {
+ // i. Set mnfd to ? DefaultNumberOption(mnfd, 0, 20, undefined).
+ auto min_digits = TRY(default_number_option(global_object, min_fraction_digits, 0, 20, {}));
+
+ // ii. Set mxfd to ? DefaultNumberOption(mxfd, 0, 20, undefined).
+ auto max_digits = TRY(default_number_option(global_object, max_fraction_digits, 0, 20, {}));
+
+ // 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)
+ return vm.throw_completion<RangeError>(global_object, ErrorType::IntlMinimumExceedsMaximum, *min_digits, *max_digits);
+
+ // 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);
+ }
+ }
+
+ // 16. If needSd is false and needFd is false, then
+ if (!need_significant_digits && !need_fraction_digits) {
+ // a. Set intlObj.[[RoundingType]] to compactRounding.
+ intl_object.set_rounding_type(NumberFormatBase::RoundingType::CompactRounding);
+ }
+ // 17. Else if hasSd is true, then
+ else if (has_significant_digits) {
+ // a. Set intlObj.[[RoundingType]] to significantDigits.
+ intl_object.set_rounding_type(NumberFormatBase::RoundingType::SignificantDigits);
+ }
+ // 18. Else,
+ else {
+ // a. Set intlObj.[[RoundingType]] to fractionDigits.
+ intl_object.set_rounding_type(NumberFormatBase::RoundingType::FractionDigits);
+ }
+
+ return {};
+}
+
+// 15.1.4 SetNumberFormatUnitOptions ( intlObj, options ), https://tc39.es/ecma402/#sec-setnumberformatunitoptions
+ThrowCompletionOr<void> set_number_format_unit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Assert: Type(intlObj) is Object.
+ // 2. Assert: Type(options) is Object.
+
+ // 3. Let style be ? GetOption(options, "style", "string", « "decimal", "percent", "currency", "unit" », "decimal").
+ auto style = TRY(get_option(global_object, options, vm.names.style, Value::Type::String, { "decimal"sv, "percent"sv, "currency"sv, "unit"sv }, "decimal"sv));
+
+ // 4. Set intlObj.[[Style]] to style.
+ intl_object.set_style(style.as_string().string());
+
+ // 5. Let currency be ? GetOption(options, "currency", "string", undefined, undefined).
+ auto currency = TRY(get_option(global_object, options, vm.names.currency, Value::Type::String, {}, Empty {}));
+
+ // 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)
+ return vm.throw_completion<TypeError>(global_object, ErrorType::IntlOptionUndefined, "currency"sv, "style"sv, style);
+ }
+ // 7. Else,
+ // a. If the result of IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
+ else if (!is_well_formed_currency_code(currency.as_string().string()))
+ return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, currency, "currency"sv);
+
+ // 8. Let currencyDisplay be ? GetOption(options, "currencyDisplay", "string", « "code", "symbol", "narrowSymbol", "name" », "symbol").
+ auto currency_display = TRY(get_option(global_object, options, vm.names.currencyDisplay, Value::Type::String, { "code"sv, "symbol"sv, "narrowSymbol"sv, "name"sv }, "symbol"sv));
+
+ // 9. Let currencySign be ? GetOption(options, "currencySign", "string", « "standard", "accounting" », "standard").
+ auto currency_sign = TRY(get_option(global_object, options, vm.names.currencySign, Value::Type::String, { "standard"sv, "accounting"sv }, "standard"sv));
+
+ // 10. Let unit be ? GetOption(options, "unit", "string", undefined, undefined).
+ auto unit = TRY(get_option(global_object, options, vm.names.unit, Value::Type::String, {}, Empty {}));
+
+ // 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)
+ return vm.throw_completion<TypeError>(global_object, ErrorType::IntlOptionUndefined, "unit"sv, "style"sv, style);
+ }
+ // 12. Else,
+ // a. If the result of IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
+ else if (!is_well_formed_unit_identifier(unit.as_string().string()))
+ return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, unit, "unit"sv);
+
+ // 13. Let unitDisplay be ? GetOption(options, "unitDisplay", "string", « "short", "narrow", "long" », "short").
+ auto unit_display = TRY(get_option(global_object, options, vm.names.unitDisplay, Value::Type::String, { "short"sv, "narrow"sv, "long"sv }, "short"sv));
+
+ // 14. If style is "currency", then
+ if (intl_object.style() == NumberFormat::Style::Currency) {
+ // a. Let currency be the result of converting currency to upper case as specified in 6.1.
+ // b. Set intlObj.[[Currency]] to currency.
+ intl_object.set_currency(currency.as_string().string().to_uppercase());
+
+ // c. Set intlObj.[[CurrencyDisplay]] to currencyDisplay.
+ intl_object.set_currency_display(currency_display.as_string().string());
+
+ // d. Set intlObj.[[CurrencySign]] to currencySign.
+ intl_object.set_currency_sign(currency_sign.as_string().string());
+ }
+
+ // 15. If style is "unit", then
+ if (intl_object.style() == NumberFormat::Style::Unit) {
+ // a. Set intlObj.[[Unit]] to unit.
+ intl_object.set_unit(unit.as_string().string());
+
+ // b. Set intlObj.[[UnitDisplay]] to unitDisplay.
+ intl_object.set_unit_display(unit_display.as_string().string());
+ }
+
+ return {};
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.h
index 367110335c..9a4a82ad7f 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatConstructor.h
@@ -1,11 +1,12 @@
/*
- * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
+ * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
+#include <LibJS/Runtime/Intl/NumberFormat.h>
#include <LibJS/Runtime/NativeFunction.h>
namespace JS::Intl {
@@ -27,4 +28,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(supported_locales_of);
};
+ThrowCompletionOr<NumberFormat*> initialize_number_format(GlobalObject& global_object, NumberFormat& number_format, Value locales_value, Value options_value);
+ThrowCompletionOr<void> set_number_format_digit_options(GlobalObject& global_object, NumberFormatBase& intl_object, Object const& options, int default_min_fraction_digits, int default_max_fraction_digits, NumberFormat::Notation notation);
+ThrowCompletionOr<void> set_number_format_unit_options(GlobalObject& global_object, NumberFormat& intl_object, Object const& options);
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp
index f2d53cec36..0f3ef88933 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatFunction.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
+ * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -10,7 +10,7 @@
namespace JS::Intl {
-// 15.1.4 Number Format Functions, https://tc39.es/ecma402/#sec-number-format-functions
+// 15.5.2 Number Format Functions, https://tc39.es/ecma402/#sec-number-format-functions
NumberFormatFunction* NumberFormatFunction::create(GlobalObject& global_object, NumberFormat& number_format)
{
return global_object.heap().allocate<NumberFormatFunction>(global_object, number_format, *global_object.function_prototype());
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp
index 719a993478..191fd47c89 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormatPrototype.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
+ * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -13,7 +13,7 @@
namespace JS::Intl {
-// 15.4 Properties of the Intl.NumberFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-numberformat-prototype-object
+// 15.3 Properties of the Intl.NumberFormat Prototype Object, https://tc39.es/ecma402/#sec-properties-of-intl-numberformat-prototype-object
NumberFormatPrototype::NumberFormatPrototype(GlobalObject& global_object)
: PrototypeObject(*global_object.object_prototype())
{
@@ -25,7 +25,7 @@ void NumberFormatPrototype::initialize(GlobalObject& global_object)
auto& vm = this->vm();
- // 15.4.2 Intl.NumberFormat.prototype [ @@toStringTag ], https://tc39.es/ecma402/#sec-intl.numberformat.prototype-@@tostringtag
+ // 15.3.2 Intl.NumberFormat.prototype [ @@toStringTag ], https://tc39.es/ecma402/#sec-intl.numberformat.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.NumberFormat"), Attribute::Configurable);
define_native_accessor(vm.names.format, format, nullptr, Attribute::Configurable);
@@ -35,7 +35,7 @@ void NumberFormatPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr);
}
-// 15.4.3 get Intl.NumberFormat.prototype.format, https://tc39.es/ecma402/#sec-intl.numberformat.prototype.format
+// 15.3.3 get Intl.NumberFormat.prototype.format, https://tc39.es/ecma402/#sec-intl.numberformat.prototype.format
JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::format)
{
// 1. Let nf be the this value.
@@ -58,7 +58,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::format)
return number_format->bound_format();
}
-// 15.4.4 Intl.NumberFormat.prototype.formatToParts ( value ), https://tc39.es/ecma402/#sec-intl.numberformat.prototype.formattoparts
+// 15.3.4 Intl.NumberFormat.prototype.formatToParts ( value ), https://tc39.es/ecma402/#sec-intl.numberformat.prototype.formattoparts
JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::format_to_parts)
{
auto value = vm.argument(0);
@@ -75,7 +75,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::format_to_parts)
return format_numeric_to_parts(global_object, *number_format, value);
}
-// 15.4.5 Intl.NumberFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.numberformat.prototype.resolvedoptions
+// 15.3.5 Intl.NumberFormat.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.numberformat.prototype.resolvedoptions
JS_DEFINE_NATIVE_FUNCTION(NumberFormatPrototype::resolved_options)
{
// 1. Let nf be the this value.
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp b/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp
index d5be521d43..267a6b7e44 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp
@@ -6,6 +6,7 @@
#include <AK/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
+#include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
#include <LibJS/Runtime/Intl/PluralRules.h>
namespace JS::Intl {