LibJS: Port temporal_year_month_to_string() to String

This commit is contained in:
Linus Groh 2023-01-26 16:00:20 +00:00
parent cfb4b44691
commit ae98eddc9f
Notes: sideshowbarker 2024-07-17 01:11:42 +09:00
2 changed files with 7 additions and 7 deletions

View File

@ -201,7 +201,7 @@ ThrowCompletionOr<PlainYearMonth*> create_temporal_year_month(VM& vm, i32 iso_ye
}
// 9.5.6 TemporalYearMonthToString ( yearMonth, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalyearmonthtostring
ThrowCompletionOr<DeprecatedString> temporal_year_month_to_string(VM& vm, PlainYearMonth& year_month, StringView show_calendar)
ThrowCompletionOr<String> temporal_year_month_to_string(VM& vm, PlainYearMonth& year_month, StringView show_calendar)
{
// 1. Assert: Type(yearMonth) is Object.
// 2. Assert: yearMonth has an [[InitializedTemporalYearMonth]] internal slot.
@ -209,16 +209,16 @@ ThrowCompletionOr<DeprecatedString> temporal_year_month_to_string(VM& vm, PlainY
// 3. Let year be ! PadISOYear(yearMonth.[[ISOYear]]).
// 4. Let month be ToZeroPaddedDecimalString(yearMonth.[[ISOMonth]], 2).
// 5. Let result be the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and month.
auto result = DeprecatedString::formatted("{}-{:02}", MUST_OR_THROW_OOM(pad_iso_year(vm, year_month.iso_year())), year_month.iso_month());
auto result = TRY_OR_THROW_OOM(vm, String::formatted("{}-{:02}", MUST_OR_THROW_OOM(pad_iso_year(vm, year_month.iso_year())), year_month.iso_month()));
// 6. Let calendarID be ? ToString(yearMonth.[[Calendar]]).
auto calendar_id = TRY(Value(&year_month.calendar()).to_deprecated_string(vm));
auto calendar_id = TRY(Value(&year_month.calendar()).to_string(vm));
// 7. If showCalendar is one of "always" or "critical", or if calendarID is not "iso8601", then
if (show_calendar.is_one_of("always"sv, "critical"sv) || calendar_id != "iso8601") {
// a. Let day be ToZeroPaddedDecimalString(yearMonth.[[ISODay]], 2).
// b. Set result to the string-concatenation of result, the code unit 0x002D (HYPHEN-MINUS), and day.
result = DeprecatedString::formatted("{}-{:02}", result, year_month.iso_day());
result = TRY_OR_THROW_OOM(vm, String::formatted("{}-{:02}", result, year_month.iso_day()));
}
// 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar).
@ -226,7 +226,7 @@ ThrowCompletionOr<DeprecatedString> temporal_year_month_to_string(VM& vm, PlainY
// 9. Set result to the string-concatenation of result and calendarString.
// 10. Return result.
return DeprecatedString::formatted("{}{}", result, calendar_string);
return TRY_OR_THROW_OOM(vm, String::formatted("{}{}", result, calendar_string));
}
// 9.5.7 DifferenceTemporalPlainYearMonth ( operation, yearMonth, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplainyearmonth

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -46,7 +46,7 @@ ThrowCompletionOr<ISOYearMonth> regulate_iso_year_month(VM&, double year, double
bool iso_year_month_within_limits(i32 year, u8 month);
ISOYearMonth balance_iso_year_month(double year, double month);
ThrowCompletionOr<PlainYearMonth*> create_temporal_year_month(VM&, i32 iso_year, u8 iso_month, Object& calendar, u8 reference_iso_day, FunctionObject const* new_target = nullptr);
ThrowCompletionOr<DeprecatedString> temporal_year_month_to_string(VM&, PlainYearMonth&, StringView show_calendar);
ThrowCompletionOr<String> temporal_year_month_to_string(VM&, PlainYearMonth&, StringView show_calendar);
ThrowCompletionOr<Duration*> difference_temporal_plain_year_month(VM&, DifferenceOperation, PlainYearMonth&, Value other, Value options);
ThrowCompletionOr<PlainYearMonth*> add_duration_to_or_subtract_duration_from_plain_year_month(VM&, ArithmeticOperation, PlainYearMonth&, Value temporal_duration_like, Value options_value);