فهرست منبع

LibJS: Implement Temporal.Calendar.prototype.daysInYear

Idan Horowitz 4 سال پیش
والد
کامیت
7f27035342

+ 1 - 0
Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

@@ -111,6 +111,7 @@ namespace JS {
     P(days)                                  \
     P(days)                                  \
     P(daysInMonth)                           \
     P(daysInMonth)                           \
     P(daysInWeek)                            \
     P(daysInWeek)                            \
+    P(daysInYear)                            \
     P(debug)                                 \
     P(debug)                                 \
     P(decodeURI)                             \
     P(decodeURI)                             \
     P(decodeURIComponent)                    \
     P(decodeURIComponent)                    \

+ 28 - 0
Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp

@@ -40,6 +40,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.weekOfYear, week_of_year, 1, attr);
     define_native_function(vm.names.weekOfYear, week_of_year, 1, attr);
     define_native_function(vm.names.daysInWeek, days_in_week, 1, attr);
     define_native_function(vm.names.daysInWeek, days_in_week, 1, attr);
     define_native_function(vm.names.daysInMonth, days_in_month, 1, attr);
     define_native_function(vm.names.daysInMonth, days_in_month, 1, attr);
+    define_native_function(vm.names.daysInYear, days_in_year, 1, attr);
     define_native_function(vm.names.toString, to_string, 0, attr);
     define_native_function(vm.names.toString, to_string, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 0, attr);
 }
 }
@@ -329,6 +330,33 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_month)
     return Value(iso_days_in_month(iso_year(temporal_date_like.as_object()), iso_month(temporal_date_like.as_object())));
     return Value(iso_days_in_month(iso_year(temporal_date_like.as_object()), iso_month(temporal_date_like.as_object())));
 }
 }
 
 
+// 12.4.18 Temporal.Calendar.prototype.daysInYear ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.daysinyear
+// NOTE: This is the minimum daysInYear implementation for engines without ECMA-402.
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_year)
+{
+    // 1. Let calendar be the this value.
+    // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
+    auto* calendar = typed_this(global_object);
+    if (vm.exception())
+        return {};
+
+    // 3. Assert: calendar.[[Identifier]] is "iso8601".
+    VERIFY(calendar->identifier() == "iso8601"sv);
+
+    auto temporal_date_like = vm.argument(0);
+    // 4. If Type(temporalDateLike) is not Object or temporalDateLike does not have an [[InitializedTemporalDate]] or [[InitializedTemporalYearMonth]] internal slot, then
+    // TODO PlainYearMonth objects
+    if (!temporal_date_like.is_object() || !is<PlainDate>(temporal_date_like.as_object())) {
+        // a. Set temporalDateLike to ? ToTemporalDate(temporalDateLike).
+        temporal_date_like = to_temporal_date(global_object, temporal_date_like);
+        if (vm.exception())
+            return {};
+    }
+
+    // 5. Return 𝔽(! ISODaysInYear(temporalDateLike.[[ISOYear]])).
+    return Value(iso_days_in_year(iso_year(temporal_date_like.as_object())));
+}
+
 // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
 // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
 JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
 JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
 {
 {

+ 1 - 0
Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h

@@ -30,6 +30,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(week_of_year);
     JS_DECLARE_NATIVE_FUNCTION(week_of_year);
     JS_DECLARE_NATIVE_FUNCTION(days_in_week);
     JS_DECLARE_NATIVE_FUNCTION(days_in_week);
     JS_DECLARE_NATIVE_FUNCTION(days_in_month);
     JS_DECLARE_NATIVE_FUNCTION(days_in_month);
+    JS_DECLARE_NATIVE_FUNCTION(days_in_year);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
 };
 };

+ 11 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.daysInYear.js

@@ -0,0 +1,11 @@
+describe("correct behavior", () => {
+    test("length is 1", () => {
+        expect(Temporal.Calendar.prototype.daysInYear).toHaveLength(1);
+    });
+
+    test("basic functionality", () => {
+        const calendar = new Temporal.Calendar("iso8601");
+        const date = new Temporal.PlainDate(2021, 7, 23);
+        expect(calendar.daysInYear(date)).toBe(365);
+    });
+});