Quellcode durchsuchen

LibJS: Implement Temporal.PlainDate.prototype.add()

Linus Groh vor 3 Jahren
Ursprung
Commit
bcd96c80f3

+ 18 - 0
Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp

@@ -53,6 +53,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
     define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
     define_native_function(vm.names.toPlainMonthDay, to_plain_month_day, 0, attr);
     define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
+    define_native_function(vm.names.add, add, 1, attr);
     define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
     define_native_function(vm.names.equals, equals, 1, attr);
     define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 0, attr);
@@ -335,6 +336,23 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields)
     return fields;
 }
 
+// 3.3.19 Temporal.PlainDate.prototype.add ( temporalDurationLike [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.add
+JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::add)
+{
+    // 1. Let temporalDate be the this value.
+    // 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
+    auto* temporal_date = TRY(typed_this_object(global_object));
+
+    // 3. Let duration be ? ToTemporalDuration(temporalDurationLike).
+    auto* duration = TRY(to_temporal_duration(global_object, vm.argument(0)));
+
+    // 4. Set options to ? GetOptionsObject(options).
+    auto* options = TRY(get_options_object(global_object, vm.argument(1)));
+
+    // 5. Return ? CalendarDateAdd(temporalDate.[[Calendar]], temporalDate, duration, options).
+    return TRY(calendar_date_add(global_object, temporal_date->calendar(), *temporal_date, *duration, options));
+}
+
 // 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar
 JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
 {

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

@@ -38,6 +38,7 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month);
     JS_DECLARE_NATIVE_FUNCTION(to_plain_month_day);
     JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
+    JS_DECLARE_NATIVE_FUNCTION(add);
     JS_DECLARE_NATIVE_FUNCTION(with_calendar);
     JS_DECLARE_NATIVE_FUNCTION(equals);
     JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time);

+ 19 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.add.js

@@ -0,0 +1,19 @@
+describe("correct behavior", () => {
+    test("length is 1", () => {
+        expect(Temporal.PlainDate.prototype.add).toHaveLength(1);
+    });
+
+    test("basic functionality", () => {
+        const plainDate = new Temporal.PlainDate(1970, 1, 1);
+        const result = plainDate.add(new Temporal.Duration(51, 6, 0, 5));
+        expect(result.equals(new Temporal.PlainDate(2021, 7, 6))).toBeTrue();
+    });
+});
+
+describe("errors", () => {
+    test("this value must be a Temporal.PlainDate object", () => {
+        expect(() => {
+            Temporal.PlainDate.prototype.add.call("foo");
+        }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
+    });
+});