mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 20:33:10 +00:00
LibJS: Implement Temporal.PlainDate.prototype.toPlainYearMonth()
This commit is contained in:
parent
ed9d37bd40
commit
6709c915aa
|
@ -392,6 +392,7 @@ namespace JS {
|
|||
P(toPlainDate) \
|
||||
P(toPlainDateTime) \
|
||||
P(toPlainTime) \
|
||||
P(toPlainYearMonth) \
|
||||
P(toString) \
|
||||
P(toTemporalInstant) \
|
||||
P(toTimeString) \
|
||||
|
|
|
@ -417,6 +417,36 @@ PlainDate* date_from_fields(GlobalObject& global_object, Object& calendar, Objec
|
|||
return static_cast<PlainDate*>(date_object);
|
||||
}
|
||||
|
||||
// 12.1.25 YearMonthFromFields ( calendar, fields [ , options ] ),
|
||||
PlainYearMonth* year_month_from_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object* options)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Assert: Type(calendar) is Object.
|
||||
// 2. Assert: Type(fields) is Object.
|
||||
// 3. If options is not present, then
|
||||
// a. Set options to undefined.
|
||||
// 4. Else,
|
||||
// a. Assert: Type(options) is Object.
|
||||
|
||||
// 5. Let yearMonth be ? Invoke(calendar, "yearMonthFromFields", « fields, options »).
|
||||
auto year_month = Value(&calendar).invoke(global_object, vm.names.yearMonthFromFields, &fields, options ?: js_undefined());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 6. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto* year_month_object = year_month.to_object(global_object);
|
||||
if (!year_month_object)
|
||||
return {};
|
||||
if (!is<PlainYearMonth>(year_month_object)) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainYearMonth");
|
||||
return {};
|
||||
}
|
||||
|
||||
// 7. Return yearMonth.
|
||||
return static_cast<PlainYearMonth*>(year_month_object);
|
||||
}
|
||||
|
||||
// 12.1.28 CalendarEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-calendarequals
|
||||
bool calendar_equals(GlobalObject& global_object, Object& one, Object& two)
|
||||
{
|
||||
|
|
|
@ -49,6 +49,7 @@ Object* to_temporal_calendar(GlobalObject&, Value);
|
|||
Object* to_temporal_calendar_with_iso_default(GlobalObject&, Value);
|
||||
Object* get_temporal_calendar_with_iso_default(GlobalObject&, Object&);
|
||||
PlainDate* date_from_fields(GlobalObject&, Object& calendar, Object& fields, Object& options);
|
||||
PlainYearMonth* year_month_from_fields(GlobalObject&, Object& calendar, Object& fields, Object* options = nullptr);
|
||||
bool calendar_equals(GlobalObject&, Object& one, Object& two);
|
||||
Object* consolidate_calendars(GlobalObject&, Object& one, Object& two);
|
||||
bool is_iso_leap_year(i32 year);
|
||||
|
|
|
@ -7,9 +7,11 @@
|
|||
|
||||
#include <AK/TypeCasts.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDate.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
|
||||
#include <LibJS/Runtime/Temporal/PlainYearMonth.h>
|
||||
|
||||
namespace JS::Temporal {
|
||||
|
||||
|
@ -43,6 +45,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
|
|||
define_native_accessor(vm.names.inLeapYear, in_leap_year_getter, {}, Attribute::Configurable);
|
||||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(vm.names.toPlainYearMonth, to_plain_year_month, 0, attr);
|
||||
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
|
||||
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
|
||||
define_native_function(vm.names.equals, equals, 1, attr);
|
||||
|
@ -267,6 +270,32 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::in_leap_year_getter)
|
|||
return Value(calendar_in_leap_year(global_object, calendar, *temporal_date));
|
||||
}
|
||||
|
||||
// 3.3.16 Temporal.PlainDate.prototype.toPlainYearMonth ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.toplainyearmonth
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::to_plain_year_month)
|
||||
{
|
||||
// 1. Let temporalDate be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
||||
auto* temporal_date = typed_this(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 3. Let calendar be temporalDate.[[Calendar]].
|
||||
auto& calendar = temporal_date->calendar();
|
||||
|
||||
// 4. Let fieldNames be ? CalendarFields(calendar, « "monthCode", "year" »).
|
||||
auto field_names = calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv });
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 5. Let fields be ? PrepareTemporalFields(temporalDate, fieldNames, «»).
|
||||
auto* fields = prepare_temporal_fields(global_object, *temporal_date, field_names, {});
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 6. Return ? YearMonthFromFields(calendar, fields).
|
||||
return year_month_from_fields(global_object, calendar, *fields);
|
||||
}
|
||||
|
||||
// 3.3.18 Temporal.PlainDate.prototype.getISOFields ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.getisofields
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::get_iso_fields)
|
||||
{
|
||||
|
|
|
@ -32,7 +32,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(days_in_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(months_in_year_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(in_leap_year_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_year_month);
|
||||
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
||||
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 0", () => {
|
||||
expect(Temporal.PlainDate.prototype.toPlainYearMonth).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainDate = new Temporal.PlainDate(2021, 7, 6);
|
||||
const plainYearMonth = plainDate.toPlainYearMonth();
|
||||
expect(plainYearMonth.calendar).toBe(plainDate.calendar);
|
||||
expect(plainYearMonth.year).toBe(2021);
|
||||
expect(plainYearMonth.month).toBe(7);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue