mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-25 19:02:07 -05:00
LibJS: Implement Temporal.PlainYearMonth.prototype.toPlainDate()
This commit is contained in:
parent
99adb54391
commit
2c222ba40b
5 changed files with 115 additions and 0 deletions
|
@ -113,6 +113,31 @@ ThrowCompletionOr<Vector<String>> calendar_fields(GlobalObject& global_object, O
|
|||
return result;
|
||||
}
|
||||
|
||||
// 12.1.6 CalendarMergeFields ( calendar, fields, additionalFields ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmergefields
|
||||
ThrowCompletionOr<Object*> calendar_merge_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object& additional_fields)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let mergeFields be ? GetMethod(calendar, "mergeFields").
|
||||
auto* merge_fields = TRY(Value(&calendar).get_method(global_object, vm.names.mergeFields));
|
||||
|
||||
// 2. If mergeFields is undefined, then
|
||||
if (!merge_fields) {
|
||||
// a. Return ? DefaultMergeFields(fields, additionalFields).
|
||||
return TRY(default_merge_fields(global_object, fields, additional_fields));
|
||||
}
|
||||
|
||||
// 3. Let result be ? Call(mergeFields, calendar, « fields, additionalFields »).
|
||||
auto result = TRY(call(global_object, merge_fields, &calendar, &fields, &additional_fields));
|
||||
|
||||
// 4. If Type(result) is not Object, throw a TypeError exception.
|
||||
if (!result.is_object())
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, result.to_string_without_side_effects());
|
||||
|
||||
// 5. Return result.
|
||||
return &result.as_object();
|
||||
}
|
||||
|
||||
// 12.1.9 CalendarYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendaryear
|
||||
ThrowCompletionOr<double> calendar_year(GlobalObject& global_object, Object& calendar, Object& date_like)
|
||||
{
|
||||
|
|
|
@ -35,6 +35,7 @@ bool is_builtin_calendar(String const& identifier);
|
|||
ThrowCompletionOr<Calendar*> get_builtin_calendar(GlobalObject&, String const& identifier);
|
||||
Calendar* get_iso8601_calendar(GlobalObject&);
|
||||
ThrowCompletionOr<Vector<String>> calendar_fields(GlobalObject&, Object& calendar, Vector<StringView> const& field_names);
|
||||
ThrowCompletionOr<Object*> calendar_merge_fields(GlobalObject&, Object& calendar, Object& fields, Object& additional_fields);
|
||||
ThrowCompletionOr<double> calendar_year(GlobalObject&, Object& calendar, Object& date_like);
|
||||
ThrowCompletionOr<double> calendar_month(GlobalObject&, Object& calendar, Object& date_like);
|
||||
ThrowCompletionOr<String> calendar_month_code(GlobalObject&, Object& calendar, Object& date_like);
|
||||
|
|
|
@ -45,6 +45,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
|
||||
define_native_function(vm.names.toJSON, to_json, 0, attr);
|
||||
define_native_function(vm.names.valueOf, value_of, 0, attr);
|
||||
define_native_function(vm.names.toPlainDate, to_plain_date, 1, attr);
|
||||
define_native_function(vm.names.getISOFields, get_iso_fields, 0, attr);
|
||||
}
|
||||
|
||||
|
@ -289,6 +290,66 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::value_of)
|
|||
return {};
|
||||
}
|
||||
|
||||
// 9.3.21 Temporal.PlainYearMonth.prototype.toPlainDate ( item ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.toplaindate
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::to_plain_date)
|
||||
{
|
||||
auto item = vm.argument(0);
|
||||
|
||||
// 1. Let yearMonth be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]).
|
||||
auto* year_month = typed_this_object(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 3. If Type(item) is not Object, then
|
||||
if (!item.is_object()) {
|
||||
// a. Throw a TypeError exception.
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, item);
|
||||
return {};
|
||||
}
|
||||
|
||||
// 4. Let calendar be yearMonth.[[Calendar]].
|
||||
auto& calendar = year_month->calendar();
|
||||
|
||||
// 5. Let receiverFieldNames be ? CalendarFields(calendar, « "monthCode", "year" »).
|
||||
auto receiver_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv }));
|
||||
|
||||
// 6. Let fields be ? PrepareTemporalFields(yearMonth, receiverFieldNames, «»).
|
||||
auto* fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *year_month, receiver_field_names, {}));
|
||||
|
||||
// 7. Let inputFieldNames be ? CalendarFields(calendar, « "day" »).
|
||||
auto input_field_names = TRY_OR_DISCARD(calendar_fields(global_object, calendar, { "day"sv }));
|
||||
|
||||
// 8. Let inputFields be ? PrepareTemporalFields(item, inputFieldNames, «»).
|
||||
auto* input_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, item.as_object(), input_field_names, {}));
|
||||
|
||||
// 9. Let mergedFields be ? CalendarMergeFields(calendar, fields, inputFields).
|
||||
auto* merged_fields = TRY_OR_DISCARD(calendar_merge_fields(global_object, calendar, *fields, *input_fields));
|
||||
|
||||
// 10. Let mergedFieldNames be the List containing all the elements of receiverFieldNames followed by all the elements of inputFieldNames, with duplicate elements removed.
|
||||
Vector<String> merged_field_names;
|
||||
for (auto& field_name : receiver_field_names) {
|
||||
if (!merged_field_names.contains_slow(field_name))
|
||||
merged_field_names.append(move(field_name));
|
||||
}
|
||||
for (auto& field_name : input_field_names) {
|
||||
if (!merged_field_names.contains_slow(field_name))
|
||||
merged_field_names.append(move(field_name));
|
||||
}
|
||||
|
||||
// 11. Set mergedFields to ? PrepareTemporalFields(mergedFields, mergedFieldNames, «»).
|
||||
merged_fields = TRY_OR_DISCARD(prepare_temporal_fields(global_object, *merged_fields, merged_field_names, {}));
|
||||
|
||||
// 12. Let options be ! OrdinaryObjectCreate(null).
|
||||
auto* options = Object::create(global_object, nullptr);
|
||||
|
||||
// 13. Perform ! CreateDataPropertyOrThrow(options, "overflow", "reject").
|
||||
MUST(options->create_data_property_or_throw(vm.names.overflow, js_string(vm, vm.names.reject.as_string())));
|
||||
|
||||
// 14. Return ? DateFromFields(calendar, mergedFields, options).
|
||||
return TRY_OR_DISCARD(date_from_fields(global_object, calendar, *merged_fields, *options));
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
|
|
|
@ -35,6 +35,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_json);
|
||||
JS_DECLARE_NATIVE_FUNCTION(value_of);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
|
||||
JS_DECLARE_NATIVE_FUNCTION(get_iso_fields);
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
describe("normal behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.PlainYearMonth.prototype.toPlainDate).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
const plainDate = plainYearMonth.toPlainDate({ day: 6 });
|
||||
expect(plainDate.equals(new Temporal.PlainDate(2021, 7, 6))).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("argument must be an object", () => {
|
||||
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
expect(() => {
|
||||
plainYearMonth.toPlainDate(42);
|
||||
}).toThrowWithMessage(TypeError, "42 is not an object");
|
||||
});
|
||||
|
||||
test("day field is required", () => {
|
||||
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
|
||||
expect(() => {
|
||||
plainYearMonth.toPlainDate({});
|
||||
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue