1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:17:35 +00:00

LibJS: Implement Intl.Locale.prototype.timeZones property

This commit is contained in:
Timothy Flynn 2022-07-05 16:20:36 -04:00 committed by Linus Groh
parent fa005bd276
commit 814f13bc2a
6 changed files with 79 additions and 0 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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