diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp index 5961344baf..cda244512e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.cpp @@ -87,25 +87,8 @@ PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_ // 3. Let any be false. auto any = false; - struct PartialDurationProperty { - Optional PartialDuration::*internal_slot { nullptr }; - PropertyName property; - }; - auto properties = AK::Array { - PartialDurationProperty { &PartialDuration::years, vm.names.years }, - PartialDurationProperty { &PartialDuration::months, vm.names.months }, - PartialDurationProperty { &PartialDuration::weeks, vm.names.weeks }, - PartialDurationProperty { &PartialDuration::days, vm.names.days }, - PartialDurationProperty { &PartialDuration::hours, vm.names.hours }, - PartialDurationProperty { &PartialDuration::minutes, vm.names.minutes }, - PartialDurationProperty { &PartialDuration::seconds, vm.names.seconds }, - PartialDurationProperty { &PartialDuration::milliseconds, vm.names.milliseconds }, - PartialDurationProperty { &PartialDuration::microseconds, vm.names.microseconds }, - PartialDurationProperty { &PartialDuration::nanoseconds, vm.names.nanoseconds }, - }; - // 4. For each row of Table 7, except the header row, in table order, do - for (auto& [internal_slot, property] : properties) { + for (auto& [internal_slot, property] : temporal_duration_like_properties>(vm)) { // a. Let property be the Property value of the current row. // b. Let value be ? Get(temporalDurationLike, property). diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h index 75e8b86879..d492ea3940 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Duration.h @@ -57,6 +57,31 @@ struct PartialDuration { Optional nanoseconds; }; +// Table 7: Properties of a TemporalDurationLike, https://tc39.es/proposal-temporal/#table-temporal-temporaldurationlike-properties + +template +struct TemporalDurationLikeProperty { + ValueT StructT::*internal_slot { nullptr }; + PropertyName property; +}; + +template +auto temporal_duration_like_properties = [](VM& vm) { + using PropertyT = TemporalDurationLikeProperty; + return AK::Array { + PropertyT { &StructT::days, vm.names.days }, + PropertyT { &StructT::hours, vm.names.hours }, + PropertyT { &StructT::microseconds, vm.names.microseconds }, + PropertyT { &StructT::milliseconds, vm.names.milliseconds }, + PropertyT { &StructT::minutes, vm.names.minutes }, + PropertyT { &StructT::months, vm.names.months }, + PropertyT { &StructT::nanoseconds, vm.names.nanoseconds }, + PropertyT { &StructT::seconds, vm.names.seconds }, + PropertyT { &StructT::weeks, vm.names.weeks }, + PropertyT { &StructT::years, vm.names.years }, + }; +}; + i8 duration_sign(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds); bool is_valid_duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds); PartialDuration to_partial_duration(GlobalObject&, Value temporal_duration_like);