mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 03:08:13 +00:00
LibJS: Implement Intl.Locale.prototype.calendars property
This commit is contained in:
parent
028a6b90b1
commit
e9bc35d805
6 changed files with 90 additions and 4 deletions
|
@ -1,9 +1,10 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/Locale.h>
|
||||
#include <LibUnicode/Locale.h>
|
||||
|
@ -50,4 +51,40 @@ Locale::Locale(Unicode::LocaleID const& locale_id, Object& prototype)
|
|||
}
|
||||
}
|
||||
|
||||
// 1.1.1 CreateArrayFromListOrRestricted ( list , restricted )
|
||||
static Array* create_array_from_list_or_restricted(GlobalObject& global_object, Vector<StringView> list, Optional<String> restricted)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. If restricted is not undefined, then
|
||||
if (restricted.has_value()) {
|
||||
// a. Set list to « restricted ».
|
||||
list = { *restricted };
|
||||
}
|
||||
|
||||
// 2. Return ! CreateArrayFromList( list ).
|
||||
return Array::create_from<StringView>(global_object, list, [&vm](auto value) {
|
||||
return js_string(vm, value);
|
||||
});
|
||||
}
|
||||
|
||||
// 1.1.2 CalendarsOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-calendars-of-locale
|
||||
Array* calendars_of_locale(GlobalObject& global_object, Locale const& locale_object)
|
||||
{
|
||||
// 1. Let restricted be loc.[[Calendar]].
|
||||
Optional<String> restricted = locale_object.has_calendar() ? locale_object.calendar() : Optional<String> {};
|
||||
|
||||
// 2. Let locale be loc.[[Locale]].
|
||||
auto const& locale = locale_object.locale();
|
||||
|
||||
// 3. Assert: locale matches the unicode_locale_id production.
|
||||
VERIFY(Unicode::parse_unicode_locale_id(locale).has_value());
|
||||
|
||||
// 4. Let list be a List of 1 or more unique canonical calendar identifiers, which must be lower case String values conforming to the type sequence from UTS 35 Unicode Locale Identifier, section 3.2, sorted in descending preference of those in common use for date and time formatting in locale.
|
||||
auto list = Unicode::get_keywords_for_locale(locale, "ca"sv);
|
||||
|
||||
// 5. Return ! CreateArrayFromListOrRestricted( list, restricted ).
|
||||
return create_array_from_list_or_restricted(global_object, move(list), move(restricted));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue