diff options
author | Linus Groh <mail@linusgroh.de> | 2021-07-23 01:04:10 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-23 08:18:11 +0100 |
commit | 080112eb821d6a111697b1849295526828ab9dfc (patch) | |
tree | 773fe1d9c7bf2262326b3d1d705c4caa2445a4c7 | |
parent | 18fd0d401171634f87cf9d3b45d60a59d251f7a9 (diff) | |
download | serenity-080112eb821d6a111697b1849295526828ab9dfc.zip |
LibJS: Implement Temporal.PlainDateTime.prototype.getISOFields()
4 files changed, 106 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index efc86cb2ae..fdda3c191a 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -173,6 +173,7 @@ namespace JS { P(getInt8) \ P(getInt16) \ P(getInt32) \ + P(getISOFields) \ P(getMilliseconds) \ P(getMinutes) \ P(getMonth) \ @@ -227,6 +228,15 @@ namespace JS { P(isSafeInteger) \ P(isSealed) \ P(isView) \ + P(isoDay) \ + P(isoHour) \ + P(isoMicrosecond) \ + P(isoMillisecond) \ + P(isoMinute) \ + P(isoMonth) \ + P(isoNanosecond) \ + P(isoSecond) \ + P(isoYear) \ P(italics) \ P(join) \ P(keyFor) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp index 1cb2fe38eb..0a273be456 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.cpp @@ -31,6 +31,7 @@ void PlainDateTimePrototype::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.toPlainDate, to_plain_date, 0, attr); + define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr); } static PlainDateTime* typed_this(GlobalObject& global_object) @@ -80,4 +81,51 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_date) return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()); } +// 5.3.41 Temporal.PlainDateTime.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.getisofields +JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::get_iso_fields) +{ + // 1. Let dateTime be the this value. + // 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]). + auto* date_time = 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", dateTime.[[Calendar]]). + fields->create_data_property_or_throw(vm.names.calendar, Value(&date_time->calendar())); + + // 5. Perform ! CreateDataPropertyOrThrow(fields, "isoDay", 𝔽(dateTime.[[ISODay]])). + fields->create_data_property_or_throw(vm.names.isoDay, Value(date_time->iso_day())); + + // 6. Perform ! CreateDataPropertyOrThrow(fields, "isoHour", 𝔽(dateTime.[[ISOHour]])). + fields->create_data_property_or_throw(vm.names.isoHour, Value(date_time->iso_hour())); + + // 7. Perform ! CreateDataPropertyOrThrow(fields, "isoMicrosecond", 𝔽(dateTime.[[ISOMicrosecond]])). + fields->create_data_property_or_throw(vm.names.isoMicrosecond, Value(date_time->iso_microsecond())); + + // 8. Perform ! CreateDataPropertyOrThrow(fields, "isoMillisecond", 𝔽(dateTime.[[ISOMillisecond]])). + fields->create_data_property_or_throw(vm.names.isoMillisecond, Value(date_time->iso_millisecond())); + + // 9. Perform ! CreateDataPropertyOrThrow(fields, "isoMinute", 𝔽(dateTime.[[ISOMinute]])). + fields->create_data_property_or_throw(vm.names.isoMinute, Value(date_time->iso_minute())); + + // 10. Perform ! CreateDataPropertyOrThrow(fields, "isoMonth", 𝔽(dateTime.[[ISOMonth]])). + fields->create_data_property_or_throw(vm.names.isoMonth, Value(date_time->iso_month())); + + // 11. Perform ! CreateDataPropertyOrThrow(fields, "isoNanosecond", 𝔽(dateTime.[[ISONanosecond]])). + fields->create_data_property_or_throw(vm.names.isoNanosecond, Value(date_time->iso_nanosecond())); + + // TODO: Typo in the spec? ([[Second]] -> [[ISOSecond]]) + // 12. Perform ! CreateDataPropertyOrThrow(fields, "isoSecond", 𝔽(dateTime.[[Second]])). + fields->create_data_property_or_throw(vm.names.isoSecond, Value(date_time->iso_second())); + + // 13. Perform ! CreateDataPropertyOrThrow(fields, "isoYear", 𝔽(dateTime.[[ISOYear]])). + fields->create_data_property_or_throw(vm.names.isoYear, Value(date_time->iso_year())); + + // 14. Return fields. + return fields; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h index 28eadc2422..fee1079a1e 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDateTimePrototype.h @@ -22,6 +22,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(calendar_getter); JS_DECLARE_NATIVE_FUNCTION(value_of); JS_DECLARE_NATIVE_FUNCTION(to_plain_date); + JS_DECLARE_NATIVE_FUNCTION(get_iso_fields); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.getISOFields.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.getISOFields.js new file mode 100644 index 0000000000..ecfe2b1487 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainDateTime/PlainDateTime.prototype.getISOFields.js @@ -0,0 +1,47 @@ +describe("normal behavior", () => { + test("length is 0", () => { + expect(Temporal.PlainDateTime.prototype.getISOFields).toHaveLength(0); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainDateTime = new Temporal.PlainDateTime( + 2021, + 7, + 23, + 0, + 42, + 18, + 123, + 456, + 789, + calendar + ); + const fields = plainDateTime.getISOFields(); + expect(fields).toEqual({ + calendar: calendar, + isoDay: 23, + isoHour: 0, + isoMicrosecond: 200, + isoMillisecond: 123, + isoMinute: 42, + isoMonth: 7, + isoNanosecond: 21, + isoSecond: 18, + isoYear: 2021, + }); + // Test field order + expect(Object.getOwnPropertyNames(fields)).toEqual([ + "calendar", + "isoDay", + "isoHour", + "isoMicrosecond", + "isoMillisecond", + "isoMinute", + "isoMonth", + "isoNanosecond", + "isoSecond", + "isoYear", + ]); + }); +}); |