LibJS: Implement Temporal.PlainYearMonth.prototype.toString()

This commit is contained in:
Linus Groh 2021-08-19 00:43:39 +01:00
parent 89eddb5bff
commit 421ad73b4f
Notes: sideshowbarker 2024-07-18 05:27:23 +09:00
5 changed files with 116 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
@ -186,4 +187,37 @@ PlainYearMonth* create_temporal_year_month(GlobalObject& global_object, i32 iso_
return object;
}
// 9.5.8 TemporalYearMonthToString ( yearMonth, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalyearmonthtostring
Optional<String> temporal_year_month_to_string(GlobalObject& global_object, PlainYearMonth& year_month, StringView show_calendar)
{
auto& vm = global_object.vm();
// 1. Assert: Type(yearMonth) is Object.
// 2. Assert: yearMonth has an [[InitializedTemporalYearMonth]] internal slot.
// 3. Let year be ! PadISOYear(yearMonth.[[ISOYear]]).
// 4. Let month be yearMonth.[[ISOMonth]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
// 5. Let result be the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and month.
auto result = String::formatted("{}-{:02}", pad_iso_year(year_month.iso_year()), year_month.iso_month());
// 6. Let calendarID be ? ToString(yearMonth.[[Calendar]]).
auto calendar_id = Value(&year_month.calendar()).to_string(global_object);
if (vm.exception())
return {};
// 7. If calendarID is not "iso8601", then
if (calendar_id != "iso8601") {
// a. Let day be yearMonth.[[ISODay]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
// b. Set result to the string-concatenation of result, the code unit 0x002D (HYPHEN-MINUS), and day.
result = String::formatted("{}-{:02}", result, year_month.iso_day());
}
// 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar).
auto calendar_string = format_calendar_annotation(calendar_id, show_calendar);
// 9. Set result to the string-concatenation of result and calendarString.
// 10. Return result.
return String::formatted("{}{}", result, calendar_string);
}
}

View File

@ -45,5 +45,6 @@ bool iso_year_month_within_limits(i32 year, u8 month);
ISOYearMonth balance_iso_year_month(double year, double month);
ISOYearMonth constrain_iso_year_month(double year, double month);
PlainYearMonth* create_temporal_year_month(GlobalObject&, i32 iso_year, u8 iso_month, Object& calendar, u8 reference_iso_day, FunctionObject* new_target = nullptr);
Optional<String> temporal_year_month_to_string(GlobalObject&, PlainYearMonth&, StringView show_calendar);
}

View File

@ -6,6 +6,7 @@
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
#include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
@ -37,6 +38,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
}
@ -179,6 +181,33 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::in_leap_year_getter)
return Value(calendar_in_leap_year(global_object, calendar, *year_month));
}
// 9.3.17 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string)
{
// 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = typed_this(global_object);
if (vm.exception())
return {};
// 3. Set options to ? GetOptionsObject(options).
auto* options = get_options_object(global_object, vm.argument(0));
if (vm.exception())
return {};
// 4. Let showCalendar be ? ToShowCalendarOption(options).
auto show_calendar = to_show_calendar_option(global_object, *options);
if (vm.exception())
return {};
// 5. Return ? TemporalYearMonthToString(yearMonth, showCalendar).
auto string = temporal_year_month_to_string(global_object, *year_month, *show_calendar);
if (vm.exception())
return {};
return js_string(vm, *string);
}
// 9.3.20 Temporal.PlainYearMonth.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of)
{

View File

@ -27,6 +27,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_month_getter);
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
};

View File

@ -0,0 +1,51 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainYearMonth.prototype.toString).toHaveLength(0);
});
test("basic functionality", () => {
let plainYearMonth;
plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.toString()).toBe("2021-07");
expect(plainYearMonth.toString({ calendarName: "auto" })).toBe("2021-07");
expect(plainYearMonth.toString({ calendarName: "always" })).toBe("2021-07[u-ca=iso8601]");
expect(plainYearMonth.toString({ calendarName: "never" })).toBe("2021-07");
plainYearMonth = new Temporal.PlainYearMonth(2021, 7, { toString: () => "foo" }, 6);
expect(plainYearMonth.toString()).toBe("2021-07-06[u-ca=foo]");
expect(plainYearMonth.toString({ calendarName: "auto" })).toBe("2021-07-06[u-ca=foo]");
expect(plainYearMonth.toString({ calendarName: "always" })).toBe("2021-07-06[u-ca=foo]");
expect(plainYearMonth.toString({ calendarName: "never" })).toBe("2021-07-06");
plainYearMonth = new Temporal.PlainYearMonth(0, 1);
expect(plainYearMonth.toString()).toBe("+000000-01");
plainYearMonth = new Temporal.PlainYearMonth(999, 1);
expect(plainYearMonth.toString()).toBe("+000999-01");
plainYearMonth = new Temporal.PlainYearMonth(12345, 1);
expect(plainYearMonth.toString()).toBe("+012345-01");
plainYearMonth = new Temporal.PlainYearMonth(123456, 1);
expect(plainYearMonth.toString()).toBe("+123456-01");
plainYearMonth = new Temporal.PlainYearMonth(-12345, 1);
expect(plainYearMonth.toString()).toBe("-012345-01");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Temporal.PlainYearMonth.prototype.toString.call("foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth");
});
test("calendarName option must be one of 'auto', 'always', 'never'", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(() => {
plainYearMonth.toString({ calendarName: "foo" });
}).toThrowWithMessage(RangeError, "foo is not a valid value for option calendarName");
});
});