LibJS: Implement Intl.Locale.prototype.maximize

This commit is contained in:
Timothy Flynn 2021-09-02 19:03:41 -04:00 committed by Linus Groh
parent 03d7f01e0c
commit 0b1f5118d5
Notes: sideshowbarker 2024-07-18 04:47:22 +09:00
4 changed files with 55 additions and 0 deletions

View File

@ -281,6 +281,7 @@ namespace JS {
P(log10) \
P(map) \
P(max) \
P(maximize) \
P(mergeFields) \
P(message) \
P(microsecond) \

View File

@ -41,6 +41,7 @@ void LocalePrototype::initialize(GlobalObject& global_object)
auto& vm = this->vm();
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.maximize, maximize, 0, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
// 14.3.2 Intl.Locale.prototype[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.Locale.prototype-@@tostringtag
@ -58,6 +59,26 @@ void LocalePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.region, region, {}, Attribute::Configurable);
}
// 14.3.3 Intl.Locale.prototype.maximize ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.maximize
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::maximize)
{
// 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object);
if (!locale_object)
return {};
auto locale = Unicode::parse_unicode_locale_id(locale_object->locale());
VERIFY(locale.has_value());
// 3. Let maximal be the result of the Add Likely Subtags algorithm applied to loc.[[Locale]]. If an error is signaled, set maximal to loc.[[Locale]].
if (auto maximal = Unicode::add_likely_subtags(locale->language_id); maximal.has_value())
locale->language_id = maximal.release_value();
// 4. Return ! Construct(%Locale%, maximal).
return Locale::create(global_object, *locale);
}
// 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::to_string)
{

View File

@ -19,6 +19,7 @@ public:
virtual ~LocalePrototype() override = default;
private:
JS_DECLARE_NATIVE_FUNCTION(maximize);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_GETTER(base_name);

View File

@ -0,0 +1,32 @@
test("length is 0", () => {
expect(Intl.Locale.prototype.maximize).toHaveLength(0);
});
test("normal behavior", () => {
expect(new Intl.Locale("en").maximize().toString()).toBe("en-Latn-US");
expect(new Intl.Locale("en-Latn").maximize().toString()).toBe("en-Latn-US");
expect(new Intl.Locale("en-Shaw").maximize().toString()).toBe("en-Shaw-GB");
expect(new Intl.Locale("en-Arab").maximize().toString()).toBe("en-Arab-US");
expect(new Intl.Locale("en-US").maximize().toString()).toBe("en-Latn-US");
expect(new Intl.Locale("en-GB").maximize().toString()).toBe("en-Latn-GB");
expect(new Intl.Locale("en-FR").maximize().toString()).toBe("en-Latn-FR");
expect(new Intl.Locale("it-Kana-CA").maximize().toString()).toBe("it-Kana-CA");
expect(new Intl.Locale("und").maximize().toString()).toBe("en-Latn-US");
expect(new Intl.Locale("und-Thai").maximize().toString()).toBe("th-Thai-TH");
expect(new Intl.Locale("und-419").maximize().toString()).toBe("es-Latn-419");
expect(new Intl.Locale("und-150").maximize().toString()).toBe("ru-Cyrl-RU");
expect(new Intl.Locale("und-AT").maximize().toString()).toBe("de-Latn-AT");
expect(new Intl.Locale("und-Cyrl-RO").maximize().toString()).toBe("bg-Cyrl-RO");
expect(new Intl.Locale("und-AQ").maximize().toString()).toBe("und-Latn-AQ");
});
test("keywords are preserved", () => {
expect(new Intl.Locale("en-u-ca-abc").maximize().toString()).toBe("en-Latn-US-u-ca-abc");
expect(new Intl.Locale("en", { calendar: "abc" }).maximize().toString()).toBe(
"en-Latn-US-u-ca-abc"
);
});