summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-08-27 16:42:21 +0300
committerLinus Groh <mail@linusgroh.de>2021-08-27 16:40:16 +0100
commit9ed877e8e70b5a9852f75163d860f7c2db6cc520 (patch)
tree77a6beee4943066c2ca6cdc008a9057c34d8be43
parent28fa4d15689a158a6b2915f43546ba56c5cac714 (diff)
downloadserenity-9ed877e8e70b5a9852f75163d860f7c2db6cc520.zip
LibJS: Implement Temporal.PlainDateTime.compare()
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.cpp18
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.compare.js13
3 files changed, 32 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.cpp
index a86526866b..7d0434bd03 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.cpp
@@ -31,6 +31,7 @@ void PlainDateTimeConstructor::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.from, from, 1, attr);
+ define_native_function(vm.names.compare, compare, 2, attr);
define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
}
@@ -141,4 +142,21 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::from)
return to_temporal_date_time(global_object, item, options);
}
+// 5.2.3 Temporal.PlainDateTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.compare
+JS_DEFINE_NATIVE_FUNCTION(PlainDateTimeConstructor::compare)
+{
+ // 1. Set one to ? ToTemporalDateTime(one).
+ auto* one = to_temporal_date_time(global_object, vm.argument(0));
+ if (vm.exception())
+ return {};
+
+ // 2. Set two to ? ToTemporalDateTime(two).
+ auto two = to_temporal_date_time(global_object, vm.argument(1));
+ if (vm.exception())
+ return {};
+
+ // 3. Return 𝔽(! CompareISODateTime(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], one.[[ISOHour]], one.[[ISOMinute]], one.[[ISOSecond]], one.[[ISOMillisecond]], one.[[ISOMicrosecond]], one.[[ISONanosecond]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]], two.[[ISOHour]], two.[[ISOMinute]], two.[[ISOSecond]], two.[[ISOMillisecond]], two.[[ISOMicrosecond]], two.[[ISONanosecond]])).
+ return Value(compare_iso_date_time(one->iso_year(), one->iso_month(), one->iso_day(), one->iso_hour(), one->iso_minute(), one->iso_second(), one->iso_millisecond(), one->iso_microsecond(), one->iso_nanosecond(), two->iso_year(), two->iso_month(), two->iso_day(), two->iso_hour(), two->iso_minute(), two->iso_second(), two->iso_millisecond(), two->iso_microsecond(), two->iso_nanosecond()));
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.h
index bd87641517..90577a8da6 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimeConstructor.h
@@ -25,6 +25,7 @@ private:
virtual bool has_constructor() const override { return true; }
JS_DECLARE_NATIVE_FUNCTION(from);
+ JS_DECLARE_NATIVE_FUNCTION(compare);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.compare.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.compare.js
new file mode 100644
index 0000000000..7eb4781f28
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.compare.js
@@ -0,0 +1,13 @@
+describe("correct behavior", () => {
+ test("length is 2", () => {
+ expect(Temporal.PlainDateTime.compare).toHaveLength(2);
+ });
+
+ test("basic functionality", () => {
+ const plainDateTime1 = new Temporal.PlainDateTime(2021, 8, 27, 16, 38, 40, 1, 2, 3);
+ expect(Temporal.PlainDateTime.compare(plainDateTime1, plainDateTime1)).toBe(0);
+ const plainDateTime2 = new Temporal.PlainDateTime(2021, 8, 27, 16, 39, 5, 0, 1, 2);
+ expect(Temporal.PlainDateTime.compare(plainDateTime1, plainDateTime2)).toBe(-1);
+ expect(Temporal.PlainDateTime.compare(plainDateTime2, plainDateTime1)).toBe(1);
+ });
+});