mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 23:47:45 +00:00
LibJS: Implement Intl.Locale.prototype.weekInfo property
This commit is contained in:
parent
12e7c0808a
commit
b3deec061e
6 changed files with 172 additions and 0 deletions
|
@ -174,6 +174,7 @@ namespace JS {
|
|||
P(findLast) \
|
||||
P(findLastIndex) \
|
||||
P(findIndex) \
|
||||
P(firstDay) \
|
||||
P(fixed) \
|
||||
P(flags) \
|
||||
P(flat) \
|
||||
|
@ -330,6 +331,7 @@ namespace JS {
|
|||
P(milliseconds) \
|
||||
P(millisecondsDisplay) \
|
||||
P(min) \
|
||||
P(minimalDays) \
|
||||
P(minimize) \
|
||||
P(maximumFractionDigits) \
|
||||
P(maximumSignificantDigits) \
|
||||
|
@ -531,8 +533,10 @@ namespace JS {
|
|||
P(valueOf) \
|
||||
P(values) \
|
||||
P(warn) \
|
||||
P(weekInfo) \
|
||||
P(weekOfYear) \
|
||||
P(weekday) \
|
||||
P(weekend) \
|
||||
P(weeks) \
|
||||
P(weeksDisplay) \
|
||||
P(with) \
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/Locale.h>
|
||||
#include <LibTimeZone/TimeZone.h>
|
||||
#include <LibUnicode/DateTimeFormat.h>
|
||||
#include <LibUnicode/Locale.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
@ -186,4 +187,64 @@ StringView character_direction_of_locale(Locale const& locale_object)
|
|||
return "ltr"sv;
|
||||
}
|
||||
|
||||
static u8 weekday_to_integer(Optional<Unicode::Weekday> weekday, Unicode::Weekday falllback)
|
||||
{
|
||||
// NOTE: This fallback will be used if LibUnicode data generation is disabled. Its value should
|
||||
// be that of the default region ("001") in the CLDR.
|
||||
switch (weekday.value_or(falllback)) {
|
||||
case Unicode::Weekday::Monday:
|
||||
return 1;
|
||||
case Unicode::Weekday::Tuesday:
|
||||
return 2;
|
||||
case Unicode::Weekday::Wednesday:
|
||||
return 3;
|
||||
case Unicode::Weekday::Thursday:
|
||||
return 4;
|
||||
case Unicode::Weekday::Friday:
|
||||
return 5;
|
||||
case Unicode::Weekday::Saturday:
|
||||
return 6;
|
||||
case Unicode::Weekday::Sunday:
|
||||
return 7;
|
||||
}
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
static Vector<u8> weekend_of_locale(StringView locale)
|
||||
{
|
||||
auto weekend_start = weekday_to_integer(Unicode::get_locale_weekend_start(locale), Unicode::Weekday::Saturday);
|
||||
auto weekend_end = weekday_to_integer(Unicode::get_locale_weekend_end(locale), Unicode::Weekday::Sunday);
|
||||
|
||||
// There currently aren't any regions in the CLDR which wrap around from Sunday (7) to Monday (1).
|
||||
// If this changes, this logic will need to be updated to handle that.
|
||||
VERIFY(weekend_start <= weekend_end);
|
||||
|
||||
Vector<u8> weekend;
|
||||
weekend.ensure_capacity(weekend_end - weekend_start + 1);
|
||||
|
||||
for (auto day = weekend_start; day <= weekend_end; ++day)
|
||||
weekend.unchecked_append(day);
|
||||
|
||||
return weekend;
|
||||
}
|
||||
|
||||
// 1.1.8 WeekInfoOfLocale ( loc ), https://tc39.es/proposal-intl-locale-info/#sec-week-info-of-locale
|
||||
WeekInfo week_info_of_locale(Locale const& locale_object)
|
||||
{
|
||||
// 1. Let locale be loc.[[Locale]].
|
||||
auto const& locale = locale_object.locale();
|
||||
|
||||
// 2. Assert: locale matches the unicode_locale_id production.
|
||||
VERIFY(Unicode::parse_unicode_locale_id(locale).has_value());
|
||||
|
||||
// 3. Return a record whose fields are defined by Table 1, with values based on locale.
|
||||
WeekInfo week_info {};
|
||||
week_info.minimal_days = Unicode::get_locale_minimum_days(locale).value_or(1);
|
||||
week_info.first_day = weekday_to_integer(Unicode::get_locale_first_day(locale), Unicode::Weekday::Monday);
|
||||
week_info.weekend = weekend_of_locale(locale);
|
||||
|
||||
return week_info;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -74,11 +74,19 @@ private:
|
|||
bool m_numeric { false }; // [[Numeric]]
|
||||
};
|
||||
|
||||
// Table 1: WeekInfo Record Fields, https://tc39.es/proposal-intl-locale-info/#table-locale-weekinfo-record
|
||||
struct WeekInfo {
|
||||
u8 minimal_days { 0 }; // [[MinimalDays]]
|
||||
u8 first_day { 0 }; // [[FirstDay]]
|
||||
Vector<u8> weekend; // [[Weekend]]
|
||||
};
|
||||
|
||||
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);
|
||||
StringView character_direction_of_locale(Locale const& locale);
|
||||
WeekInfo week_info_of_locale(Locale const& locale);
|
||||
|
||||
}
|
||||
|
|
|
@ -49,6 +49,7 @@ void LocalePrototype::initialize(GlobalObject& global_object)
|
|||
define_native_accessor(vm.names.region, region, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.timeZones, time_zones, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.textInfo, text_info, {}, Attribute::Configurable);
|
||||
define_native_accessor(vm.names.weekInfo, week_info, {}, Attribute::Configurable);
|
||||
}
|
||||
|
||||
// 14.3.3 Intl.Locale.prototype.maximize ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.maximize
|
||||
|
@ -263,4 +264,33 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::text_info)
|
|||
return info;
|
||||
}
|
||||
|
||||
// 1.4.22 get Intl.Locale.prototype.weekInfo, https://tc39.es/proposal-intl-locale-info/#sec-Intl.Locale.prototype.weekInfo
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::week_info)
|
||||
{
|
||||
// 1. Let loc be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]).
|
||||
[[maybe_unused]] auto* locale_object = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Let info be ! ObjectCreate(%Object.prototype%).
|
||||
auto* info = Object::create(global_object, global_object.object_prototype());
|
||||
|
||||
// 4. Let wi be ! WeekInfoOfLocale(loc).
|
||||
auto week_info = week_info_of_locale(*locale_object);
|
||||
|
||||
// 5. Let we be ! CreateArrayFromList( wi.[[Weekend]] ).
|
||||
auto weekend = Array::create_from<u8>(global_object, week_info.weekend, [](auto day) { return Value(day); });
|
||||
|
||||
// 6. Perform ! CreateDataPropertyOrThrow(info, "firstDay", wi.[[FirstDay]]).
|
||||
MUST(info->create_data_property_or_throw(vm.names.firstDay, Value(week_info.first_day)));
|
||||
|
||||
// 7. Perform ! CreateDataPropertyOrThrow(info, "weekend", we).
|
||||
MUST(info->create_data_property_or_throw(vm.names.weekend, weekend));
|
||||
|
||||
// 8. Perform ! CreateDataPropertyOrThrow(info, "minimalDays", wi.[[MinimalDays]]).
|
||||
MUST(info->create_data_property_or_throw(vm.names.minimalDays, Value(week_info.minimal_days)));
|
||||
|
||||
// 9. Return info.
|
||||
return info;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(region);
|
||||
JS_DECLARE_NATIVE_FUNCTION(time_zones);
|
||||
JS_DECLARE_NATIVE_FUNCTION(text_info);
|
||||
JS_DECLARE_NATIVE_FUNCTION(week_info);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue