mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 01:37:39 +03:00
LibJS: Implement Temporal.Calendar.prototype.fields()
This commit is contained in:
parent
7d23d9d557
commit
c1ffc17134
Notes:
sideshowbarker
2024-07-18 05:38:08 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/c1ffc171345 Pull-request: https://github.com/SerenityOS/serenity/pull/9459
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
};
|
||||
|
@ -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.");
|
||||
}
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user