Kaynağa Gözat

LibJS: Implement Intl.Locale.prototype.timeZones property

Timothy Flynn 3 yıl önce
ebeveyn
işleme
814f13bc2a

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

@@ -476,6 +476,7 @@ namespace JS {
     P(timeStyle)                             \
     P(timeZone)                              \
     P(timeZoneName)                          \
+    P(timeZones)                             \
     P(toDateString)                          \
     P(toExponential)                         \
     P(toFixed)                               \

+ 22 - 0
Userland/Libraries/LibJS/Runtime/Intl/Locale.cpp

@@ -4,9 +4,11 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <AK/QuickSort.h>
 #include <LibJS/Runtime/Array.h>
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Intl/Locale.h>
+#include <LibTimeZone/TimeZone.h>
 #include <LibUnicode/Locale.h>
 
 namespace JS::Intl {
@@ -145,4 +147,24 @@ Array* numbering_systems_of_locale(GlobalObject& global_object, Locale const& lo
     return create_array_from_list_or_restricted(global_object, move(list), move(restricted));
 }
 
+// 1.1.6 TimeZonesOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-time-zones-of-locale
+// NOTE: Our implementation takes a region rather than a Locale object to avoid needlessly parsing the locale twice.
+Array* time_zones_of_locale(GlobalObject& global_object, StringView region)
+{
+    auto& vm = global_object.vm();
+
+    // 1. Let locale be loc.[[Locale]].
+    // 2. Assert: locale matches the unicode_locale_id production.
+    // 3. Let region be the substring of locale corresponding to the unicode_region_subtag production of the unicode_language_id.
+
+    // 4. Let list be a List of unique canonical time zone identifiers, which must be String values indicating a canonical Zone name of the IANA Time Zone Database, ordered as if an Array of the same values had been sorted using %Array.prototype.sort% using undefined as comparefn, of those in common use in region. If no time zones are commonly used in region, let list be a new empty List.
+    auto list = TimeZone::time_zones_in_region(region);
+    quick_sort(list);
+
+    // 5. Return ! CreateArrayFromList( list ).
+    return Array::create_from<StringView>(global_object, list, [&vm](auto value) {
+        return js_string(vm, value);
+    });
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Intl/Locale.h

@@ -78,5 +78,6 @@ Array* calendars_of_locale(GlobalObject& global_object, Locale const& locale);
 Array* collations_of_locale(GlobalObject& global_object, Locale const& locale);
 Array* hour_cycles_of_locale(GlobalObject& global_object, Locale const& locale);
 Array* numbering_systems_of_locale(GlobalObject& global_object, Locale const& locale);
+Array* time_zones_of_locale(GlobalObject& global_object, StringView region);
 
 }

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

@@ -47,6 +47,7 @@ void LocalePrototype::initialize(GlobalObject& global_object)
     define_native_accessor(vm.names.language, language, {}, Attribute::Configurable);
     define_native_accessor(vm.names.script, script, {}, Attribute::Configurable);
     define_native_accessor(vm.names.region, region, {}, Attribute::Configurable);
+    define_native_accessor(vm.names.timeZones, time_zones, {}, Attribute::Configurable);
 }
 
 // 14.3.3 Intl.Locale.prototype.maximize ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.maximize
@@ -223,4 +224,22 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::region)
 JS_ENUMERATE_LOCALE_INFO_PROPERTIES
 #undef __JS_ENUMERATE
 
+// 1.4.20 get Intl.Locale.prototype.timeZones, https://tc39.es/proposal-intl-locale-info/#sec-Intl.Locale.prototype.timeZones
+JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::time_zones)
+{
+    // 1. Let loc be the this value.
+    // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
+    auto* locale_object = TRY(typed_this_object(global_object));
+
+    // 3. Let locale be loc.[[Locale]].
+    auto locale = Unicode::parse_unicode_locale_id(locale_object->locale());
+
+    // 4. If the unicode_language_id production of locale does not contain the ["-" unicode_region_subtag] sequence, return undefined.
+    if (!locale.has_value() || !locale->language_id.region.has_value())
+        return js_undefined();
+
+    // 5. Return ! TimeZonesOfLocale(loc).
+    return time_zones_of_locale(global_object, locale->language_id.region.value());
+}
+
 }

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

@@ -38,6 +38,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(language);
     JS_DECLARE_NATIVE_FUNCTION(script);
     JS_DECLARE_NATIVE_FUNCTION(region);
+    JS_DECLARE_NATIVE_FUNCTION(time_zones);
 };
 
 }

+ 35 - 0
Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.timeZones.js

@@ -0,0 +1,35 @@
+describe("errors", () => {
+    test("called on non-Locale object", () => {
+        expect(() => {
+            Intl.Locale.prototype.timeZones;
+        }).toThrowWithMessage(TypeError, "Not an object of type Intl.Locale");
+    });
+});
+
+describe("normal behavior", () => {
+    test("basic functionality", () => {
+        expect(new Intl.Locale("en").timeZones).toBeUndefined();
+        expect(new Intl.Locale("ar-Latn").timeZones).toBeUndefined();
+
+        const adZones = new Intl.Locale("en-AD").timeZones;
+        expect(Array.isArray(adZones)).toBeTrue();
+        expect(adZones).toEqual(["Europe/Andorra"]);
+
+        const esZones = new Intl.Locale("en-ES").timeZones;
+        expect(Array.isArray(esZones)).toBeTrue();
+        expect(esZones).toEqual(["Africa/Ceuta", "Atlantic/Canary", "Europe/Madrid"]);
+    });
+
+    test("zone list is sorted", () => {
+        const zones = new Intl.Locale("en-US").timeZones;
+        const sortedZones = zones.toSorted();
+
+        expect(zones).toEqual(sortedZones);
+    });
+
+    test("invalid region produces empty list", () => {
+        const zones = new Intl.Locale("en-ZZ").timeZones;
+        expect(Array.isArray(zones)).toBeTrue();
+        expect(zones).toEqual([]);
+    });
+});