diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp index 5880d900af..449f4f0f13 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Idan Horowitz + * Copyright (c) 2021, Linus Groh * * 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) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h index 3691340905..73f2f97b93 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDatePrototype.h @@ -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); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.getISOFields.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.getISOFields.js new file mode 100644 index 0000000000..eaef972318 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDate/PlainDate.prototype.getISOFields.js @@ -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", + ]); + }); +});