diff options
author | Linus Groh <mail@linusgroh.de> | 2021-08-23 23:27:41 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-24 01:53:30 +0200 |
commit | fc58f9373497573418e2311ddba65128b1d5ae6b (patch) | |
tree | 86a337afd7bd34c818e146a6398ba3c861c04a32 | |
parent | 5573efa3bd5ac31ad65e581816babe911ebec112 (diff) | |
download | serenity-fc58f9373497573418e2311ddba65128b1d5ae6b.zip |
LibJS: Implement Temporal.PlainDateTime.prototype.toPlainMonthDay()
3 files changed, 41 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 4b595744ff..fcd3ed8cde 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -56,6 +56,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.valueOf, value_of, 0, attr); define_native_function(vm.names.toPlainDate, to_plain_date, 0, attr); 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.toPlainTime, to_plain_time, 0, attr); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); } @@ -444,6 +445,32 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_year_month) return year_month_from_fields(global_object, calendar, *fields); } +// 5.3.39 Temporal.PlainDateTime.prototype.toPlainMonthDay ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplainmonthday +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_month_day) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto* date_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Let calendar be dateTime.[[Calendar]]. + auto& calendar = date_time->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(dateTime, fieldNames, «»). + auto* fields = prepare_temporal_fields(global_object, *date_time, field_names, {}); + if (vm.exception()) + return {}; + + // 6. Return ? MonthDayFromFields(calendar, fields). + return month_day_from_fields(global_object, calendar, *fields); +} + // 5.3.40 Temporal.PlainDateTime.prototype.toPlainTime ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplaintime JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_time) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 4d377cdf04..ed64b8fad6 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -43,6 +43,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(to_plain_date); JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month); + JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day); JS_DECLARE_NATIVE_FUNCTION(to_plain_time); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainMonthDay.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainMonthDay.js new file mode 100644 index 0000000000..6b52cdbd4f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainMonthDay.js @@ -0,0 +1,13 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainDateTime.prototype.toPlainMonthDay).toHaveLength(0); + }); + + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47); + const plainMonthDay = plainDateTime.toPlainMonthDay(); + expect(plainMonthDay.calendar).toBe(plainDateTime.calendar); + expect(plainMonthDay.monthCode).toBe("M07"); + expect(plainMonthDay.day).toBe(6); + }); +}); |