mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibJS: Port temporal_duration_to_string() to String
This commit is contained in:
parent
49b5d55b95
commit
f669d2e558
Notes:
sideshowbarker
2024-07-17 02:22:23 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/f669d2e558 Pull-request: https://github.com/SerenityOS/serenity/pull/17186 Reviewed-by: https://github.com/trflynn89 ✅
3 changed files with 11 additions and 12 deletions
|
@ -1655,7 +1655,7 @@ ThrowCompletionOr<DurationRecord> adjust_rounded_duration_days(VM& vm, double ye
|
|||
}
|
||||
|
||||
// 7.5.27 TemporalDurationToString ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, precision ), https://tc39.es/proposal-temporal/#sec-temporal-temporaldurationtostring
|
||||
DeprecatedString temporal_duration_to_string(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Variant<StringView, u8> const& precision)
|
||||
ThrowCompletionOr<String> temporal_duration_to_string(VM& vm, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Variant<StringView, u8> const& precision)
|
||||
{
|
||||
if (precision.has<StringView>())
|
||||
VERIFY(precision.get<StringView>() == "auto"sv);
|
||||
|
@ -1736,24 +1736,23 @@ DeprecatedString temporal_duration_to_string(double years, double months, double
|
|||
|
||||
// b. Let decimalPart be ToZeroPaddedDecimalString(fraction, 9).
|
||||
// NOTE: padding with zeros leads to weird results when applied to a double. Not sure if that's a bug in AK/Format.h or if I'm doing this wrong.
|
||||
auto decimal_part = DeprecatedString::formatted("{:09}", (u64)fraction);
|
||||
auto decimal_part_string = TRY_OR_THROW_OOM(vm, String::formatted("{:09}", (u64)fraction));
|
||||
StringView decimal_part;
|
||||
|
||||
// c. If precision is "auto", then
|
||||
if (precision.has<StringView>() && precision.get<StringView>() == "auto"sv) {
|
||||
// i. Set decimalPart to the longest possible substring of decimalPart starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
|
||||
// NOTE: trim() would keep the left-most 0.
|
||||
while (decimal_part.ends_with('0'))
|
||||
decimal_part = decimal_part.substring(0, decimal_part.length() - 1);
|
||||
decimal_part = decimal_part_string.bytes_as_string_view().trim("0"sv, TrimMode::Right);
|
||||
}
|
||||
// d. Else if precision = 0, then
|
||||
else if (precision.get<u8>() == 0) {
|
||||
// i. Set decimalPart to "".
|
||||
decimal_part = DeprecatedString::empty();
|
||||
decimal_part = ""sv;
|
||||
}
|
||||
// e. Else,
|
||||
else {
|
||||
// i. Set decimalPart to the substring of decimalPart from 0 to precision.
|
||||
decimal_part = decimal_part.substring(0, precision.get<u8>());
|
||||
decimal_part = decimal_part_string.bytes_as_string_view().substring_view(0, precision.get<u8>());
|
||||
}
|
||||
|
||||
// f. Let secondsPart be abs(seconds) formatted as a decimal number.
|
||||
|
@ -1789,7 +1788,7 @@ DeprecatedString temporal_duration_to_string(double years, double months, double
|
|||
}
|
||||
|
||||
// 20. Return result.
|
||||
return result.to_deprecated_string();
|
||||
return TRY_OR_THROW_OOM(vm, result.to_string());
|
||||
}
|
||||
|
||||
// 7.5.28 AddDurationToOrSubtractDurationFromDuration ( operation, duration, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtoorsubtractdurationfromduration
|
||||
|
|
|
@ -140,7 +140,7 @@ ThrowCompletionOr<MoveRelativeDateResult> move_relative_date(VM&, Object& calend
|
|||
ThrowCompletionOr<ZonedDateTime*> move_relative_zoned_date_time(VM&, ZonedDateTime&, double years, double months, double weeks, double days);
|
||||
ThrowCompletionOr<RoundedDuration> round_duration(VM&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object = nullptr);
|
||||
ThrowCompletionOr<DurationRecord> adjust_rounded_duration_days(VM&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, u32 increment, StringView unit, StringView rounding_mode, Object* relative_to_object);
|
||||
DeprecatedString temporal_duration_to_string(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Variant<StringView, u8> const& precision);
|
||||
ThrowCompletionOr<String> temporal_duration_to_string(VM&, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Variant<StringView, u8> const& precision);
|
||||
ThrowCompletionOr<Duration*> add_duration_to_or_subtract_duration_from_duration(VM&, ArithmeticOperation, Duration const&, Value other_value, Value options_value);
|
||||
|
||||
// 7.5.22 DaysUntil ( earlier, later ), https://tc39.es/proposal-temporal/#sec-temporal-daysuntil
|
||||
|
|
|
@ -586,7 +586,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_string)
|
|||
auto result = TRY(round_duration(vm, duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), precision.increment, precision.unit, rounding_mode)).duration_record;
|
||||
|
||||
// 8. Return ! TemporalDurationToString(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]], precision.[[Precision]]).
|
||||
return PrimitiveString::create(vm, temporal_duration_to_string(result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds, precision.precision));
|
||||
return PrimitiveString::create(vm, MUST_OR_THROW_OOM(temporal_duration_to_string(vm, result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds, precision.precision)));
|
||||
}
|
||||
|
||||
// 7.3.23 Temporal.Duration.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tojson
|
||||
|
@ -597,7 +597,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_json)
|
|||
auto* duration = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ! TemporalDurationToString(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "auto").
|
||||
return PrimitiveString::create(vm, temporal_duration_to_string(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), "auto"sv));
|
||||
return PrimitiveString::create(vm, MUST_OR_THROW_OOM(temporal_duration_to_string(vm, duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), "auto"sv)));
|
||||
}
|
||||
|
||||
// 7.3.24 Temporal.Duration.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tolocalestring
|
||||
|
@ -609,7 +609,7 @@ JS_DEFINE_NATIVE_FUNCTION(DurationPrototype::to_locale_string)
|
|||
auto* duration = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return ! TemporalDurationToString(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], "auto").
|
||||
return PrimitiveString::create(vm, temporal_duration_to_string(duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), "auto"sv));
|
||||
return PrimitiveString::create(vm, MUST_OR_THROW_OOM(temporal_duration_to_string(vm, duration->years(), duration->months(), duration->weeks(), duration->days(), duration->hours(), duration->minutes(), duration->seconds(), duration->milliseconds(), duration->microseconds(), duration->nanoseconds(), "auto"sv)));
|
||||
}
|
||||
|
||||
// 7.3.25 Temporal.Duration.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof
|
||||
|
|
Loading…
Reference in a new issue