diff options
author | Linus Groh <mail@linusgroh.de> | 2021-08-16 18:13:47 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-08-16 20:40:21 +0100 |
commit | 795e077eb87d61de8d779f2e53a1622a507780e3 (patch) | |
tree | 571329cde830f2ec5a6ef38ed5f7f1d39c855def | |
parent | 31f65b8c500aa92a082653deb74fbfa11637a696 (diff) | |
download | serenity-795e077eb87d61de8d779f2e53a1622a507780e3.zip |
LibJS: Implement Temporal.PlainDate.prototype.toPlainMonthDay()
6 files changed, 76 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 017e35cc81..31a5cbaa2c 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -392,6 +392,7 @@ namespace JS { P(toLowerCase) \ P(toPlainDate) \ P(toPlainDateTime) \ + P(toPlainMonthDay) \ P(toPlainTime) \ P(toPlainYearMonth) \ P(toString) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp index 63577dfb71..655a501474 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp @@ -447,6 +447,36 @@ PlainYearMonth* year_month_from_fields(GlobalObject& global_object, Object& cale return static_cast<PlainYearMonth*>(year_month_object); } +// 12.1.26 MonthDayFromFields ( calendar, fields [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-monthdayfromfields +PlainMonthDay* month_day_from_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object* options) +{ + auto& vm = global_object.vm(); + + // 1. Assert: Type(calendar) is Object. + // 2. Assert: Type(fields) is Object. + // 3. If options is not present, then + // a. Set options to undefined. + // 4. Else, + // a. Assert: Type(options) is Object. + + // 5. Let monthDay be ? Invoke(calendar, "monthDayFromFields", « fields, options »). + auto month_day = Value(&calendar).invoke(global_object, vm.names.monthDayFromFields, &fields, options ?: js_undefined()); + if (vm.exception()) + return {}; + + // 6. Perform ? RequireInternalSlot(monthDay, [[InitializedTemporalMonthDay]]). + auto* month_day_object = month_day.to_object(global_object); + if (!month_day_object) + return {}; + if (!is<PlainMonthDay>(month_day_object)) { + vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainMonthDay"); + return {}; + } + + // 7. Return monthDay. + return static_cast<PlainMonthDay*>(month_day_object); +} + // 12.1.28 CalendarEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-calendarequals bool calendar_equals(GlobalObject& global_object, Object& one, Object& two) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h index f75b5194fb..8e6c4d1d53 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/Calendar.h @@ -51,6 +51,7 @@ Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value); Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&); PlainDate* date_from_fields(GlobalObject&, Object& calendar, Object& fields, Object& options); PlainYearMonth* year_month_from_fields(GlobalObject&, Object& calendar, Object& fields, Object* options = nullptr); +PlainMonthDay* month_day_from_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object* options = nullptr); bool calendar_equals(GlobalObject&, Object& one, Object& two); Object* consolidate_calendars(GlobalObject&, Object& one, Object& two); bool is_iso_leap_year(i32 year); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index 8183e69aea..2d4fa05d55 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -11,6 +11,7 @@ #include <LibJS/Runtime/Temporal/Calendar.h> #include <LibJS/Runtime/Temporal/PlainDate.h> #include <LibJS/Runtime/Temporal/PlainDatePrototype.h> +#include <LibJS/Runtime/Temporal/PlainMonthDay.h> #include <LibJS/Runtime/Temporal/PlainYearMonth.h> namespace JS::Temporal { @@ -46,6 +47,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr); + define_native_function(vm.names.toPlainMonthDay, to_plain_month_day, 0, attr); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); define_native_function(vm.names.withCalendar, with_calendar, 1, attr); define_native_function(vm.names.equals, equals, 1, attr); @@ -296,6 +298,32 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month) return year_month_from_fields(global_object, calendar, *fields); } +// 3.3.17 Temporal.PlainDate.prototype.toPlainMonthDay ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplainmonthday +JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_month_day) +{ + // 1. Let temporalDate be the this value. + // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]). + auto* temporal_date = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be temporalDate.[[Calendar]]. + auto& calendar = temporal_date->calendar(); + + // 4. Let fieldNames be ? CalendarFields(calendar, « "day", "monthCode" »). + auto field_names = calendar_fields(global_object, calendar, { "day"sv, "monthCode"sv }); + if (vm.exception()) + return {}; + + // 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»). + auto* fields = prepare_temporal_fields(global_object, *temporal_date, field_names, {}); + if (vm.exception()) + return {}; + + // 6. Return ? MonthDayFromFields(calendar, fields). + return month_day_from_fields(global_object, calendar, *fields); +} + // 3.3.18 Temporal.PlainDate.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.getisofields JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h index b5cc772fb8..a5b3eaf3e1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h @@ -33,6 +33,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter); JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter); JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month); + JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); JS_DECLARE_NATIVE_FUNCTION(with_calendar); JS_DECLARE_NATIVE_FUNCTION(equals); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toPlainMonthDay.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toPlainMonthDay.js new file mode 100644 index 0000000000..4dbbebb24a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.toPlainMonthDay.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainDate.prototype.toPlainMonthDay).toHaveLength(0); + }); + + test("basic functionality", () => { + const plainDate = new Temporal.PlainDate(2021, 7, 6); + const plainMonthDay = plainDate.toPlainMonthDay(); + expect(plainMonthDay.calendar).toBe(plainDate.calendar); + expect(plainMonthDay.monthCode).toBe("M07"); + expect(plainMonthDay.day).toBe(6); + const fields = plainMonthDay.getISOFields(); + expect(fields.isoYear).toBe(1972); + }); +}); |