Explorar el Código

LibJS: Implement Intl.Locale.prototype.baseName

Timothy Flynn hace 3 años
padre
commit
21b3c5edba

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

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

+ 20 - 0
Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp

@@ -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());
+}
+
 }

+ 2 - 0
Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h

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

+ 21 - 0
Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.baseName.js

@@ -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");
+    });
+});