LibJS: Implement Intl.Locale.prototype.minimize

This commit is contained in:
Timothy Flynn 2021-09-02 21:45:42 -04:00 committed by Linus Groh
parent a77f323dfb
commit 90971673c7
4 changed files with 57 additions and 0 deletions

View file

@ -289,6 +289,7 @@ namespace JS {
P(millisecond) \
P(milliseconds) \
P(min) \
P(minimize) \
P(minute) \
P(minutes) \
P(month) \

View file

@ -42,6 +42,7 @@ void LocalePrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.maximize, maximize, 0, attr);
define_native_function(vm.names.minimize, minimize, 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
@ -79,6 +80,26 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::maximize)
return Locale::create(global_object, *locale);
}
// 14.3.4 Intl.Locale.prototype.minimize ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.minimize
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::minimize)
{
// 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 minimal be the result of the Remove Likely Subtags algorithm applied to loc.[[Locale]]. If an error is signaled, set minimal to loc.[[Locale]].
if (auto minimal = Unicode::remove_likely_subtags(locale->language_id); minimal.has_value())
locale->language_id = minimal.release_value();
// 4. Return ! Construct(%Locale%, minimal).
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

@ -20,6 +20,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(maximize);
JS_DECLARE_NATIVE_FUNCTION(minimize);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_GETTER(base_name);

View file

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