summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-23 23:23:53 +0100
committerAndreas Kling <kling@serenityos.org>2021-08-24 01:53:30 +0200
commit5573efa3bd5ac31ad65e581816babe911ebec112 (patch)
tree527af8073c21740d5910341566da498adbeea313 /Userland
parented5ae7b093521cb47d09623fc149988c7963c6f9 (diff)
downloadserenity-5573efa3bd5ac31ad65e581816babe911ebec112.zip
LibJS: Implement Temporal.PlainDateTime.prototype.toPlainYearMonth()
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp28
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainYearMonth.js13
3 files changed, 42 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
index b29c1d78a9..4b595744ff 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp
@@ -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/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainDateTime.h>
@@ -54,6 +55,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
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.toPlainTime, to_plain_time, 0, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
}
@@ -416,6 +418,32 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_date)
return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar());
}
+// 5.3.38 Temporal.PlainDateTime.prototype.toPlainYearMonth ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplainyearmonth
+JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_year_month)
+{
+ // 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, « "monthCode", "year" »).
+ auto field_names = calendar_fields(global_object, calendar, { "monthCode"sv, "year"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 ? YearMonthFromFields(calendar, fields).
+ return year_month_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 ee541c62ba..4d377cdf04 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h
@@ -42,6 +42,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
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_time);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainYearMonth.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainYearMonth.js
new file mode 100644
index 0000000000..023470357d
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.toPlainYearMonth.js
@@ -0,0 +1,13 @@
+describe("correct behavior", () => {
+ test("length is 0", () => {
+ expect(Temporal.PlainDateTime.prototype.toPlainYearMonth).toHaveLength(0);
+ });
+
+ test("basic functionality", () => {
+ const plainDateTime = new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47);
+ const plainYearMonth = plainDateTime.toPlainYearMonth();
+ expect(plainYearMonth.calendar).toBe(plainDateTime.calendar);
+ expect(plainYearMonth.year).toBe(2021);
+ expect(plainYearMonth.month).toBe(7);
+ });
+});