summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp26
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.fields.js30
3 files changed, 57 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
index ecdd4ad969..7330973396 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/TypeCasts.h>
+#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
@@ -47,6 +48,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.daysInYear, days_in_year, 1, attr);
define_native_function(vm.names.monthsInYear, months_in_year, 1, attr);
define_native_function(vm.names.inLeapYear, in_leap_year, 1, attr);
+ define_native_function(vm.names.fields, fields, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
}
@@ -446,6 +448,30 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::in_leap_year)
return Value(is_iso_leap_year(iso_year(temporal_date_like.as_object())));
}
+// 12.4.21 Temporal.Calendar.prototype.fields ( fields ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.fields
+// NOTE: This is the minimum fields implementation for engines without ECMA-402.
+JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::fields)
+{
+ auto fields = vm.argument(0);
+
+ // 1. Let calendar be the this value.
+ // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]).
+ auto* calendar = typed_this(global_object);
+ if (vm.exception())
+ return {};
+
+ // 3. Assert: calendar.[[Identifier]] is "iso8601".
+ VERIFY(calendar->identifier() == "iso8601"sv);
+
+ // 4. Let fieldNames be ? IterableToListOfType(fields, ยซ String ยป).
+ auto field_names = iterable_to_list_of_type(global_object, fields, { OptionType::String });
+ if (vm.exception())
+ return {};
+
+ // 5. Return ! CreateArrayFromList(fieldNames).
+ return Array::create_from(global_object, field_names);
+}
+
// 12.4.23 Temporal.Calendar.prototype.toString ( ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.tostring
JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::to_string)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
index c4d5a0d494..d208f73d81 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h
@@ -34,6 +34,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(days_in_year);
JS_DECLARE_NATIVE_FUNCTION(months_in_year);
JS_DECLARE_NATIVE_FUNCTION(in_leap_year);
+ JS_DECLARE_NATIVE_FUNCTION(fields);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
};
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.fields.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.fields.js
new file mode 100644
index 0000000000..714f978ccf
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.fields.js
@@ -0,0 +1,30 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.Calendar.prototype.fields).toHaveLength(1);
+ });
+
+ test("basic functionality", () => {
+ const calendar = new Temporal.Calendar("iso8601");
+ const array = ["foo", "bar", "baz"];
+ const fields = calendar.fields(array);
+ expect(fields).toEqual(array);
+ expect(fields).not.toBe(array);
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.Calendar object", () => {
+ expect(() => {
+ Temporal.Calendar.prototype.fields.call("foo");
+ }).toThrowWithMessage(TypeError, "Not a Temporal.Calendar");
+ });
+
+ test("iterator values must be strings", () => {
+ const calendar = new Temporal.Calendar("iso8601");
+ for (const value of [123, null, undefined, true, {}]) {
+ expect(() => {
+ calendar.fields([value]);
+ }).toThrowWithMessage(TypeError, "FIXME: Add a string for this error.");
+ }
+ });
+});