From d4e9d572f5d43edf7cbe0073d15f2ecc91c4b561 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 30 Jul 2021 00:17:41 +0300 Subject: [PATCH] LibJS: Implement Temporal.PlainDateTime.prototype.weekOfYear --- .../Runtime/Temporal/PlainDateTimePrototype.cpp | 17 +++++++++++++++++ .../Runtime/Temporal/PlainDateTimePrototype.h | 1 + .../PlainDateTime.prototype.weekOfYear.js | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.weekOfYear.js diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 21833a2410..0a71918962 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -40,6 +40,7 @@ void PlainDateTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.nanosecond, nanosecond_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.dayOfWeek, day_of_week_getter, {}, Attribute::Configurable); define_native_accessor(vm.names.dayOfYear, day_of_year_getter, {}, Attribute::Configurable); + define_native_accessor(vm.names.weekOfYear, week_of_year_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.valueOf, value_of, 0, attr); @@ -247,6 +248,22 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::day_of_year_getter) return calendar_day_of_year(global_object, calendar, *date_time); } +// 5.3.16 get Temporal.PlainDateTime.prototype.weekOfYear, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.weekofyear +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::week_of_year_getter) +{ + // 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. Return ? CalendarWeekOfYear(calendar, dateTime). + return calendar_week_of_year(global_object, calendar, *date_time); +} + // 5.3.35 Temporal.PlainDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.valueof JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::value_of) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 7da50c5cf5..0d33bc645c 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -32,6 +32,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter); JS_DECLARE_NATIVE_FUNCTION(day_of_week_getter); JS_DECLARE_NATIVE_FUNCTION(day_of_year_getter); + JS_DECLARE_NATIVE_FUNCTION(week_of_year_getter); JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(to_plain_date); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.weekOfYear.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.weekOfYear.js new file mode 100644 index 0000000000..2315bc26fe --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.weekOfYear.js @@ -0,0 +1,14 @@ +describe("correct behavior", () => { + test("basic functionality", () => { + const plainDateTime = new Temporal.PlainDateTime(2021, 7, 30); + expect(plainDateTime.weekOfYear).toBe(30); + }); +}); + +test("errors", () => { + test("this value must be a Temporal.PlainDateTime object", () => { + expect(() => { + Reflect.get(Temporal.PlainDateTime.prototype, "weekOfYear", "foo"); + }).toThrowWithMessage(TypeError, "Not a Temporal.PlainDateTime"); + }); +});