diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp index 4d83c1a477..1eb60b0275 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.cpp @@ -38,6 +38,7 @@ void PlainTimePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.nanosecond, nanosecond_getter, {}, Attribute::Configurable); u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.equals, equals, 1, attr); define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 1, attr); define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); define_native_function(vm.names.valueOf, value_of, 0, attr); @@ -147,6 +148,48 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::nanosecond_getter) return Value(temporal_time->iso_nanosecond()); } +// 4.3.16 Temporal.PlainTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.equals +JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::equals) +{ + // 1. Let temporalTime be the this value. + // 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]). + auto* temporal_time = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Set other to ? ToTemporalTime(other). + auto* other = to_temporal_time(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 4. If temporalTime.[[ISOHour]] ≠ other.[[ISOHour]], return false. + if (temporal_time->iso_hour() != other->iso_hour()) + return Value(false); + + // 5. If temporalTime.[[ISOMinute]] ≠ other.[[ISOMinute]], return false. + if (temporal_time->iso_minute() != other->iso_minute()) + return Value(false); + + // 6. If temporalTime.[[ISOSecond]] ≠ other.[[ISOSecond]], return false. + if (temporal_time->iso_second() != other->iso_second()) + return Value(false); + + // 7. If temporalTime.[[ISOMillisecond]] ≠ other.[[ISOMillisecond]], return false. + if (temporal_time->iso_millisecond() != other->iso_millisecond()) + return Value(false); + + // 8. If temporalTime.[[ISOMicrosecond]] ≠ other.[[ISOMicrosecond]], return false. + if (temporal_time->iso_microsecond() != other->iso_microsecond()) + return Value(false); + + // 9. If temporalTime.[[ISONanosecond]] ≠ other.[[ISONanosecond]], return false. + if (temporal_time->iso_nanosecond() != other->iso_nanosecond()) + return Value(false); + + // 10. Return true. + return Value(true); +} + // 4.3.17 Temporal.PlainTime.prototype.toPlainDateTime ( temporalDate ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.prototype.toplaindatetime JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_plain_date_time) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h index f5b2f881a4..d590872bca 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimePrototype.h @@ -26,6 +26,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(millisecond_getter); JS_DECLARE_NATIVE_FUNCTION(microsecond_getter); JS_DECLARE_NATIVE_FUNCTION(nanosecond_getter); + JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time); JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); JS_DECLARE_NATIVE_FUNCTION(value_of); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.equals.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.equals.js new file mode 100644 index 0000000000..595b299063 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.prototype.equals.js @@ -0,0 +1,12 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainTime.prototype.equals).toHaveLength(1); + }); + + test("basic functionality", () => { + const firstPlainTime = new Temporal.PlainTime(1, 1, 1, 1, 1, 1); + const secondPlainTime = new Temporal.PlainTime(0, 1, 1, 1, 1, 1); + expect(firstPlainTime.equals(firstPlainTime)); + expect(!secondPlainTime.equals(secondPlainTime)); + }); +});