فهرست منبع

LibJS: Move IsValidDurationRecord check to ToDurationRecord

This is an editorial change in the Intl.DurationFormat proposal. See:
https://github.com/tc39/proposal-intl-duration-format/commit/fb21723
Timothy Flynn 2 سال پیش
والد
کامیت
85f079dc6b

+ 5 - 1
Userland/Libraries/LibJS/Runtime/Intl/DurationFormat.cpp

@@ -173,7 +173,11 @@ ThrowCompletionOr<Temporal::DurationRecord> to_duration_record(VM& vm, Value inp
     if (!any)
         return vm.throw_completion<TypeError>(ErrorType::TemporalInvalidDurationLikeObject);
 
-    // 6. Return result.
+    // 6. If IsValidDurationRecord(result) is false, throw a RangeError exception.
+    if (!is_valid_duration_record(result))
+        return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidDurationLikeObject);
+
+    // 7. Return result.
     return result;
 }
 

+ 9 - 17
Userland/Libraries/LibJS/Runtime/Intl/DurationFormatPrototype.cpp

@@ -42,23 +42,19 @@ JS_DEFINE_NATIVE_FUNCTION(DurationFormatPrototype::format)
     // 3. Let record be ? ToDurationRecord(duration).
     auto record = TRY(to_duration_record(vm, vm.argument(0)));
 
-    // 4. If IsValidDurationRecord(record) is false, throw a RangeError exception.
-    if (!is_valid_duration_record(record))
-        return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidDurationLikeObject);
-
-    // 5. Let parts be PartitionDurationFormatPattern(df, record).
+    // 4. Let parts be PartitionDurationFormatPattern(df, record).
     auto parts = partition_duration_format_pattern(vm, *duration_format, record);
 
-    // 6. Let result be a new empty String.
+    // 5. Let result be a new empty String.
     StringBuilder result;
 
-    // 7. For each Record { [[Type]], [[Value]] } part in parts, do
+    // 6. For each Record { [[Type]], [[Value]] } part in parts, do
     for (auto const& part : parts) {
         // a. Set result to the string-concatenation of result and part.[[Value]].
         result.append(part.value);
     }
 
-    // 8. Return result.
+    // 7. Return result.
     return PrimitiveString::create(vm, result.build());
 }
 
@@ -74,18 +70,14 @@ JS_DEFINE_NATIVE_FUNCTION(DurationFormatPrototype::format_to_parts)
     // 3. Let record be ? ToDurationRecord(duration).
     auto record = TRY(to_duration_record(vm, vm.argument(0)));
 
-    // 4. If IsValidDurationRecord(record) is false, throw a RangeError exception.
-    if (!is_valid_duration_record(record))
-        return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidDurationLikeObject);
-
-    // 5. Let parts be PartitionDurationFormatPattern(df, record).
+    // 4. Let parts be PartitionDurationFormatPattern(df, record).
     auto parts = partition_duration_format_pattern(vm, *duration_format, record);
 
-    // 6. Let result be ! ArrayCreate(0).
+    // 5. Let result be ! ArrayCreate(0).
     auto result = MUST(Array::create(realm, 0));
 
-    // 7. Let n be 0.
-    // 8. For each { [[Type]], [[Value]] } part in parts, do
+    // 6. Let n be 0.
+    // 7. For each { [[Type]], [[Value]] } part in parts, do
     for (size_t n = 0; n < parts.size(); ++n) {
         auto const& part = parts[n];
 
@@ -104,7 +96,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationFormatPrototype::format_to_parts)
         // e. Increment n by 1.
     }
 
-    // 9. Return result.
+    // 8. Return result.
     return result;
 }