Browse Source

LibJS: Implement Temporal.Calendar.prototype.dayOfWeek

Idan Horowitz 4 years ago
parent
commit
339b0a17e8

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

@@ -106,6 +106,7 @@ namespace JS {
     P(create)                                \
     P(dateFromFields)                        \
     P(day)                                   \
+    P(dayOfWeek)                             \
     P(days)                                  \
     P(debug)                                 \
     P(decodeURI)                             \

+ 20 - 0
Userland/Libraries/LibJS/Runtime/Temporal/Calendar.cpp

@@ -386,6 +386,26 @@ i32 iso_days_in_month(i32 year, i32 month)
     return 28;
 }
 
+// 12.1.33 ToISODayOfWeek ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisodayofweek
+u8 to_iso_day_of_week(i32 year, u8 month, u8 day)
+{
+    // 1. Assert: year is an integer.
+    // 2. Assert: month is an integer.
+    // 3. Assert: day is an integer.
+
+    // 4. Let date be the date given by year, month, and day.
+    // 5. Return date's day of the week according to ISO-8601.
+    // NOTE: Implemented based on https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
+    auto normalized_month = month + (month < 3 ? 10 : -2);
+    auto normalized_year = year - (month < 3 ? 1 : 0);
+    auto century = normalized_year / 100;
+    auto truncated_year = normalized_year - (century * 100);
+    auto result = (day + static_cast<u8>((2.6 * normalized_month) - 0.2) - (2 * century) + truncated_year + (truncated_year / 4) + (century / 4)) % 7;
+    if (result <= 0) // Mathematical modulo
+        result += 7;
+    return result;
+}
+
 // 12.1.36 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode
 String build_iso_month_code(i32 month)
 {

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

@@ -45,6 +45,7 @@ PlainDate* date_from_fields(GlobalObject&, Object& calendar, Object& fields, Obj
 bool calendar_equals(GlobalObject&, Object& one, Object& two);
 bool is_iso_leap_year(i32 year);
 i32 iso_days_in_month(i32 year, i32 month);
+u8 to_iso_day_of_week(i32 year, u8 month, u8 day);
 String build_iso_month_code(i32 month);
 double resolve_iso_month(GlobalObject&, Object& fields);
 Optional<TemporalDate> iso_date_from_fields(GlobalObject&, Object& fields, Object& options);

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

@@ -35,6 +35,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.month, month, 1, attr);
     define_native_function(vm.names.monthCode, month_code, 1, attr);
     define_native_function(vm.names.day, day, 1, attr);
+    define_native_function(vm.names.dayOfWeek, day_of_week, 1, attr);
     define_native_function(vm.names.toString, to_string, 0, attr);
     define_native_function(vm.names.toJSON, to_json, 0, attr);
 }
@@ -208,6 +209,28 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day)
     return Value(iso_day(temporal_date_like.as_object()));
 }
 
+// 12.4.13 Temporal.Calendar.prototype.dayOfWeek ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.dayofweek
+// NOTE: This is the minimum dayOfWeek implementation for engines without ECMA-402.
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::day_of_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).
+    auto* temporal_date = to_temporal_date(global_object, vm.argument(0));
+    if (vm.exception())
+        return {};
+
+    // 5. Return 𝔽(! ToISODayOfWeek(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]])).
+    return Value(to_iso_day_of_week(temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day()));
+}
+
 // 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

@@ -25,6 +25,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(month);
     JS_DECLARE_NATIVE_FUNCTION(month_code);
     JS_DECLARE_NATIVE_FUNCTION(day);
+    JS_DECLARE_NATIVE_FUNCTION(day_of_week);
     JS_DECLARE_NATIVE_FUNCTION(to_string);
     JS_DECLARE_NATIVE_FUNCTION(to_json);
 };

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

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