1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:47:35 +00:00

LibJS: Port format_calendar_annotation() to String

This commit is contained in:
Linus Groh 2023-01-26 15:27:53 +00:00
parent 269f3c4105
commit bcca5efd5a
4 changed files with 8 additions and 8 deletions

View file

@ -578,27 +578,27 @@ ThrowCompletionOr<String> maybe_format_calendar_annotation(VM& vm, Object const*
auto calendar_id = TRY(Value(calendar_object).to_string(vm));
// 4. Return FormatCalendarAnnotation(calendarID, showCalendar).
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(format_calendar_annotation(calendar_id, show_calendar)));
return format_calendar_annotation(vm, calendar_id, show_calendar);
}
// 12.2.28 FormatCalendarAnnotation ( id, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-formatcalendarannotation
DeprecatedString format_calendar_annotation(StringView id, StringView show_calendar)
ThrowCompletionOr<String> format_calendar_annotation(VM& vm, StringView id, StringView show_calendar)
{
VERIFY(show_calendar == "auto"sv || show_calendar == "always"sv || show_calendar == "never"sv || show_calendar == "critical"sv);
// 1. If showCalendar is "never", return the empty String.
if (show_calendar == "never"sv)
return DeprecatedString::empty();
return String {};
// 2. If showCalendar is "auto" and id is "iso8601", return the empty String.
if (show_calendar == "auto"sv && id == "iso8601"sv)
return DeprecatedString::empty();
return String {};
// 3. If showCalendar is "critical", let flag be "!"; else, let flag be the empty String.
auto flag = show_calendar == "critical"sv ? "!"sv : ""sv;
// 4. Return the string-concatenation of "[", flag, "u-ca=", id, and "]".
return DeprecatedString::formatted("[{}u-ca={}]", flag, id);
return TRY_OR_THROW_OOM(vm, String::formatted("[{}u-ca={}]", flag, id));
}
// 12.2.29 CalendarEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-calendarequals

View file

@ -68,7 +68,7 @@ ThrowCompletionOr<PlainDate*> calendar_date_from_fields(VM&, Object& calendar, O
ThrowCompletionOr<PlainYearMonth*> calendar_year_month_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
ThrowCompletionOr<PlainMonthDay*> calendar_month_day_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
ThrowCompletionOr<String> maybe_format_calendar_annotation(VM&, Object const* calendar_object, StringView show_calendar);
DeprecatedString format_calendar_annotation(StringView id, StringView show_calendar);
ThrowCompletionOr<String> format_calendar_annotation(VM&, StringView id, StringView show_calendar);
ThrowCompletionOr<bool> calendar_equals(VM&, Object& one, Object& two);
ThrowCompletionOr<Object*> consolidate_calendars(VM&, Object& one, Object& two);
u8 iso_days_in_month(i32 year, u8 month);

View file

@ -195,7 +195,7 @@ ThrowCompletionOr<DeprecatedString> temporal_month_day_to_string(VM& vm, PlainMo
}
// 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar).
auto calendar_string = format_calendar_annotation(calendar_id, show_calendar);
auto calendar_string = MUST_OR_THROW_OOM(format_calendar_annotation(vm, calendar_id, show_calendar));
// 9. Set result to the string-concatenation of result and calendarString.
// 10. Return result.

View file

@ -222,7 +222,7 @@ ThrowCompletionOr<DeprecatedString> temporal_year_month_to_string(VM& vm, PlainY
}
// 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar).
auto calendar_string = format_calendar_annotation(calendar_id, show_calendar);
auto calendar_string = MUST_OR_THROW_OOM(format_calendar_annotation(vm, calendar_id, show_calendar));
// 9. Set result to the string-concatenation of result and calendarString.
// 10. Return result.