diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-09-11 07:50:54 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-12 12:57:17 +0100 |
commit | 094c390fb1befb8858beae1ab4450b14dd221b72 (patch) | |
tree | a8469bbae97149faa3acf238dacc37b3032824a5 /Userland/Libraries | |
parent | 6ae1a805837b41f2bb083da4d35b3ffa039b8e02 (diff) | |
download | serenity-094c390fb1befb8858beae1ab4450b14dd221b72.zip |
LibJS: Move CanonicalCodeForDisplayNames to Intl.DisplayNames
Intl.DisplayNames was the first Intl object implemented, and at that
point all AOs were just put into the main Intl AO header. But AOs that
belong to specific objects belong in that object's header. So this moves
CanonicalCodeForDisplayNames to the Intl.DisplayNames header.
Diffstat (limited to 'Userland/Libraries')
5 files changed, 70 insertions, 68 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp index f71d1d1cf7..cb55c4bccd 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.cpp @@ -765,70 +765,4 @@ Vector<PatternPartition> partition_pattern(StringView pattern) return result; } -// 12.1.1 CanonicalCodeForDisplayNames ( type, code ), https://tc39.es/ecma402/#sec-canonicalcodefordisplaynames -Value canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code) -{ - auto& vm = global_object.vm(); - - // 1. If type is "language", then - if (type == DisplayNames::Type::Language) { - // a. If code does not match the unicode_language_id production, throw a RangeError exception. - if (!Unicode::parse_unicode_language_id(code).has_value()) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "language"sv); - return {}; - } - - // b. If IsStructurallyValidLanguageTag(code) is false, throw a RangeError exception. - auto locale_id = is_structurally_valid_language_tag(code); - if (!locale_id.has_value()) { - vm.throw_exception<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, code); - return {}; - } - - // c. Set code to CanonicalizeUnicodeLocaleId(code). - // d. Return code. - auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id); - return js_string(vm, move(canonicalized_tag)); - } - - // 2. If type is "region", then - if (type == DisplayNames::Type::Region) { - // a. If code does not match the unicode_region_subtag production, throw a RangeError exception. - if (!Unicode::is_unicode_region_subtag(code)) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "region"sv); - return {}; - } - - // b. Let code be the result of mapping code to upper case as described in 6.1. - // c. Return code. - return js_string(vm, code.to_uppercase_string()); - } - - // 3. If type is "script", then - if (type == DisplayNames::Type::Script) { - // a. If code does not match the unicode_script_subtag production, throw a RangeError exception. - if (!Unicode::is_unicode_script_subtag(code)) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "script"sv); - return {}; - } - - // b. Let code be the result of mapping the first character in code to upper case, and mapping the second, third, and fourth character in code to lower case, as described in 6.1. - // c. Return code. - return js_string(vm, code.to_titlecase_string()); - } - - // 4. Assert: type is "currency". - VERIFY(type == DisplayNames::Type::Currency); - - // 5. If ! IsWellFormedCurrencyCode(code) is false, throw a RangeError exception. - if (!is_well_formed_currency_code(code)) { - vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "currency"sv); - return {}; - } - - // 6. Let code be the result of mapping code to upper case as described in 6.1. - // 7. Return code. - return js_string(vm, code.to_uppercase_string()); -} - } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h index 16b370db55..3602b1ca64 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/AbstractOperations.h @@ -50,6 +50,5 @@ Optional<int> get_number_option(GlobalObject& global_object, Object& options, Pr Vector<PatternPartition> partition_pattern(StringView pattern); String insert_unicode_extension_and_canonicalize(Unicode::LocaleID locale_id, Unicode::LocaleExtension extension); LocaleResult resolve_locale(Vector<String> const& requested_locales, LocaleOptions const& options, Vector<StringView> const& relevant_extension_keys); -Value canonical_code_for_display_names(GlobalObject&, DisplayNames::Type type, StringView code); } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp index 2f0d15c3f2..876d00d271 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.cpp @@ -5,7 +5,9 @@ */ #include <LibJS/Runtime/GlobalObject.h> +#include <LibJS/Runtime/Intl/AbstractOperations.h> #include <LibJS/Runtime/Intl/DisplayNames.h> +#include <LibUnicode/Locale.h> namespace JS::Intl { @@ -96,4 +98,70 @@ StringView DisplayNames::fallback_string() const } } +// 12.1.1 CanonicalCodeForDisplayNames ( type, code ), https://tc39.es/ecma402/#sec-canonicalcodefordisplaynames +Value canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code) +{ + auto& vm = global_object.vm(); + + // 1. If type is "language", then + if (type == DisplayNames::Type::Language) { + // a. If code does not match the unicode_language_id production, throw a RangeError exception. + if (!Unicode::parse_unicode_language_id(code).has_value()) { + vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "language"sv); + return {}; + } + + // b. If IsStructurallyValidLanguageTag(code) is false, throw a RangeError exception. + auto locale_id = is_structurally_valid_language_tag(code); + if (!locale_id.has_value()) { + vm.throw_exception<RangeError>(global_object, ErrorType::IntlInvalidLanguageTag, code); + return {}; + } + + // c. Set code to CanonicalizeUnicodeLocaleId(code). + // d. Return code. + auto canonicalized_tag = JS::Intl::canonicalize_unicode_locale_id(*locale_id); + return js_string(vm, move(canonicalized_tag)); + } + + // 2. If type is "region", then + if (type == DisplayNames::Type::Region) { + // a. If code does not match the unicode_region_subtag production, throw a RangeError exception. + if (!Unicode::is_unicode_region_subtag(code)) { + vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "region"sv); + return {}; + } + + // b. Let code be the result of mapping code to upper case as described in 6.1. + // c. Return code. + return js_string(vm, code.to_uppercase_string()); + } + + // 3. If type is "script", then + if (type == DisplayNames::Type::Script) { + // a. If code does not match the unicode_script_subtag production, throw a RangeError exception. + if (!Unicode::is_unicode_script_subtag(code)) { + vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "script"sv); + return {}; + } + + // b. Let code be the result of mapping the first character in code to upper case, and mapping the second, third, and fourth character in code to lower case, as described in 6.1. + // c. Return code. + return js_string(vm, code.to_titlecase_string()); + } + + // 4. Assert: type is "currency". + VERIFY(type == DisplayNames::Type::Currency); + + // 5. If ! IsWellFormedCurrencyCode(code) is false, throw a RangeError exception. + if (!is_well_formed_currency_code(code)) { + vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, code, "currency"sv); + return {}; + } + + // 6. Let code be the result of mapping code to upper case as described in 6.1. + // 7. Return code. + return js_string(vm, code.to_uppercase_string()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h index 874c135450..17fef5af76 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNames.h @@ -62,4 +62,6 @@ private: Fallback m_fallback { Fallback::Invalid }; // [[Fallback]] }; +Value canonical_code_for_display_names(GlobalObject& global_object, DisplayNames::Type type, StringView code); + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp index 0d4a58d618..320bcb126a 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp @@ -6,7 +6,6 @@ #include <AK/TypeCasts.h> #include <LibJS/Runtime/GlobalObject.h> -#include <LibJS/Runtime/Intl/AbstractOperations.h> #include <LibJS/Runtime/Intl/DisplayNames.h> #include <LibJS/Runtime/Intl/DisplayNamesPrototype.h> #include <LibUnicode/Locale.h> |