summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-08 00:35:27 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-08 17:45:06 +0100
commit7d8c182359ab28b00db01856053af6365b239ba8 (patch)
tree930f9db6e29c76ae93770a78f5db1cbbf78a17c3 /Userland/Libraries/LibJS
parent5a260fcad1d07690966060ece198ab9ef0771489 (diff)
downloadserenity-7d8c182359ab28b00db01856053af6365b239ba8.zip
LibJS: Implement Temporal.PlainYearMonth.prototype.getISOFields()
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp29
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.getISOFields.js27
3 files changed, 57 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
index 089aac1233..aa239efd86 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp
@@ -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;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
index b53035eea8..4c540217b0 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h
@@ -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);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.getISOFields.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.getISOFields.js
new file mode 100644
index 0000000000..db8ac2b24b
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.getISOFields.js
@@ -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");
+ });
+});