LibJS: Implement Temporal.PlainYearMonth.prototype.equals

This commit is contained in:
Luke Wilde 2021-09-09 03:53:47 +01:00 committed by Linus Groh
parent ff0b01a505
commit 58e2597dc2
Notes: sideshowbarker 2024-07-18 04:23:36 +09:00
3 changed files with 47 additions and 0 deletions

View file

@ -40,6 +40,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
@ -217,6 +218,36 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::era_year_getter)
return calendar_era_year(global_object, calendar, *plain_year_month);
}
// 9.3.16 Temporal.PlainYearMonth.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.equals
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::equals)
{
// 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = typed_this(global_object);
if (vm.exception())
return {};
// 3. Set other to ? ToTemporalYearMonth(other).
auto* other = to_temporal_year_month(global_object, vm.argument(0));
if (vm.exception())
return {};
// 4. If yearMonth.[[ISOYear]] ≠ other.[[ISOYear]], return false.
if (year_month->iso_year() != other->iso_year())
return Value(false);
// 5. If yearMonth.[[ISOMonth]] ≠ other.[[ISOMonth]], return false.
if (year_month->iso_month() != other->iso_month())
return Value(false);
// 6. If yearMonth.[[ISODay]] ≠ other.[[ISODay]], return false.
if (year_month->iso_day() != other->iso_day())
return Value(false);
// 7. Return ? CalendarEquals(yearMonth.[[Calendar]], other.[[Calendar]]).
return Value(calendar_equals(global_object, year_month->calendar(), other->calendar()));
}
// 9.3.17 Temporal.PlainYearMonth.prototype.toString ( [ options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_string)
{

View file

@ -29,6 +29,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(era_getter);
JS_DECLARE_NATIVE_FUNCTION(era_year_getter);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainYearMonth.prototype.equals).toHaveLength(1);
});
test("basic functionality", () => {
const calendar = { hello: "friends" };
const firstPlainDateTime = new Temporal.PlainYearMonth(1, 1, calendar);
const secondPlainDateTime = new Temporal.PlainYearMonth(0, 1, calendar);
expect(firstPlainDateTime.equals(firstPlainDateTime)).toBeTrue();
expect(secondPlainDateTime.equals(secondPlainDateTime)).toBeTrue();
expect(firstPlainDateTime.equals(secondPlainDateTime)).toBeFalse();
expect(secondPlainDateTime.equals(firstPlainDateTime)).toBeFalse();
});
});