1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibJS+LibUnicode: Implement the Intl.DateTimeFormat constructor

This commit is contained in:
Timothy Flynn 2021-11-28 17:55:47 -05:00 committed by Linus Groh
parent 75b2a09a2f
commit 16151aa7d5
9 changed files with 1089 additions and 2 deletions

View file

@ -10,6 +10,8 @@
#include <AK/StringView.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibUnicode/DateTimeFormat.h>
@ -123,4 +125,48 @@ private:
Optional<Style> m_time_style; // [[TimeStyle]]
};
enum class OptionRequired {
Any,
Date,
Time,
};
enum class OptionDefaults {
All,
Date,
Time,
};
ThrowCompletionOr<DateTimeFormat*> initialize_date_time_format(GlobalObject& global_object, DateTimeFormat& date_time_format, Value locales_value, Value options_value);
ThrowCompletionOr<Object*> to_date_time_options(GlobalObject& global_object, Value options_value, OptionRequired, OptionDefaults);
Optional<Unicode::CalendarPattern> date_time_style_format(StringView data_locale, DateTimeFormat& date_time_format);
Optional<Unicode::CalendarPattern> basic_format_matcher(Unicode::CalendarPattern const& options, Vector<Unicode::CalendarPattern> formats);
Optional<Unicode::CalendarPattern> best_fit_format_matcher(Unicode::CalendarPattern const& options, Vector<Unicode::CalendarPattern> formats);
template<typename Callback>
ThrowCompletionOr<void> for_each_calendar_field(GlobalObject& global_object, Unicode::CalendarPattern& pattern, Callback&& callback)
{
auto& vm = global_object.vm();
constexpr auto narrow_short_long = AK::Array { "narrow"sv, "short"sv, "long"sv };
constexpr auto two_digit_numeric = AK::Array { "2-digit"sv, "numeric"sv };
constexpr auto two_digit_numeric_narrow_short_long = AK::Array { "2-digit"sv, "numeric"sv, "narrow"sv, "short"sv, "long"sv };
constexpr auto short_long = AK::Array { "short"sv, "long"sv };
// Table 4: Components of date and time formats, https://tc39.es/ecma402/#table-datetimeformat-components
TRY(callback(pattern.weekday, vm.names.weekday, narrow_short_long));
TRY(callback(pattern.era, vm.names.era, narrow_short_long));
TRY(callback(pattern.year, vm.names.year, two_digit_numeric));
TRY(callback(pattern.month, vm.names.month, two_digit_numeric_narrow_short_long));
TRY(callback(pattern.day, vm.names.day, two_digit_numeric));
TRY(callback(pattern.day_period, vm.names.dayPeriod, narrow_short_long));
TRY(callback(pattern.hour, vm.names.hour, two_digit_numeric));
TRY(callback(pattern.minute, vm.names.minute, two_digit_numeric));
TRY(callback(pattern.second, vm.names.second, two_digit_numeric));
TRY(callback(pattern.fractional_second_digits, vm.names.fractionalSecondDigits, Empty {}));
TRY(callback(pattern.time_zone_name, vm.names.timeZoneName, short_long));
return {};
}
}