mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:37:35 +00:00
LibJS: Start implementing the stage 3 Intl.DurationFormat proposal
This commit is contained in:
parent
e1ee33ba7c
commit
97fe37bcc2
14 changed files with 998 additions and 0 deletions
194
Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.h
Normal file
194
Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.h
Normal file
|
@ -0,0 +1,194 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Array.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
||||
class DurationFormat final : public Object {
|
||||
JS_OBJECT(DurationFormat, Object);
|
||||
|
||||
public:
|
||||
enum class Style {
|
||||
Long,
|
||||
Short,
|
||||
Narrow,
|
||||
Digital
|
||||
};
|
||||
|
||||
enum class ValueStyle {
|
||||
Long,
|
||||
Short,
|
||||
Narrow,
|
||||
Numeric,
|
||||
TwoDigit
|
||||
};
|
||||
|
||||
enum class Display {
|
||||
Auto,
|
||||
Always
|
||||
};
|
||||
|
||||
static constexpr auto relevant_extension_keys()
|
||||
{
|
||||
// 1.3.3 Internal slots, https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat-internal-slots
|
||||
// The value of the [[RelevantExtensionKeys]] internal slot is « "nu" ».
|
||||
return AK::Array { "nu"sv };
|
||||
}
|
||||
|
||||
explicit DurationFormat(Object& prototype);
|
||||
virtual ~DurationFormat() override = default;
|
||||
|
||||
void set_locale(String locale) { m_locale = move(locale); }
|
||||
String const& locale() const { return m_locale; }
|
||||
|
||||
void set_data_locale(String data_locale) { m_data_locale = move(data_locale); }
|
||||
String const& data_locale() const { return m_data_locale; }
|
||||
|
||||
void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); }
|
||||
String const& numbering_system() const { return m_numbering_system; }
|
||||
|
||||
void set_style(StringView style) { m_style = style_from_string(style); }
|
||||
String style_string() const { return style_to_string(m_style); }
|
||||
|
||||
void set_years_style(StringView years_style) { m_years_style = date_style_from_string(years_style); }
|
||||
StringView years_style_string() const { return value_style_to_string(m_years_style); }
|
||||
|
||||
void set_years_display(StringView years_display) { m_years_display = display_from_string(years_display); }
|
||||
StringView years_display_string() const { return display_to_string(m_years_display); }
|
||||
|
||||
void set_months_style(StringView months_style) { m_months_style = date_style_from_string(months_style); }
|
||||
StringView months_style_string() const { return value_style_to_string(m_months_style); }
|
||||
|
||||
void set_months_display(StringView months_display) { m_months_display = display_from_string(months_display); }
|
||||
StringView months_display_string() const { return display_to_string(m_months_display); }
|
||||
|
||||
void set_weeks_style(StringView weeks_style) { m_weeks_style = date_style_from_string(weeks_style); }
|
||||
StringView weeks_style_string() const { return value_style_to_string(m_weeks_style); }
|
||||
|
||||
void set_weeks_display(StringView weeks_display) { m_weeks_display = display_from_string(weeks_display); }
|
||||
StringView weeks_display_string() const { return display_to_string(m_weeks_display); }
|
||||
|
||||
void set_days_style(StringView days_style) { m_days_style = date_style_from_string(days_style); }
|
||||
StringView days_style_string() const { return value_style_to_string(m_days_style); }
|
||||
|
||||
void set_days_display(StringView days_display) { m_days_display = display_from_string(days_display); }
|
||||
StringView days_display_string() const { return display_to_string(m_days_display); }
|
||||
|
||||
void set_hours_style(StringView hours_style) { m_hours_style = time_style_from_string(hours_style); }
|
||||
StringView hours_style_string() const { return value_style_to_string(m_hours_style); }
|
||||
|
||||
void set_hours_display(StringView hours_display) { m_hours_display = display_from_string(hours_display); }
|
||||
StringView hours_display_string() const { return display_to_string(m_hours_display); }
|
||||
|
||||
void set_minutes_style(StringView minutes_style) { m_minutes_style = time_style_from_string(minutes_style); }
|
||||
StringView minutes_style_string() const { return value_style_to_string(m_minutes_style); }
|
||||
|
||||
void set_minutes_display(StringView minutes_display) { m_minutes_display = display_from_string(minutes_display); }
|
||||
StringView minutes_display_string() const { return display_to_string(m_minutes_display); }
|
||||
|
||||
void set_seconds_style(StringView seconds_style) { m_seconds_style = time_style_from_string(seconds_style); }
|
||||
StringView seconds_style_string() const { return value_style_to_string(m_seconds_style); }
|
||||
|
||||
void set_seconds_display(StringView seconds_display) { m_seconds_display = display_from_string(seconds_display); }
|
||||
StringView seconds_display_string() const { return display_to_string(m_seconds_display); }
|
||||
|
||||
void set_milliseconds_style(StringView milliseconds_style) { m_milliseconds_style = sub_second_style_from_string(milliseconds_style); }
|
||||
StringView milliseconds_style_string() const { return value_style_to_string(m_milliseconds_style); }
|
||||
|
||||
void set_milliseconds_display(StringView milliseconds_display) { m_milliseconds_display = display_from_string(milliseconds_display); }
|
||||
StringView milliseconds_display_string() const { return display_to_string(m_milliseconds_display); }
|
||||
|
||||
void set_microseconds_style(StringView microseconds_style) { m_microseconds_style = sub_second_style_from_string(microseconds_style); }
|
||||
StringView microseconds_style_string() const { return value_style_to_string(m_microseconds_style); }
|
||||
|
||||
void set_microseconds_display(StringView microseconds_display) { m_microseconds_display = display_from_string(microseconds_display); }
|
||||
StringView microseconds_display_string() const { return display_to_string(m_microseconds_display); }
|
||||
|
||||
void set_nanoseconds_style(StringView nanoseconds_style) { m_nanoseconds_style = sub_second_style_from_string(nanoseconds_style); }
|
||||
StringView nanoseconds_style_string() const { return value_style_to_string(m_nanoseconds_style); }
|
||||
|
||||
void set_nanoseconds_display(StringView nanoseconds_display) { m_nanoseconds_display = display_from_string(nanoseconds_display); }
|
||||
StringView nanoseconds_display_string() const { return display_to_string(m_nanoseconds_display); }
|
||||
|
||||
void set_fractional_digits(Optional<u8> fractional_digits) { m_fractional_digits = move(fractional_digits); }
|
||||
bool has_fractional_digits() const { return m_fractional_digits.has_value(); }
|
||||
u8 fractional_digits() const { return m_fractional_digits.value(); }
|
||||
|
||||
private:
|
||||
static Style style_from_string(StringView style);
|
||||
static StringView style_to_string(Style);
|
||||
static ValueStyle date_style_from_string(StringView date_style);
|
||||
static ValueStyle time_style_from_string(StringView time_style);
|
||||
static ValueStyle sub_second_style_from_string(StringView sub_second_style);
|
||||
static StringView value_style_to_string(ValueStyle);
|
||||
static Display display_from_string(StringView display);
|
||||
static StringView display_to_string(Display);
|
||||
|
||||
String m_locale; // [[Locale]]
|
||||
String m_data_locale; // [[DataLocale]]
|
||||
String m_numbering_system; // [[NumberingSystem]]
|
||||
Style m_style; // [[Style]]
|
||||
ValueStyle m_years_style { ValueStyle::Long }; // [[YearsStyle]]
|
||||
Display m_years_display { Display::Auto }; // [[YearsDisplay]]
|
||||
ValueStyle m_months_style { ValueStyle::Long }; // [[MonthsStyle]]
|
||||
Display m_months_display { Display::Auto }; // [[MonthsDisplay]]
|
||||
ValueStyle m_weeks_style { ValueStyle::Long }; // [[WeeksStyle]]
|
||||
Display m_weeks_display { Display::Auto }; // [[WeeksDisplay]]
|
||||
ValueStyle m_days_style { ValueStyle::Long }; // [[DaysStyle]]
|
||||
Display m_days_display { Display::Auto }; // [[DaysDisplay]]
|
||||
ValueStyle m_hours_style { ValueStyle::Long }; // [[HoursStyle]]
|
||||
Display m_hours_display { Display::Auto }; // [[HoursDisplay]]
|
||||
ValueStyle m_minutes_style { ValueStyle::Long }; // [[MinutesStyle]]
|
||||
Display m_minutes_display { Display::Auto }; // [[MinutesDisplay]]
|
||||
ValueStyle m_seconds_style { ValueStyle::Long }; // [[SecondsStyle]]
|
||||
Display m_seconds_display { Display::Auto }; // [[SecondsDisplay]]
|
||||
ValueStyle m_milliseconds_style { ValueStyle::Long }; // [[MillisecondsStyle]]
|
||||
Display m_milliseconds_display { Display::Auto }; // [[MillisecondsDisplay]]
|
||||
ValueStyle m_microseconds_style { ValueStyle::Long }; // [[MicrosecondsStyle]]
|
||||
Display m_microseconds_display { Display::Auto }; // [[MicrosecondsDisplay]]
|
||||
ValueStyle m_nanoseconds_style { ValueStyle::Long }; // [[NanosecondsStyle]]
|
||||
Display m_nanoseconds_display { Display::Auto }; // [[NanosecondsDisplay]]
|
||||
Optional<u8> m_fractional_digits; // [[FractionalDigits]]
|
||||
};
|
||||
|
||||
struct DurationInstanceComponent {
|
||||
void (DurationFormat::*set_style_slot)(StringView);
|
||||
void (DurationFormat::*set_display_slot)(StringView);
|
||||
StringView unit;
|
||||
Span<StringView const> values;
|
||||
StringView digital_default;
|
||||
};
|
||||
|
||||
// Table 1: Components of Duration Instances, https://tc39.es/proposal-intl-duration-format/#table-duration-component
|
||||
static constexpr AK::Array<StringView, 3> date_values = { "long"sv, "short"sv, "narrow"sv };
|
||||
static constexpr AK::Array<StringView, 5> time_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv, "2-digit"sv };
|
||||
static constexpr AK::Array<StringView, 4> sub_second_values = { "long"sv, "short"sv, "narrow"sv, "numeric"sv };
|
||||
static constexpr AK::Array<DurationInstanceComponent, 10> duration_instances_components {
|
||||
DurationInstanceComponent { &DurationFormat::set_years_style, &DurationFormat::set_years_display, "years"sv, date_values, "narrow"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_months_style, &DurationFormat::set_months_display, "months"sv, date_values, "narrow"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_weeks_style, &DurationFormat::set_weeks_display, "weeks"sv, date_values, "narrow"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_days_style, &DurationFormat::set_days_display, "days"sv, date_values, "narrow"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_hours_style, &DurationFormat::set_hours_display, "hours"sv, time_values, "numeric"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_minutes_style, &DurationFormat::set_minutes_display, "minutes"sv, time_values, "numeric"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_seconds_style, &DurationFormat::set_seconds_display, "seconds"sv, time_values, "numeric"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_milliseconds_style, &DurationFormat::set_milliseconds_display, "milliseconds"sv, sub_second_values, "numeric"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_microseconds_style, &DurationFormat::set_microseconds_display, "microseconds"sv, sub_second_values, "numeric"sv },
|
||||
DurationInstanceComponent { &DurationFormat::set_nanoseconds_style, &DurationFormat::set_nanoseconds_display, "nanoseconds"sv, sub_second_values, "numeric"sv },
|
||||
};
|
||||
|
||||
struct DurationUnitOptions {
|
||||
String style;
|
||||
String display;
|
||||
};
|
||||
|
||||
ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(GlobalObject& global_object, String const& unit, Object const& options, StringView base_style, Span<StringView const> styles_list, StringView digital_base, Optional<String> const& previous_style);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue