mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:07:44 +00:00
LibJS: Implement Temporal.PlainDate.prototype.until
This commit is contained in:
parent
99c15741ba
commit
ddec3bc888
4 changed files with 333 additions and 0 deletions
|
@ -196,6 +196,7 @@
|
|||
M(StringRawCannotConvert, "Cannot convert property 'raw' to object from {}") \
|
||||
M(StringRepeatCountMustBe, "repeat count must be a {} number") \
|
||||
M(TemporalAmbiguousMonthOfPlainMonthDay, "Accessing month of PlainMonthDay is ambiguous, use monthCode instead") \
|
||||
M(TemporalDifferentCalendars, "Cannot compare dates from two different calendars") \
|
||||
M(TemporalDisambiguatePossibleInstantsEarlierZero, "Cannot disambiguate zero possible instants with mode \"earlier\"") \
|
||||
M(TemporalDisambiguatePossibleInstantsRejectMoreThanOne, "Cannot disambiguate two or more possible instants with mode \"reject\"") \
|
||||
M(TemporalDisambiguatePossibleInstantsRejectZero, "Cannot disambiguate zero possible instants with mode \"reject\"") \
|
||||
|
|
|
@ -60,6 +60,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
|
|||
define_native_function(vm.names.subtract, subtract, 1, attr);
|
||||
define_native_function(vm.names.with, with, 1, attr);
|
||||
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
|
||||
define_native_function(vm.names.until, until, 1, attr);
|
||||
define_native_function(vm.names.equals, equals, 1, attr);
|
||||
define_native_function(vm.names.toPlainDateTime, to_plain_date_time, 0, attr);
|
||||
define_native_function(vm.names.toZonedDateTime, to_zoned_date_time, 1, attr);
|
||||
|
@ -436,6 +437,73 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::with_calendar)
|
|||
return TRY(create_temporal_date(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), *calendar));
|
||||
}
|
||||
|
||||
// 3.3.23 Temporal.PlainDate.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.until
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::until)
|
||||
{
|
||||
// 1. Let temporalDate be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(temporalDate, [[InitializedTemporalDate]]).
|
||||
auto* temporal_date = TRY(typed_this_object(global_object));
|
||||
|
||||
// 3. Set other to ? ToTemporalDate(other).
|
||||
auto* other = TRY(to_temporal_date(global_object, vm.argument(0)));
|
||||
|
||||
// 4. If ? CalendarEquals(temporalDate.[[Calendar]], other.[[Calendar]]) is false, throw a RangeError exception.
|
||||
if (!TRY(calendar_equals(global_object, temporal_date->calendar(), other->calendar())))
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalDifferentCalendars);
|
||||
|
||||
// 5. Set options to ? GetOptionsObject(options).
|
||||
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
|
||||
|
||||
// 6. Let disallowedUnits be « "hour", "minute", "second", "millisecond", "microsecond", "nanosecond" ».
|
||||
Vector<StringView> disallowed_units { "hour"sv, "minute"sv, "second"sv, "millisecond"sv, "microsecond"sv, "nanosecond"sv };
|
||||
|
||||
// 7. Let smallestUnit be ? ToSmallestTemporalUnit(options, disallowedUnits, "day").
|
||||
auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, *options, disallowed_units, "day"sv));
|
||||
|
||||
// 8. Let largestUnit be ? ToLargestTemporalUnit(options, disallowedUnits, "auto", "day").
|
||||
auto largest_unit = TRY(to_largest_temporal_unit(global_object, *options, disallowed_units, "auto"sv, "day"sv));
|
||||
|
||||
// 9. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit).
|
||||
TRY(validate_temporal_unit_range(global_object, largest_unit, *smallest_unit));
|
||||
|
||||
// 10. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
|
||||
auto rounding_mode = TRY(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
|
||||
|
||||
// 11. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, undefined, false).
|
||||
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, {}, false));
|
||||
|
||||
// 12. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit).
|
||||
auto* until_options = TRY(merge_largest_unit_option(global_object, *options, move(largest_unit)));
|
||||
|
||||
// 13. Let result be ? CalendarDateUntil(temporalDate.[[Calendar]], temporalDate, other, untilOptions).
|
||||
auto* result = TRY(calendar_date_until(global_object, temporal_date->calendar(), temporal_date, other, *until_options));
|
||||
|
||||
// NOTE: Result can be reassigned by 14.b, `result` above has the type `Duration*` from calendar_date_until while 14.b has the type `RoundedDuration` from round_duration.
|
||||
// Thus, we must store the individual parts we're interested in.
|
||||
auto years = result->years();
|
||||
auto months = result->months();
|
||||
auto weeks = result->weeks();
|
||||
auto days = result->days();
|
||||
|
||||
// 14. If smallestUnit is not "day" or roundingIncrement ≠ 1, then
|
||||
if (*smallest_unit != "day"sv || rounding_increment != 1) {
|
||||
// a. Let relativeTo be ! CreateTemporalDateTime(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], 0, 0, 0, 0, 0, 0, temporalDate.[[Calendar]]).
|
||||
auto* relative_to = MUST(create_temporal_date_time(global_object, temporal_date->iso_year(), temporal_date->iso_month(), temporal_date->iso_day(), 0, 0, 0, 0, 0, 0, temporal_date->calendar()));
|
||||
|
||||
// b. Set result to ? RoundDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], 0, 0, 0, 0, 0, 0, roundingIncrement, smallestUnit, roundingMode, relativeTo).
|
||||
// See NOTE above about why this is done.
|
||||
auto rounded_result = TRY(round_duration(global_object, years, months, weeks, days, 0, 0, 0, 0, 0, 0, rounding_increment, *smallest_unit, rounding_mode, relative_to));
|
||||
years = rounded_result.years;
|
||||
months = rounded_result.months;
|
||||
weeks = rounded_result.weeks;
|
||||
days = rounded_result.days;
|
||||
}
|
||||
|
||||
// 15. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], 0, 0, 0, 0, 0, 0).
|
||||
// See NOTE above about why `result` isn't used.
|
||||
return TRY(create_temporal_duration(global_object, years, months, weeks, days, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
// 3.3.25 Temporal.PlainDate.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.equals
|
||||
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::equals)
|
||||
{
|
||||
|
|
|
@ -42,6 +42,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(subtract);
|
||||
JS_DECLARE_NATIVE_FUNCTION(with);
|
||||
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
|
||||
JS_DECLARE_NATIVE_FUNCTION(until);
|
||||
JS_DECLARE_NATIVE_FUNCTION(equals);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_plain_date_time);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue