LibJS: Implement Temporal.Calendar.prototype.fields()

This commit is contained in:
Linus Groh 2021-08-16 18:42:15 +01:00
parent 7d23d9d557
commit c1ffc17134
Notes: sideshowbarker 2024-07-18 05:38:08 +09:00
3 changed files with 57 additions and 0 deletions

View File

@ -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)
{

View File

@ -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);
};

View File

@ -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.");
}
});
});