diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-08-27 18:52:51 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-08-27 19:01:30 +0100 |
commit | 684e62476bbd0bda3714b13cc2fd7e3d58f1f83a (patch) | |
tree | 8391f224a72ab00bca74498093da9f74e0e2db84 /Userland | |
parent | a77cdc5f92241db4a1845aa9dcaf9c5908ed268e (diff) | |
download | serenity-684e62476bbd0bda3714b13cc2fd7e3d58f1f83a.zip |
LibJS: Implement Temporal.PlainTime.compare()
Diffstat (limited to 'Userland')
3 files changed, 32 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimeConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimeConstructor.cpp index 04d7d3d8b7..ec3e039d02 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimeConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimeConstructor.cpp @@ -29,6 +29,7 @@ void PlainTimeConstructor::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(0), Attribute::Configurable); } @@ -118,4 +119,21 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimeConstructor::from) return to_temporal_time(global_object, item, *overflow); } +// 4.2.3 Temporal.PlainTime.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.compare +JS_DEFINE_NATIVE_FUNCTION(PlainTimeConstructor::compare) +{ + // 1. Set one to ? ToTemporalTime(one). + auto* one = to_temporal_time(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 2. Set two to ? ToTemporalTime(two). + auto* two = to_temporal_time(global_object, vm.argument(1)); + if (vm.exception()) + return {}; + + // 3. Return 𝔽(! CompareTemporalTime(one.[[ISOHour]], one.[[ISOMinute]], one.[[ISOSecond]], one.[[ISOMillisecond]], one.[[ISOMicrosecond]], one.[[ISONanosecond]], two.[[ISOHour]], two.[[ISOMinute]], two.[[ISOSecond]], two.[[ISOMillisecond]], two.[[ISOMicrosecond]], two.[[ISONanosecond]])). + return Value(compare_temporal_time(one->iso_hour(), one->iso_minute(), one->iso_second(), one->iso_millisecond(), one->iso_microsecond(), one->iso_nanosecond(), 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/PlainTimeConstructor.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimeConstructor.h index 483b218e71..9e6b69a57e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimeConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainTimeConstructor.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/PlainTime/PlainTime.compare.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.compare.js new file mode 100644 index 0000000000..52110cc0bd --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainTime/PlainTime.compare.js @@ -0,0 +1,13 @@ +describe("correct behavior", () => { + test("length is 2", () => { + expect(Temporal.PlainTime.compare).toHaveLength(2); + }); + + test("basic functionality", () => { + const plainTime1 = new Temporal.PlainTime(16, 38, 40, 1, 2, 3); + expect(Temporal.PlainTime.compare(plainTime1, plainTime1)).toBe(0); + const plainTime2 = new Temporal.PlainTime(16, 39, 5, 0, 1, 2); + expect(Temporal.PlainTime.compare(plainTime1, plainTime2)).toBe(-1); + expect(Temporal.PlainTime.compare(plainTime2, plainTime1)).toBe(1); + }); +}); |