mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:37:35 +00:00
LibJS: Implement Temporal.Duration.compare
This commit is contained in:
parent
29072f4b09
commit
5e3fe52fc4
3 changed files with 166 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -29,6 +30,7 @@ void DurationConstructor::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);
|
||||
}
|
||||
|
@ -100,4 +102,71 @@ JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::from)
|
|||
return TRY(to_temporal_duration(global_object, item));
|
||||
}
|
||||
|
||||
// 7.2.3 Temporal.Duration.compare ( one, two [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.duration.compare
|
||||
JS_DEFINE_NATIVE_FUNCTION(DurationConstructor::compare)
|
||||
{
|
||||
// 1. Set one to ? ToTemporalDuration(one).
|
||||
auto* one = TRY(to_temporal_duration(global_object, vm.argument(0)));
|
||||
|
||||
// 2. Set two to ? ToTemporalDuration(two).
|
||||
auto* two = TRY(to_temporal_duration(global_object, vm.argument(1)));
|
||||
|
||||
// 3. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(2)));
|
||||
|
||||
// 4. Let relativeTo be ? ToRelativeTemporalObject(options).
|
||||
auto relative_to = TRY(to_relative_temporal_object(global_object, *options));
|
||||
|
||||
// 5. Let shift1 be ? CalculateOffsetShift(relativeTo, one.[[Years]], one.[[Months]], one.[[Weeks]], one.[[Days]], one.[[Hours]], one.[[Minutes]], one.[[Seconds]], one.[[Milliseconds]], one.[[Microseconds]], one.[[Nanoseconds]]).
|
||||
auto shift1 = TRY(calculate_offset_shift(global_object, relative_to, one->years(), one->months(), one->weeks(), one->days(), one->hours(), one->minutes(), one->seconds(), one->milliseconds(), one->microseconds(), one->nanoseconds()));
|
||||
|
||||
// 6. Let shift2 be ? CalculateOffsetShift(relativeTo, two.[[Years]], two.[[Months]], two.[[Weeks]], two.[[Days]], two.[[Hours]], two.[[Minutes]], two.[[Seconds]], two.[[Milliseconds]], two.[[Microseconds]], two.[[Nanoseconds]]).
|
||||
auto shift2 = TRY(calculate_offset_shift(global_object, relative_to, two->years(), two->months(), two->weeks(), two->days(), two->hours(), two->minutes(), two->seconds(), two->milliseconds(), two->microseconds(), two->nanoseconds()));
|
||||
|
||||
double days1;
|
||||
double days2;
|
||||
|
||||
// 7. If any of one.[[Years]], two.[[Years]], one.[[Months]], two.[[Months]], one.[[Weeks]], or two.[[Weeks]] are not 0, then
|
||||
if (one->years() != 0 || two->years() != 0 || one->months() != 0 || two->months() != 0 || one->weeks() != 0 || two->weeks() != 0) {
|
||||
// a. Let unbalanceResult1 be ? UnbalanceDurationRelative(one.[[Years]], one.[[Months]], one.[[Weeks]], one.[[Days]], "day", relativeTo).
|
||||
auto unbalance_result1 = TRY(unbalance_duration_relative(global_object, one->years(), one->months(), one->weeks(), one->days(), "day", relative_to));
|
||||
|
||||
// b. Let unbalanceResult2 be ? UnbalanceDurationRelative(two.[[Years]], two.[[Months]], two.[[Weeks]], two.[[Days]], "day", relativeTo).
|
||||
auto unbalance_result2 = TRY(unbalance_duration_relative(global_object, two->years(), two->months(), two->weeks(), two->days(), "day", relative_to));
|
||||
|
||||
// c. Let days1 be unbalanceResult1.[[Days]].
|
||||
days1 = unbalance_result1.days;
|
||||
|
||||
// d. Let days2 be unbalanceResult2.[[Days]].
|
||||
days2 = unbalance_result2.days;
|
||||
}
|
||||
// 8. Else,
|
||||
else {
|
||||
// a. Let days1 be one.[[Days]].
|
||||
days1 = one->days();
|
||||
|
||||
// b. Let days2 be two.[[Days]].
|
||||
days2 = two->days();
|
||||
}
|
||||
|
||||
// 9. Let ns1 be ! TotalDurationNanoseconds(days1, one.[[Hours]], one.[[Minutes]], one.[[Seconds]], one.[[Milliseconds]], one.[[Microseconds]], one.[[Nanoseconds]], shift1).
|
||||
auto* nanoseconds1_bigint = js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)one->nanoseconds()));
|
||||
auto* ns1 = total_duration_nanoseconds(global_object, days1, one->hours(), one->minutes(), one->seconds(), one->milliseconds(), one->microseconds(), *nanoseconds1_bigint, shift1);
|
||||
|
||||
// 10. Let ns2 be ! TotalDurationNanoseconds(days2, two.[[Hours]], two.[[Minutes]], two.[[Seconds]], two.[[Milliseconds]], two.[[Microseconds]], two.[[Nanoseconds]], shift2).
|
||||
auto* nanoseconds2_bigint = js_bigint(vm, Crypto::SignedBigInteger::create_from((i64)two->nanoseconds()));
|
||||
auto* ns2 = total_duration_nanoseconds(global_object, days2, two->hours(), two->minutes(), two->seconds(), two->milliseconds(), two->microseconds(), *nanoseconds2_bigint, shift2);
|
||||
|
||||
// 11. If ns1 > ns2, return 1.
|
||||
if (ns1->big_integer() > ns2->big_integer())
|
||||
return 1;
|
||||
|
||||
// 12. If ns1 < ns2, return −1.
|
||||
if (ns1->big_integer() < ns2->big_integer())
|
||||
return -1;
|
||||
|
||||
// 13. Return 0.
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ private:
|
|||
virtual bool has_constructor() const override { return true; }
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(from);
|
||||
JS_DECLARE_NATIVE_FUNCTION(compare);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 2", () => {
|
||||
expect(Temporal.Duration.compare).toHaveLength(2);
|
||||
});
|
||||
|
||||
function checkCommonResults(duration1, duration2) {
|
||||
expect(Temporal.Duration.compare(duration1, duration1)).toBe(0);
|
||||
expect(Temporal.Duration.compare(duration2, duration2)).toBe(0);
|
||||
expect(Temporal.Duration.compare(duration1, duration2)).toBe(-1);
|
||||
expect(Temporal.Duration.compare(duration2, duration1)).toBe(1);
|
||||
}
|
||||
|
||||
test("basic functionality", () => {
|
||||
const duration1 = new Temporal.Duration(0, 0, 0, 1);
|
||||
const duration2 = new Temporal.Duration(0, 0, 0, 2);
|
||||
checkCommonResults(duration1, duration2);
|
||||
});
|
||||
|
||||
test("duration-like objects", () => {
|
||||
const duration1 = { years: 0, months: 0, weeks: 0, days: 1 };
|
||||
const duration2 = { years: 0, months: 0, weeks: 0, days: 2 };
|
||||
checkCommonResults(duration1, duration2);
|
||||
});
|
||||
|
||||
// FIXME: Un-skip once ParseTemporalDurationString is implemented
|
||||
test.skip("duration strings", () => {
|
||||
const duration1 = "P1D";
|
||||
const duration2 = "P2D";
|
||||
checkCommonResults(duration1, duration2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("invalid duration-like object", () => {
|
||||
expect(() => {
|
||||
Temporal.Duration.compare({});
|
||||
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
|
||||
|
||||
expect(() => {
|
||||
Temporal.Duration.compare({ years: 0, months: 0, weeks: 0, days: 1 }, {});
|
||||
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
|
||||
});
|
||||
|
||||
test("relativeTo is required for comparing calendar units (year, month, week)", () => {
|
||||
const duration1 = new Temporal.Duration(1);
|
||||
const duration2 = new Temporal.Duration(2);
|
||||
|
||||
expect(() => {
|
||||
Temporal.Duration.compare(duration1, duration2);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"A starting point is required for balancing calendar units"
|
||||
);
|
||||
|
||||
const duration3 = new Temporal.Duration(0, 3);
|
||||
const duration4 = new Temporal.Duration(0, 4);
|
||||
|
||||
expect(() => {
|
||||
Temporal.Duration.compare(duration3, duration4);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"A starting point is required for balancing calendar units"
|
||||
);
|
||||
|
||||
const duration5 = new Temporal.Duration(0, 0, 5);
|
||||
const duration6 = new Temporal.Duration(0, 0, 6);
|
||||
|
||||
expect(() => {
|
||||
Temporal.Duration.compare(duration5, duration6);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"A starting point is required for balancing calendar units"
|
||||
);
|
||||
|
||||
// Still throws if year/month/week of one the duration objects is non-zero.
|
||||
const duration7 = new Temporal.Duration(0, 0, 0, 7);
|
||||
const duration8 = new Temporal.Duration(0, 0, 8);
|
||||
|
||||
expect(() => {
|
||||
Temporal.Duration.compare(duration7, duration8);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"A starting point is required for balancing calendar units"
|
||||
);
|
||||
|
||||
const duration9 = new Temporal.Duration(0, 0, 9);
|
||||
const duration10 = new Temporal.Duration(0, 0, 0, 10);
|
||||
|
||||
expect(() => {
|
||||
Temporal.Duration.compare(duration9, duration10);
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"A starting point is required for balancing calendar units"
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue