Przeglądaj źródła

LibJS: Implement Temporal.Calendar.prototype.daysInWeek

Idan Horowitz 4 lat temu
rodzic
commit
623df361e6

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

@@ -109,6 +109,7 @@ namespace JS {
     P(dayOfWeek)                             \
     P(dayOfYear)                             \
     P(days)                                  \
+    P(daysInWeek)                            \
     P(debug)                                 \
     P(decodeURI)                             \
     P(decodeURIComponent)                    \

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

@@ -38,6 +38,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.dayOfWeek, day_of_week, 1, attr);
     define_native_function(vm.names.dayOfYear, day_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.toString, to_string, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 0, attr);
 }
@@ -277,6 +278,28 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::week_of_year)
     return Value(to_iso_week_of_year(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day()));
 }
 
+// 12.4.16 Temporal.Calendar.prototype.daysInWeek ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.daysinweek
+// NOTE: This is the minimum daysInWeek implementation for engines without ECMA-402.
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::days_in_week)
+{
+    // 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);
+
+    // 4. Let temporalDate be ? ToTemporalDate(temporalDateLike).
+    [[maybe_unused]] auto* temporal_date = to_temporal_date(global_object, vm.argument(0));
+    if (vm.exception())
+        return {};
+
+    // 5. Return 7𝔽.
+    return Value(7);
+}
+
 // 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
 JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
 {

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

@@ -28,6 +28,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(day_of_week);
     JS_DECLARE_NATIVE_FUNCTION(day_of_year);
     JS_DECLARE_NATIVE_FUNCTION(week_of_year);
+    JS_DECLARE_NATIVE_FUNCTION(days_in_week);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
 };

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

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