1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:17:46 +00:00

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

This commit is contained in:
Linus Groh 2021-07-29 23:51:55 +01:00
parent dc34cbe28b
commit c7bef42975
3 changed files with 50 additions and 0 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -41,6 +42,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
@ -264,6 +266,34 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::in_leap_year_getter)
return Value(calendar_in_leap_year(global_object, calendar, *temporal_date));
}
// 3.3.18 Temporal.PlainDate.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.getisofields
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields)
{
// 1. Let temporalDate be the this value.
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
auto* temporal_date = 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", temporalDate.[[Calendar]]).
fields->create_data_property_or_throw(vm.names.calendar, Value(&temporal_date->calendar()));
// 5. Perform ! CreateDataPropertyOrThrow(fields, "isoDay", 𝔽(temporalDate.[[ISODay]])).
fields->create_data_property_or_throw(vm.names.isoDay, Value(temporal_date->iso_day()));
// 6. Perform ! CreateDataPropertyOrThrow(fields, "isoMonth", 𝔽(temporalDate.[[ISOMonth]])).
fields->create_data_property_or_throw(vm.names.isoMonth, Value(temporal_date->iso_month()));
// 7. Perform ! CreateDataPropertyOrThrow(fields, "isoYear", 𝔽(temporalDate.[[ISOYear]])).
fields->create_data_property_or_throw(vm.names.isoYear, Value(temporal_date->iso_year()));
// 8. Return fields.
return fields;
}
// 3.3.22 Temporal.PlainDate.prototype.withCalendar ( calendar ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.withcalendar
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
{

View file

@ -33,6 +33,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(value_of);

View file

@ -0,0 +1,19 @@
describe("normal behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainDate.prototype.getISOFields).toHaveLength(0);
});
test("basic functionality", () => {
const calendar = new Temporal.Calendar("iso8601");
const plainDate = new Temporal.PlainDate(2021, 7, 29, calendar);
const fields = plainDate.getISOFields();
expect(fields).toEqual({ calendar, isoDay: 29, isoMonth: 7, isoYear: 2021 });
// Test field order
expect(Object.getOwnPropertyNames(fields)).toEqual([
"calendar",
"isoDay",
"isoMonth",
"isoYear",
]);
});
});