mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-08 12:19:37 +03:00
LibJS: Start implementing the stage 3 Intl.DurationFormat proposal
This commit is contained in:
parent
e1ee33ba7c
commit
97fe37bcc2
Notes:
sideshowbarker
2024-07-17 09:48:24 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/97fe37bcc2 Pull-request: https://github.com/SerenityOS/serenity/pull/14437 Reviewed-by: https://github.com/linusg ✅
@ -104,6 +104,9 @@ set(SOURCES
|
||||
Runtime/Intl/DisplayNames.cpp
|
||||
Runtime/Intl/DisplayNamesConstructor.cpp
|
||||
Runtime/Intl/DisplayNamesPrototype.cpp
|
||||
Runtime/Intl/DurationFormat.cpp
|
||||
Runtime/Intl/DurationFormatConstructor.cpp
|
||||
Runtime/Intl/DurationFormatPrototype.cpp
|
||||
Runtime/Intl/Intl.cpp
|
||||
Runtime/Intl/ListFormat.cpp
|
||||
Runtime/Intl/ListFormatConstructor.cpp
|
||||
|
@ -73,6 +73,7 @@
|
||||
__JS_ENUMERATE(Collator, collator, CollatorPrototype, CollatorConstructor) \
|
||||
__JS_ENUMERATE(DateTimeFormat, date_time_format, DateTimeFormatPrototype, DateTimeFormatConstructor) \
|
||||
__JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \
|
||||
__JS_ENUMERATE(DurationFormat, duration_format, DurationFormatPrototype, DurationFormatConstructor) \
|
||||
__JS_ENUMERATE(ListFormat, list_format, ListFormatPrototype, ListFormatConstructor) \
|
||||
__JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor) \
|
||||
__JS_ENUMERATE(NumberFormat, number_format, NumberFormatPrototype, NumberFormatConstructor) \
|
||||
|
@ -183,6 +183,7 @@ namespace JS {
|
||||
P(formatRange) \
|
||||
P(formatRangeToParts) \
|
||||
P(formatToParts) \
|
||||
P(fractionalDigits) \
|
||||
P(fractionalSecondDigits) \
|
||||
P(freeze) \
|
||||
P(from) \
|
||||
|
@ -49,6 +49,8 @@
|
||||
M(IntlNumberIsNaNOrInfinity, "Number must not be NaN or Infinity") \
|
||||
M(IntlNumberIsNaNOrOutOfRange, "Value {} is NaN or is not between {} and {}") \
|
||||
M(IntlOptionUndefined, "Option {} must be defined when option {} is {}") \
|
||||
M(IntlNonNumericOr2DigitAfterNumericOr2Digit, "Styles other than 'numeric' and '2-digit' may not be used in smaller units after " \
|
||||
"being used in larger units") \
|
||||
M(InvalidAssignToConst, "Invalid assignment to const variable") \
|
||||
M(InvalidCodePoint, "Invalid code point {}, must be an integer no less than 0 and no greater than 0x10FFFF") \
|
||||
M(InvalidFormat, "Invalid {} format") \
|
||||
|
@ -57,6 +57,8 @@
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormatPrototype.h>
|
||||
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/DisplayNamesPrototype.h>
|
||||
#include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/DurationFormatPrototype.h>
|
||||
#include <LibJS/Runtime/Intl/Intl.h>
|
||||
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/ListFormatPrototype.h>
|
||||
|
182
Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp
Normal file
182
Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp
Normal file
@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Intl/DurationFormat.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
||||
// 1 DurationFormat Objects, https://tc39.es/proposal-intl-duration-format/#durationformat-objects
|
||||
DurationFormat::DurationFormat(Object& prototype)
|
||||
: Object(prototype)
|
||||
{
|
||||
}
|
||||
|
||||
DurationFormat::Style DurationFormat::style_from_string(StringView style)
|
||||
{
|
||||
if (style == "long"sv)
|
||||
return Style::Long;
|
||||
if (style == "short"sv)
|
||||
return Style::Short;
|
||||
if (style == "narrow"sv)
|
||||
return Style::Narrow;
|
||||
if (style == "digital"sv)
|
||||
return Style::Digital;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
StringView DurationFormat::style_to_string(Style style)
|
||||
{
|
||||
switch (style) {
|
||||
case Style::Long:
|
||||
return "long"sv;
|
||||
case Style::Short:
|
||||
return "short"sv;
|
||||
case Style::Narrow:
|
||||
return "narrow"sv;
|
||||
case Style::Digital:
|
||||
return "digital"sv;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
DurationFormat::ValueStyle DurationFormat::date_style_from_string(StringView date_style)
|
||||
{
|
||||
if (date_style == "long"sv)
|
||||
return ValueStyle::Long;
|
||||
if (date_style == "short"sv)
|
||||
return ValueStyle::Short;
|
||||
if (date_style == "narrow"sv)
|
||||
return ValueStyle::Narrow;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
DurationFormat::ValueStyle DurationFormat::time_style_from_string(StringView time_style)
|
||||
{
|
||||
if (time_style == "long"sv)
|
||||
return ValueStyle::Long;
|
||||
if (time_style == "short"sv)
|
||||
return ValueStyle::Short;
|
||||
if (time_style == "narrow"sv)
|
||||
return ValueStyle::Narrow;
|
||||
if (time_style == "numeric"sv)
|
||||
return ValueStyle::Numeric;
|
||||
if (time_style == "2-digit"sv)
|
||||
return ValueStyle::TwoDigit;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
DurationFormat::ValueStyle DurationFormat::sub_second_style_from_string(StringView sub_second_style)
|
||||
{
|
||||
if (sub_second_style == "long"sv)
|
||||
return ValueStyle::Long;
|
||||
if (sub_second_style == "short"sv)
|
||||
return ValueStyle::Short;
|
||||
if (sub_second_style == "narrow"sv)
|
||||
return ValueStyle::Narrow;
|
||||
if (sub_second_style == "numeric"sv)
|
||||
return ValueStyle::Numeric;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
DurationFormat::Display DurationFormat::display_from_string(StringView display)
|
||||
{
|
||||
if (display == "auto"sv)
|
||||
return Display::Auto;
|
||||
if (display == "always"sv)
|
||||
return Display::Always;
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
StringView DurationFormat::value_style_to_string(ValueStyle value_style)
|
||||
{
|
||||
switch (value_style) {
|
||||
case ValueStyle::Long:
|
||||
return "long"sv;
|
||||
case ValueStyle::Short:
|
||||
return "short"sv;
|
||||
case ValueStyle::Narrow:
|
||||
return "narrow"sv;
|
||||
case ValueStyle::Numeric:
|
||||
return "numeric"sv;
|
||||
case ValueStyle::TwoDigit:
|
||||
return "2-digit"sv;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
StringView DurationFormat::display_to_string(Display display)
|
||||
{
|
||||
switch (display) {
|
||||
case Display::Auto:
|
||||
return "auto"sv;
|
||||
case Display::Always:
|
||||
return "always"sv;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
// 1.1.2 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions
|
||||
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)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let style be ? GetOption(options, unit, "string", stylesList, undefined).
|
||||
auto style_value = TRY(get_option(global_object, options, unit, OptionType::String, styles_list, Empty {}));
|
||||
|
||||
// 2. Let displayDefault be "always".
|
||||
auto display_default = "always"sv;
|
||||
|
||||
String style;
|
||||
|
||||
// 3. If style is undefined, then
|
||||
if (style_value.is_undefined()) {
|
||||
// a. Set displayDefault to "auto".
|
||||
display_default = "auto"sv;
|
||||
|
||||
// b. If baseStyle is "digital", then
|
||||
if (base_style == "digital"sv) {
|
||||
// i. Set style to digitalBase.
|
||||
style = digital_base;
|
||||
}
|
||||
// c. Else,
|
||||
else {
|
||||
// i. Set style to baseStyle.
|
||||
style = base_style;
|
||||
}
|
||||
} else {
|
||||
style = style_value.as_string().string();
|
||||
}
|
||||
|
||||
// 4. Let displayField be the string-concatenation of unit and "Display".
|
||||
auto display_field = String::formatted("{}Display", unit);
|
||||
|
||||
// 5. Let display be ? GetOption(options, displayField, "string", « "auto", "always" », displayDefault).
|
||||
auto display = TRY(get_option(global_object, options, display_field, OptionType::String, { "auto"sv, "always"sv }, display_default));
|
||||
|
||||
// 6. If prevStyle is "numeric" or "2-digit", then
|
||||
if (previous_style == "numeric"sv || previous_style == "2-digit"sv) {
|
||||
// a. If style is not "numeric" or "2-digit", then
|
||||
if (style != "numeric"sv && style != "2-digit"sv) {
|
||||
// i. Throw a RangeError exception.
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNonNumericOr2DigitAfterNumericOr2Digit);
|
||||
}
|
||||
// b. Else if unit is "minutes" or "seconds", then
|
||||
else if (unit == "minutes"sv || unit == "seconds"sv) {
|
||||
// i. Set style to "2-digit".
|
||||
style = "2-digit"sv;
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Return the Record { [[Style]]: style, [[Display]]: display }.
|
||||
return DurationUnitOptions { .style = move(style), .display = display.as_string().string() };
|
||||
}
|
||||
|
||||
}
|
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);
|
||||
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Intl/DurationFormat.h>
|
||||
#include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
||||
// 1.2 The Intl.DurationFormat Constructor, https://tc39.es/proposal-intl-duration-format/#sec-intl-durationformat-constructor
|
||||
DurationFormatConstructor::DurationFormatConstructor(GlobalObject& global_object)
|
||||
: NativeFunction(vm().names.DurationFormat.as_string(), *global_object.function_prototype())
|
||||
{
|
||||
}
|
||||
|
||||
void DurationFormatConstructor::initialize(GlobalObject& global_object)
|
||||
{
|
||||
NativeFunction::initialize(global_object);
|
||||
|
||||
auto& vm = this->vm();
|
||||
|
||||
// 1.3.1 Intl.DurationFormat.prototype, https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.prototype
|
||||
define_direct_property(vm.names.prototype, global_object.intl_duration_format_prototype(), 0);
|
||||
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
||||
}
|
||||
|
||||
// 1.2.1 Intl.DurationFormat ( [ locales [ , options ] ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat
|
||||
ThrowCompletionOr<Value> DurationFormatConstructor::call()
|
||||
{
|
||||
// 1. If NewTarget is undefined, throw a TypeError exception.
|
||||
return vm().throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Intl.DurationFormat");
|
||||
}
|
||||
|
||||
// 1.2.1 Intl.DurationFormat ( [ locales [ , options ] ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat
|
||||
ThrowCompletionOr<Object*> DurationFormatConstructor::construct(FunctionObject& new_target)
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
auto& global_object = this->global_object();
|
||||
|
||||
auto locales = vm.argument(0);
|
||||
auto options_value = vm.argument(1);
|
||||
|
||||
// 2. Let durationFormat be ? OrdinaryCreateFromConstructor(NewTarget, "%DurationFormatPrototype%", « [[InitializedDurationFormat]], [[Locale]], [[DataLocale]], [[NumberingSystem]], [[Style]], [[YearsStyle]], [[YearsDisplay]], [[MonthsStyle]], [[MonthsDisplay]] , [[WeeksStyle]], [[WeeksDisplay]] , [[DaysStyle]], [[DaysDisplay]] , [[HoursStyle]], [[HoursDisplay]] , [[MinutesStyle]], [[MinutesDisplay]] , [[SecondsStyle]], [[SecondsDisplay]] , [[MillisecondsStyle]], [[MillisecondsDisplay]] , [[MicrosecondsStyle]], [[MicrosecondsDisplay]] , [[NanosecondsStyle]], [[NanosecondsDisplay]], [[FractionalDigits]] »).
|
||||
auto* duration_format = TRY(ordinary_create_from_constructor<DurationFormat>(global_object, new_target, &GlobalObject::intl_duration_format_prototype));
|
||||
|
||||
// 3. Let requestedLocales be ? CanonicalizeLocaleList(locales).
|
||||
auto requested_locales = TRY(canonicalize_locale_list(global_object, locales));
|
||||
|
||||
// 4. Let options be ? GetOptionsObject(options).
|
||||
auto* options = TRY(Temporal::get_options_object(global_object, options_value));
|
||||
|
||||
// 5. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
|
||||
auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, OptionType::String, { "lookup"sv, "best fit"sv }, "best fit"sv));
|
||||
|
||||
// 6. Let numberingSystem be ? GetOption(options, "numberingSystem", "string", undefined, undefined).
|
||||
auto numbering_system = TRY(get_option(global_object, *options, vm.names.numberingSystem, OptionType::String, {}, Empty {}));
|
||||
|
||||
// FIXME: Missing spec step - If numberingSystem is not undefined, then
|
||||
if (!numbering_system.is_undefined()) {
|
||||
// 7. If numberingSystem does not match the Unicode Locale Identifier type nonterminal, throw a RangeError exception.
|
||||
if (numbering_system.is_undefined() || !Unicode::is_type_identifier(numbering_system.as_string().string()))
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, numbering_system, "numberingSystem"sv);
|
||||
}
|
||||
|
||||
// 8. Let opt be the Record { [[localeMatcher]]: matcher, [[nu]]: numberingSystem }.
|
||||
LocaleOptions opt {};
|
||||
opt.locale_matcher = matcher;
|
||||
opt.nu = numbering_system.is_undefined() ? Optional<String>() : numbering_system.as_string().string();
|
||||
|
||||
// 9. Let r be ResolveLocale(%DurationFormat%.[[AvailableLocales]], requestedLocales, opt, %DurationFormat%.[[RelevantExtensionKeys]], %DurationFormat%.[[LocaleData]]).
|
||||
auto result = resolve_locale(requested_locales, opt, DurationFormat::relevant_extension_keys());
|
||||
|
||||
// 10. Let locale be r.[[locale]].
|
||||
auto locale = move(result.locale);
|
||||
|
||||
// 11. Set durationFormat.[[Locale]] to locale.
|
||||
duration_format->set_locale(move(locale));
|
||||
|
||||
// 12. Set durationFormat.[[NumberingSystem]] to r.[[nu]].
|
||||
if (result.nu.has_value())
|
||||
duration_format->set_numbering_system(result.nu.release_value());
|
||||
|
||||
// 13. Let style be ? GetOption(options, "style", "string", « "long", "short", "narrow", "digital" », "long").
|
||||
auto style = TRY(get_option(global_object, *options, vm.names.style, OptionType::String, { "long"sv, "short"sv, "narrow"sv, "digital"sv }, "long"sv));
|
||||
|
||||
// 14. Set durationFormat.[[Style]] to style.
|
||||
duration_format->set_style(style.as_string().string());
|
||||
|
||||
// 15. Set durationFormat.[[DataLocale]] to r.[[dataLocale]].
|
||||
duration_format->set_data_locale(move(result.data_locale));
|
||||
|
||||
// 16. Let prevStyle be undefined.
|
||||
Optional<String> previous_style;
|
||||
|
||||
// 17. For each row in Table 1, except the header row, in table order, do
|
||||
for (auto const& duration_instances_component : duration_instances_components) {
|
||||
// a. Let styleSlot be the Style Slot value.
|
||||
auto style_slot = duration_instances_component.set_style_slot;
|
||||
|
||||
// b. Let displaySlot be the Display Slot value.
|
||||
auto display_slot = duration_instances_component.set_display_slot;
|
||||
|
||||
// c. Let unit be the Unit value.
|
||||
auto unit = duration_instances_component.unit;
|
||||
|
||||
// d. Let valueList be the Values value.
|
||||
auto value_list = duration_instances_component.values;
|
||||
|
||||
// e. Let digitalBase be the Digital Default value.
|
||||
auto digital_base = duration_instances_component.digital_default;
|
||||
|
||||
// f. Let unitOptions be ? GetDurationUnitOptions(unit, options, style, valueList, digitalBase, prevStyle).
|
||||
auto unit_options = TRY(get_duration_unit_options(global_object, unit, *options, style.as_string().string(), value_list, digital_base, previous_style));
|
||||
|
||||
// g. Set the value of the styleSlot slot of durationFormat to unitOptions.[[Style]].
|
||||
(duration_format->*style_slot)(unit_options.style);
|
||||
|
||||
// h. Set the value of the displaySlot slot of durationFormat to unitOptions.[[Display]].
|
||||
(duration_format->*display_slot)(unit_options.display);
|
||||
|
||||
// i. If unit is one of "hours", "minutes", "seconds", "milliseconds", or "microseconds", then
|
||||
if (unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv, "milliseconds"sv, "microseconds"sv)) {
|
||||
// i. Set prevStyle to unitOptions.[[Style]].
|
||||
previous_style = unit_options.style;
|
||||
}
|
||||
}
|
||||
|
||||
// 18. Set durationFormat.[[FractionalDigits]] to ? GetNumberOption(options, "fractionalDigits", 0, 9, undefined).
|
||||
duration_format->set_fractional_digits(Optional<u8>(TRY(get_number_option(global_object, *options, vm.names.fractionalDigits, 0, 9, {}))));
|
||||
|
||||
// 19. Return durationFormat.
|
||||
return duration_format;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/NativeFunction.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
||||
class DurationFormatConstructor final : public NativeFunction {
|
||||
JS_OBJECT(DurationFormatConstructor, NativeFunction);
|
||||
|
||||
public:
|
||||
explicit DurationFormatConstructor(GlobalObject&);
|
||||
virtual void initialize(GlobalObject&) override;
|
||||
virtual ~DurationFormatConstructor() override = default;
|
||||
|
||||
virtual ThrowCompletionOr<Value> call() override;
|
||||
virtual ThrowCompletionOr<Object*> construct(FunctionObject& new_target) override;
|
||||
|
||||
private:
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
};
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/DurationFormatPrototype.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
||||
// 1.4 Properties of the Intl.DurationFormat Prototype Object, https://tc39.es/proposal-intl-duration-format/#sec-properties-of-intl-durationformat-prototype-object
|
||||
DurationFormatPrototype::DurationFormatPrototype(GlobalObject& global_object)
|
||||
: PrototypeObject(*global_object.object_prototype())
|
||||
{
|
||||
}
|
||||
|
||||
void DurationFormatPrototype::initialize(GlobalObject& global_object)
|
||||
{
|
||||
Object::initialize(global_object);
|
||||
|
||||
auto& vm = this->vm();
|
||||
|
||||
// 1.4.2 Intl.DurationFormat.prototype [ @@toStringTag ], https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.prototype-@@tostringtag
|
||||
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.DurationFormat"), Attribute::Configurable);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Runtime/Intl/DurationFormat.h>
|
||||
#include <LibJS/Runtime/PrototypeObject.h>
|
||||
|
||||
namespace JS::Intl {
|
||||
|
||||
class DurationFormatPrototype final : public PrototypeObject<DurationFormatPrototype, DurationFormat> {
|
||||
JS_PROTOTYPE_OBJECT(DurationFormatPrototype, DurationFormat, Intl.DurationFormat);
|
||||
|
||||
public:
|
||||
explicit DurationFormatPrototype(GlobalObject&);
|
||||
virtual void initialize(GlobalObject&) override;
|
||||
virtual ~DurationFormatPrototype() override = default;
|
||||
};
|
||||
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
#include <LibJS/Runtime/Intl/CollatorConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/Intl.h>
|
||||
#include <LibJS/Runtime/Intl/ListFormatConstructor.h>
|
||||
#include <LibJS/Runtime/Intl/LocaleConstructor.h>
|
||||
@ -45,6 +46,7 @@ void Intl::initialize(GlobalObject& global_object)
|
||||
define_direct_property(vm.names.Collator, global_object.intl_collator_constructor(), attr);
|
||||
define_direct_property(vm.names.DateTimeFormat, global_object.intl_date_time_format_constructor(), attr);
|
||||
define_direct_property(vm.names.DisplayNames, global_object.intl_display_names_constructor(), attr);
|
||||
define_direct_property(vm.names.DurationFormat, global_object.intl_duration_format_constructor(), attr);
|
||||
define_direct_property(vm.names.ListFormat, global_object.intl_list_format_constructor(), attr);
|
||||
define_direct_property(vm.names.Locale, global_object.intl_locale_constructor(), attr);
|
||||
define_direct_property(vm.names.NumberFormat, global_object.intl_number_format_constructor(), attr);
|
||||
|
@ -0,0 +1,3 @@
|
||||
test("basic functionality", () => {
|
||||
expect(Intl.DurationFormat.prototype[Symbol.toStringTag]).toBe("Intl.DurationFormat");
|
||||
});
|
@ -0,0 +1,389 @@
|
||||
describe("errors", () => {
|
||||
test("called without new", () => {
|
||||
expect(() => {
|
||||
Intl.DurationFormat();
|
||||
}).toThrowWithMessage(
|
||||
TypeError,
|
||||
"Intl.DurationFormat constructor must be called with 'new'"
|
||||
);
|
||||
});
|
||||
|
||||
test("structurally invalid tag", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("root");
|
||||
}).toThrowWithMessage(RangeError, "root is not a structurally valid language tag");
|
||||
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en-");
|
||||
}).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
|
||||
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("Latn");
|
||||
}).toThrowWithMessage(RangeError, "Latn is not a structurally valid language tag");
|
||||
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en-u-aa-U-aa");
|
||||
}).toThrowWithMessage(RangeError, "en-u-aa-U-aa is not a structurally valid language tag");
|
||||
});
|
||||
|
||||
test("options is an invalid type", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", null);
|
||||
}).toThrowWithMessage(TypeError, "Options is not an object");
|
||||
});
|
||||
|
||||
test("localeMatcher option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { localeMatcher: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
|
||||
});
|
||||
|
||||
test("style option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option style");
|
||||
});
|
||||
|
||||
test("years option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { years: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option years");
|
||||
});
|
||||
|
||||
test("yearsDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { yearsDisplay: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option yearsDisplay");
|
||||
});
|
||||
|
||||
test("months option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { months: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option months");
|
||||
});
|
||||
|
||||
test("monthsDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { monthsDisplay: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option monthsDisplay");
|
||||
});
|
||||
|
||||
test("weeks option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { weeks: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option weeks");
|
||||
});
|
||||
|
||||
test("weeksDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { weeksDisplay: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option weeksDisplay");
|
||||
});
|
||||
|
||||
test("days option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { days: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option days");
|
||||
});
|
||||
|
||||
test("daysDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { daysDisplay: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option daysDisplay");
|
||||
});
|
||||
|
||||
test("hours option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { hours: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option hours");
|
||||
});
|
||||
|
||||
test("hoursDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { hoursDisplay: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option hoursDisplay");
|
||||
});
|
||||
|
||||
test("minutes option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { minutes: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option minutes");
|
||||
});
|
||||
|
||||
test("minutesDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { minutesDisplay: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option minutesDisplay");
|
||||
});
|
||||
|
||||
test("seconds option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { seconds: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option seconds");
|
||||
});
|
||||
|
||||
test("secondsDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { secondsDisplay: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option secondsDisplay");
|
||||
});
|
||||
|
||||
test("milliseconds option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { milliseconds: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option milliseconds");
|
||||
});
|
||||
|
||||
test("millisecondsDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { millisecondsDisplay: "hello!" });
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"hello! is not a valid value for option millisecondsDisplay"
|
||||
);
|
||||
});
|
||||
|
||||
test("microseconds option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { microseconds: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option microseconds");
|
||||
});
|
||||
|
||||
test("microsecondsDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { microsecondsDisplay: "hello!" });
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"hello! is not a valid value for option microsecondsDisplay"
|
||||
);
|
||||
});
|
||||
|
||||
test("nanoseconds option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { nanoseconds: "hello!" });
|
||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option nanoseconds");
|
||||
});
|
||||
|
||||
test("nanosecondsDisplay option is invalid", () => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { nanosecondsDisplay: "hello!" });
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"hello! is not a valid value for option nanosecondsDisplay"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Intl.DurationFormat).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("all valid localeMatcher options", () => {
|
||||
["lookup", "best fit"].forEach(localeMatcher => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { localeMatcher: localeMatcher });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid style options", () => {
|
||||
["long", "short", "narrow", "digital"].forEach(style => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: style });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid years options", () => {
|
||||
["long", "short", "narrow"].forEach(years => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { years: years });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid yearsDisplay options", () => {
|
||||
["always", "auto"].forEach(yearsDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { yearsDisplay: yearsDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid months options", () => {
|
||||
["long", "short", "narrow"].forEach(months => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { months: months });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid monthsDisplay options", () => {
|
||||
["always", "auto"].forEach(monthsDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { monthsDisplay: monthsDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid weeks options", () => {
|
||||
["long", "short", "narrow"].forEach(weeks => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { weeks: weeks });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid weeksDisplay options", () => {
|
||||
["always", "auto"].forEach(weeksDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { weeksDisplay: weeksDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid days options", () => {
|
||||
["long", "short", "narrow"].forEach(days => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { days: days });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid daysDisplay options", () => {
|
||||
["always", "auto"].forEach(daysDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { daysDisplay: daysDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid hours options", () => {
|
||||
["long", "short", "narrow"].forEach(hours => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { hours: hours });
|
||||
}).not.toThrow();
|
||||
});
|
||||
["numeric", "2-digit"].forEach(seconds => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: "digital", hours: seconds });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid hoursDisplay options", () => {
|
||||
["always", "auto"].forEach(hoursDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { hoursDisplay: hoursDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid minutes options", () => {
|
||||
["long", "short", "narrow"].forEach(minutes => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { minutes: minutes });
|
||||
}).not.toThrow();
|
||||
});
|
||||
["numeric", "2-digit"].forEach(seconds => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: "digital", minutes: seconds });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid minutesDisplay options", () => {
|
||||
["always", "auto"].forEach(minutesDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { minutesDisplay: minutesDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid seconds options", () => {
|
||||
["long", "short", "narrow"].forEach(seconds => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { seconds: seconds });
|
||||
}).not.toThrow();
|
||||
});
|
||||
["numeric", "2-digit"].forEach(seconds => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: "digital", seconds: seconds });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid secondsDisplay options", () => {
|
||||
["always", "auto"].forEach(secondsDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { secondsDisplay: secondsDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid milliseconds options", () => {
|
||||
["long", "short", "narrow"].forEach(milliseconds => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { milliseconds: milliseconds });
|
||||
}).not.toThrow();
|
||||
});
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: "digital", milliseconds: "numeric" });
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
test("all valid millisecondsDisplay options", () => {
|
||||
["always", "auto"].forEach(millisecondsDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { millisecondsDisplay: millisecondsDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid microseconds options", () => {
|
||||
["long", "short", "narrow"].forEach(microseconds => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { microseconds: microseconds });
|
||||
}).not.toThrow();
|
||||
});
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: "digital", microseconds: "numeric" });
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
test("all valid microsecondsDisplay options", () => {
|
||||
["always", "auto"].forEach(microsecondsDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { microsecondsDisplay: microsecondsDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid nanoseconds options", () => {
|
||||
["long", "short", "narrow", "numeric"].forEach(nanoseconds => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { nanoseconds: nanoseconds });
|
||||
}).not.toThrow();
|
||||
});
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { style: "digital", nanoseconds: "numeric" });
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
test("all valid nanosecondsDisplay options", () => {
|
||||
["always", "auto"].forEach(nanosecondsDisplay => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { nanosecondsDisplay: nanosecondsDisplay });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
test("all valid fractionalDigits options", () => {
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(fractionalDigits => {
|
||||
expect(() => {
|
||||
new Intl.DurationFormat("en", { fractionalDigits: fractionalDigits });
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user