From 4475f21e9e6adfa160d3b7431fb98c2d3a0cd51e Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 30 Jan 2023 09:56:54 -0500 Subject: [PATCH] LibJS: Allow locale approximately signs to be empty in Intl.NumberFormat This is a normative change in the Intl.NumberFormat V3 spec. See: https://github.com/tc39/proposal-intl-numberformat-v3/commit/23e69cf This isn't particularly testable because every locale in the CLDR has a non-empty "approximatelySign" field in cldr-numbers-modern. The issue for this change seems to be considering the "miscPatterns/approximately" field instead, which has different semantics. But as noted on the CLDR issue https://unicode-org.atlassian.net/browse/CLDR-14918, the ICU uses the "approximatelySign" field (as do our implementation). --- .../LibJS/Runtime/Intl/NumberFormat.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp index a908ce55b8..431f411928 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp @@ -1812,16 +1812,18 @@ ThrowCompletionOr> format_approximately(VM& v { // 1. Let i be an index into result, determined by an implementation-defined algorithm based on numberFormat and result. // 2. Let approximatelySign be an ILND String value used to signify that a number is approximate. - auto approximately_sign = TRY_OR_THROW_OOM(vm, ::Locale::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), ::Locale::NumericSymbol::ApproximatelySign)).value_or("~"sv); + auto approximately_sign = TRY_OR_THROW_OOM(vm, ::Locale::get_number_system_symbol(number_format.data_locale(), number_format.numbering_system(), ::Locale::NumericSymbol::ApproximatelySign)); - // 3. Insert a new Record { [[Type]]: "approximatelySign", [[Value]]: approximatelySign } at index i in result. - PatternPartitionWithSource partition; - partition.type = "approximatelySign"sv; - partition.value = TRY_OR_THROW_OOM(vm, String::from_utf8(approximately_sign)); + // 3. If approximatelySign is not empty, insert a new Record { [[Type]]: "approximatelySign", [[Value]]: approximatelySign } at index i in result. + if (approximately_sign.has_value() && !approximately_sign->is_empty()) { + PatternPartitionWithSource partition; + partition.type = "approximatelySign"sv; + partition.value = TRY_OR_THROW_OOM(vm, String::from_utf8(*approximately_sign)); - result.insert_before_matching(move(partition), [](auto const& part) { - return part.type.is_one_of("integer"sv, "decimal"sv, "plusSign"sv, "minusSign"sv, "percentSign"sv, "currency"sv); - }); + result.insert_before_matching(move(partition), [](auto const& part) { + return part.type.is_one_of("integer"sv, "decimal"sv, "plusSign"sv, "minusSign"sv, "percentSign"sv, "currency"sv); + }); + } // 4. Return result. return result;