1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:57:35 +00:00

LibJS: Implement Temporal.PlainYearMonth.prototype.getISOFields()

This commit is contained in:
Linus Groh 2021-08-08 00:35:27 +01:00
parent 5a260fcad1
commit 7d8c182359
3 changed files with 57 additions and 0 deletions

View file

@ -38,6 +38,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
}
static PlainYearMonth* typed_this(GlobalObject& global_object)
@ -186,4 +187,32 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of)
return {};
}
// 9.3.22 Temporal.PlainYearMonth.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.getisofields
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::get_iso_fields)
{
// 1. Let yearMonth be the this value.
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
auto* year_month = typed_this(global_object);
if (vm.exception())
return {};
// 3. Let fields be ! OrdinaryObjectCreate(%Object.prototype%).
auto* fields = Object::create(global_object, global_object.object_prototype());
// 4. Perform ! CreateDataPropertyOrThrow(fields, "calendar", yearMonth.[[Calendar]]).
fields->create_data_property_or_throw(vm.names.calendar, Value(&year_month->calendar()));
// 5. Perform ! CreateDataPropertyOrThrow(fields, "isoDay", 𝔽(yearMonth.[[ISODay]])).
fields->create_data_property_or_throw(vm.names.isoDay, Value(year_month->iso_day()));
// 6. Perform ! CreateDataPropertyOrThrow(fields, "isoMonth", 𝔽(yearMonth.[[ISOMonth]])).
fields->create_data_property_or_throw(vm.names.isoMonth, Value(year_month->iso_month()));
// 7. Perform ! CreateDataPropertyOrThrow(fields, "isoYear", 𝔽(yearMonth.[[ISOYear]])).
fields->create_data_property_or_throw(vm.names.isoYear, Value(year_month->iso_year()));
// 8. Return fields.
return fields;
}
}

View file

@ -28,6 +28,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
};
}

View file

@ -0,0 +1,27 @@
describe("normal behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainYearMonth.prototype.getISOFields).toHaveLength(0);
});
test("basic functionality", () => {
const calendar = new Temporal.Calendar("iso8601");
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7, calendar, 6);
const fields = plainYearMonth.getISOFields();
expect(fields).toEqual({ calendar, isoDay: 6, isoMonth: 7, isoYear: 2021 });
// Test field order
expect(Object.getOwnPropertyNames(fields)).toEqual([
"calendar",
"isoDay",
"isoMonth",
"isoYear",
]);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Temporal.PlainYearMonth.prototype.getISOFields.call("foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.PlainYearMonth");
});
});