mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:37:35 +00:00
LibJS: Implement Temporal.PlainDate.compare
This commit is contained in:
parent
2c6bd3a61b
commit
07485802c6
5 changed files with 67 additions and 0 deletions
|
@ -220,4 +220,37 @@ bool is_valid_iso_date(i32 year, u8 month, u8 day)
|
|||
return true;
|
||||
}
|
||||
|
||||
// 3.5.10 CompareISODate ( y1, m1, d1, y2, m2, d2 ), https://tc39.es/proposal-temporal/#sec-temporal-compareisodate
|
||||
i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2)
|
||||
{
|
||||
// 1. Assert: y1, m1, d1, y2, m2, and d2 are integers.
|
||||
|
||||
// 2. If y1 > y2, return 1.
|
||||
if (year1 > year2)
|
||||
return 1;
|
||||
|
||||
// 3. If y1 < y2, return -1.
|
||||
if (year1 < year2)
|
||||
return -1;
|
||||
|
||||
// 4. If m1 > m2, return 1.
|
||||
if (month1 > month2)
|
||||
return 1;
|
||||
|
||||
// 5. If m1 < m2, return -1.
|
||||
if (month1 < month2)
|
||||
return -1;
|
||||
|
||||
// 6. If d1 > d2, return 1.
|
||||
if (day1 > day2)
|
||||
return 1;
|
||||
|
||||
// 7. If d1 < d2, return -1.
|
||||
if (day1 < day2)
|
||||
return -1;
|
||||
|
||||
// 8. Return 0.
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -38,5 +38,6 @@ PlainDate* create_temporal_date(GlobalObject&, i32 iso_year, u8 iso_month, u8 is
|
|||
PlainDate* to_temporal_date(GlobalObject&, Value item, Object* options = nullptr);
|
||||
Optional<TemporalDate> regulate_iso_date(GlobalObject&, double year, double month, double day, String const& overflow);
|
||||
bool is_valid_iso_date(i32 year, u8 month, u8 day);
|
||||
i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2);
|
||||
|
||||
}
|
||||
|
|
|
@ -27,6 +27,9 @@ void PlainDateConstructor::initialize(GlobalObject& global_object)
|
|||
// 3.2.1 Temporal.PlainDate.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-prototype
|
||||
define_direct_property(vm.names.prototype, global_object.temporal_plain_date_prototype(), 0);
|
||||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(vm.names.compare, compare, 2, attr);
|
||||
|
||||
define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
|
||||
}
|
||||
|
||||
|
@ -93,4 +96,19 @@ Value PlainDateConstructor::construct(FunctionObject& new_target)
|
|||
return create_temporal_date(global_object, y, m, d, *calendar, &new_target);
|
||||
}
|
||||
|
||||
// 3.2.3 Temporal.PlainDate.compare ( one, two ), https://tc39.es/proposal-temporal/#sec-properties-of-the-temporal-plaindate-constructor
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDateConstructor::compare)
|
||||
{
|
||||
// 1. Set one to ? ToTemporalDate(one).
|
||||
auto* one = to_temporal_date(global_object, vm.argument(0));
|
||||
if (vm.exception())
|
||||
return {};
|
||||
// 2. Set two to ? ToTemporalDate(two).
|
||||
auto* two = to_temporal_date(global_object, vm.argument(1));
|
||||
if (vm.exception())
|
||||
return {};
|
||||
// 3. Return 𝔽(! CompareISODate(one.[[ISOYear]], one.[[ISOMonth]], one.[[ISODay]], two.[[ISOYear]], two.[[ISOMonth]], two.[[ISODay]])).
|
||||
return Value(compare_iso_date(one->iso_year(), one->iso_month(), one->iso_day(), two->iso_year(), two->iso_month(), two->iso_day()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@ public:
|
|||
|
||||
private:
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(compare);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 2", () => {
|
||||
expect(Temporal.PlainDate.compare).toHaveLength(2);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainDate1 = new Temporal.PlainDate(2021, 7, 26);
|
||||
expect(Temporal.PlainDate.compare(plainDate1, plainDate1)).toBe(0);
|
||||
const plainDate2 = new Temporal.PlainDate(2021, 7, 27);
|
||||
expect(Temporal.PlainDate.compare(plainDate1, plainDate2)).toBe(-1);
|
||||
expect(Temporal.PlainDate.compare(plainDate2, plainDate1)).toBe(1);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue