1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:47:44 +00:00

LibJS: Implement Intl.Locale.prototype.baseName

This commit is contained in:
Timothy Flynn 2021-09-01 22:39:40 -04:00 committed by Linus Groh
parent 27fc3cfe75
commit 21b3c5edba
4 changed files with 44 additions and 0 deletions

View file

@ -71,6 +71,7 @@ namespace JS {
P(atan) \
P(atan2) \
P(atanh) \
P(baseName) \
P(big) \
P(bind) \
P(blank) \

View file

@ -8,6 +8,7 @@
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/Locale.h>
#include <LibJS/Runtime/Intl/LocalePrototype.h>
#include <LibUnicode/Locale.h>
namespace JS::Intl {
@ -44,6 +45,8 @@ void LocalePrototype::initialize(GlobalObject& global_object)
// 14.3.2 Intl.Locale.prototype[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.Locale.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.Locale"), Attribute::Configurable);
define_native_accessor(vm.names.baseName, base_name, {}, Attribute::Configurable);
}
// 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString
@ -59,4 +62,21 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::to_string)
return js_string(vm, locale_object->locale());
}
// 14.3.6 get Intl.Locale.prototype.baseName, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.baseName
JS_DEFINE_NATIVE_GETTER(LocalePrototype::base_name)
{
// 1. Let loc be the this value.
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
auto* locale_object = typed_this(global_object);
if (!locale_object)
return {};
// 3. Let locale be loc.[[Locale]].
auto locale = Unicode::parse_unicode_locale_id(locale_object->locale());
VERIFY(locale.has_value());
// 4. Return the substring of locale corresponding to the unicode_language_id production.
return js_string(vm, locale->language_id.to_string());
}
}

View file

@ -20,6 +20,8 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_GETTER(base_name);
};
}

View file

@ -0,0 +1,21 @@
describe("errors", () => {
test("called on non-Locale object", () => {
expect(() => {
Intl.Locale.prototype.baseName;
}).toThrowWithMessage(TypeError, "Not a Intl.Locale object");
});
});
describe("normal behavior", () => {
test("basic functionality", () => {
expect(new Intl.Locale("en").baseName).toBe("en");
expect(new Intl.Locale("en-Latn").baseName).toBe("en-Latn");
expect(new Intl.Locale("en-GB").baseName).toBe("en-GB");
expect(new Intl.Locale("en", { script: "Latn" }).baseName).toBe("en-Latn");
expect(new Intl.Locale("en", { region: "GB" }).baseName).toBe("en-GB");
expect(new Intl.Locale("en-u-ca-abc").baseName).toBe("en");
expect(new Intl.Locale("en", { calendar: "abc" }).baseName).toBe("en");
expect(new Intl.Locale("en-x-abcd").baseName).toBe("en");
});
});