Browse Source

LibJS: Use String::join and String::trim in Temporal AOs

Timothy Flynn 2 years ago
parent
commit
9e4dbea0ee

+ 2 - 4
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -853,8 +853,7 @@ ThrowCompletionOr<String> format_seconds_string_part(VM& vm, u8 second, u16 mill
         fraction_string = TRY_OR_THROW_OOM(vm, String::formatted("{:09}", fraction));
 
         // c. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
-        // FIXME: Add String::trim()
-        fraction_string = TRY_OR_THROW_OOM(vm, String::from_utf8(fraction_string.bytes_as_string_view().trim("0"sv, TrimMode::Right)));
+        fraction_string = TRY_OR_THROW_OOM(vm, fraction_string.trim("0"sv, TrimMode::Right));
     }
     // 6. Else,
     else {
@@ -1806,8 +1805,7 @@ ThrowCompletionOr<Object*> prepare_temporal_fields(VM& vm, Object const& fields,
     // 4. If requiredFields is partial and any is false, then
     if (required_fields.has<PrepareTemporalFieldsPartial>() && !any) {
         // a. Throw a TypeError exception.
-        // FIXME: Add & use String::join()
-        return vm.throw_completion<TypeError>(ErrorType::TemporalObjectMustHaveOneOf, DeprecatedString::join(", "sv, field_names));
+        return vm.throw_completion<TypeError>(ErrorType::TemporalObjectMustHaveOneOf, TRY_OR_THROW_OOM(vm, String::join(", "sv, field_names)));
     }
 
     // 5. Return result.

+ 5 - 2
Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp

@@ -202,10 +202,13 @@ ThrowCompletionOr<String> format_time_zone_offset_string(VM& vm, double offset_n
     // 11. If nanoseconds ≠ 0, then
     if (nanoseconds != 0) {
         // a. Let fraction be ToZeroPaddedDecimalString(nanoseconds, 9).
+        auto fraction = TRY_OR_THROW_OOM(vm, String::formatted("{:09}", nanoseconds));
+
         // b. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
+        fraction = TRY_OR_THROW_OOM(vm, fraction.trim("0"sv, TrimMode::Right));
+
         // c. Let post be the string-concatenation of the code unit 0x003A (COLON), s, the code unit 0x002E (FULL STOP), and fraction.
-        // FIXME: Add String::trim()
-        builder.appendff(":{:02}.{}", seconds, TRY_OR_THROW_OOM(vm, String::from_utf8(TRY_OR_THROW_OOM(vm, String::formatted("{:09}", nanoseconds)).bytes_as_string_view().trim("0"sv, TrimMode::Right))));
+        builder.appendff(":{:02}.{}", seconds, fraction);
     }
     // 12. Else if seconds ≠ 0, then
     else if (seconds != 0) {