diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp index f790e53a6e..fc027a64aa 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.cpp @@ -45,6 +45,7 @@ void PlainYearMonthPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.with, with, 1, attr); define_native_function(vm.names.add, add, 1, attr); define_native_function(vm.names.subtract, subtract, 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.toString, to_string, 0, attr); define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr); @@ -378,6 +379,86 @@ JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::subtract) return TRY(year_month_from_fields(global_object, calendar, *added_date_fields, options_copy)); } +// 9.3.14 Temporal.PlainYearMonth.prototype.until ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.until +JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::until) +{ + // 1. Let yearMonth be the this value. + // 2. Perform ? RequireInternalSlot(yearMonth, [[InitializedTemporalYearMonth]]). + auto* year_month = TRY(typed_this_object(global_object)); + + // 3. Set other to ? ToTemporalYearMonth(other). + auto* other = TRY(to_temporal_year_month(global_object, vm.argument(0))); + + // 4. Let calendar be yearMonth.[[Calendar]]. + auto& calendar = year_month->calendar(); + + // 5. If ? CalendarEquals(calendar, other.[[Calendar]]) is false, throw a RangeError exception. + if (!TRY(calendar_equals(global_object, calendar, other->calendar()))) + return vm.throw_completion(global_object, ErrorType::TemporalDifferentCalendars); + + // 6. Set options to ? GetOptionsObject(options). + auto* options = TRY(get_options_object(global_object, vm.argument(1))); + + // 7. Let disallowedUnits be « "week", "day", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond" ». + auto disallowed_units = Vector { "week"sv, "day"sv, "hour"sv, "minute"sv, "second"sv, "millisecond"sv, "microsecond"sv, "nanosecond"sv }; + + // 8. Let smallestUnit be ? ToSmallestTemporalUnit(options, disallowedUnits, "month"). + auto smallest_unit = TRY(to_smallest_temporal_unit(global_object, *options, disallowed_units, "month"sv)); + + // 9. Let largestUnit be ? ToLargestTemporalUnit(options, disallowedUnits, "auto", "year"). + auto largest_unit = TRY(to_largest_temporal_unit(global_object, *options, disallowed_units, "auto"sv, "year")); + + // 10. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit). + TRY(validate_temporal_unit_range(global_object, *largest_unit, *smallest_unit)); + + // 11. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc"). + auto rounding_mode = TRY(to_temporal_rounding_mode(global_object, *options, "trunc"sv)); + + // 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, undefined, false). + auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, {}, false)); + + // 13. Let fieldNames be ? CalendarFields(calendar, « "monthCode", "year" »). + auto field_names = TRY(calendar_fields(global_object, calendar, { "monthCode"sv, "year"sv })); + + // 14. Let otherFields be ? PrepareTemporalFields(other, fieldNames, «»). + auto* other_fields = TRY(prepare_temporal_fields(global_object, *other, field_names, {})); + + // 15. Perform ! CreateDataPropertyOrThrow(otherFields, "day", 1𝔽). + MUST(other_fields->create_data_property_or_throw(vm.names.day, Value(1))); + + // 16. Let otherDate be ? DateFromFields(calendar, otherFields). + // FIXME: Spec doesn't pass required options, see https://github.com/tc39/proposal-temporal/issues/1685. + auto* other_date = TRY(date_from_fields(global_object, calendar, *other_fields, *options)); + + // 17. Let thisFields be ? PrepareTemporalFields(yearMonth, fieldNames, «»). + auto* this_fields = TRY(prepare_temporal_fields(global_object, *year_month, field_names, {})); + + // 18. Perform ! CreateDataPropertyOrThrow(thisFields, "day", 1𝔽). + MUST(this_fields->create_data_property_or_throw(vm.names.day, Value(1))); + + // 19. Let thisDate be ? DateFromFields(calendar, thisFields). + // FIXME: Spec doesn't pass required options, see https://github.com/tc39/proposal-temporal/issues/1685. + auto* this_date = TRY(date_from_fields(global_object, calendar, *this_fields, *options)); + + // 20. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit). + auto* until_options = TRY(merge_largest_unit_option(global_object, *options, *largest_unit)); + + // 21. Let result be ? CalendarDateUntil(calendar, thisDate, otherDate, untilOptions). + auto* result = TRY(calendar_date_until(global_object, calendar, this_date, other_date, *until_options)); + + // 22. If smallestUnit is "month" and roundingIncrement = 1, then + if (smallest_unit == "month"sv && rounding_increment == 1) { + // a. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0). + return TRY(create_temporal_duration(global_object, result->years(), result->months(), 0, 0, 0, 0, 0, 0, 0, 0)); + } + + // 23. Let result be ? RoundDuration(result.[[Years]], result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0, roundingIncrement, smallestUnit, roundingMode, thisDate). + auto round_result = TRY(round_duration(global_object, result->years(), result->months(), 0, 0, 0, 0, 0, 0, 0, 0, rounding_increment, *smallest_unit, rounding_mode, this_date)); + + // 24. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], 0, 0, 0, 0, 0, 0, 0, 0). + return TRY(create_temporal_duration(global_object, round_result.years, round_result.months, 0, 0, 0, 0, 0, 0, 0, 0)); +} + // 9.3.16 Temporal.PlainYearMonth.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.plainyearmonth.prototype.equals JS_DEFINE_NATIVE_FUNCTION(PlainYearMonthPrototype::equals) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h index f3a5e5b216..a30d25ac2b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainYearMonthPrototype.h @@ -33,6 +33,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(with); JS_DECLARE_NATIVE_FUNCTION(add); JS_DECLARE_NATIVE_FUNCTION(subtract); + JS_DECLARE_NATIVE_FUNCTION(until); JS_DECLARE_NATIVE_FUNCTION(equals); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_FUNCTION(to_locale_string); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.until.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.until.js new file mode 100644 index 0000000000..1ee71514ce --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/PlainYearMonth/PlainYearMonth.prototype.until.js @@ -0,0 +1,120 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Temporal.PlainYearMonth.prototype.until).toHaveLength(1); + }); + + test("basic functionality", () => { + const values = [ + [[0, 1], [0, 1], "PT0S"], + [[1, 2], [2, 3], "P1Y1M"], + [[0, 1], [1, 2], "P1Y1M"], + [[1, 2], [0, 1], "-P1Y1M"], + [[0, 1], [0, 12], "P11M"], + [[0, 12], [0, 1], "-P11M"], + ]; + for (const [args, argsOther, expected] of values) { + const plainYearMonth = new Temporal.PlainYearMonth(...args); + const other = new Temporal.PlainYearMonth(...argsOther); + expect(plainYearMonth.until(other).toString()).toBe(expected); + } + }); + + test("smallestUnit option", () => { + const plainYearMonth = new Temporal.PlainYearMonth(0, 1); + const other = new Temporal.PlainYearMonth(1, 2); + const values = [ + ["year", "P1Y"], + ["month", "P1Y1M"], + ]; + for (const [smallestUnit, expected] of values) { + expect(plainYearMonth.until(other, { smallestUnit }).toString()).toBe(expected); + } + }); + + test("largestUnit option", () => { + const plainYearMonth = new Temporal.PlainYearMonth(0, 1); + const other = new Temporal.PlainYearMonth(1, 2); + const values = [ + ["year", "P1Y1M"], + ["month", "P13M"], + ]; + for (const [largestUnit, expected] of values) { + expect(plainYearMonth.until(other, { largestUnit }).toString()).toBe(expected); + } + }); +}); + +describe("errors", () => { + test("this value must be a Temporal.PlainYearMonth object", () => { + expect(() => { + Temporal.PlainYearMonth.prototype.until.call("foo", {}); + }).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth"); + }); + + test("disallowed smallestUnit option values", () => { + const values = [ + "week", + "day", + "hour", + "minute", + "second", + "millisecond", + "microsecond", + "nanosecond", + ]; + for (const smallestUnit of values) { + const plainYearMonth = new Temporal.PlainYearMonth(1970, 1); + const other = new Temporal.PlainYearMonth(1970, 1); + expect(() => { + plainYearMonth.until(other, { smallestUnit }); + }).toThrowWithMessage( + RangeError, + `${smallestUnit} is not a valid value for option smallestUnit` + ); + } + }); + + test("disallowed largestUnit option values", () => { + const values = [ + "week", + "day", + "hour", + "minute", + "second", + "millisecond", + "microsecond", + "nanosecond", + ]; + for (const largestUnit of values) { + const plainYearMonth = new Temporal.PlainYearMonth(1970, 1); + const other = new Temporal.PlainYearMonth(1970, 1); + expect(() => { + plainYearMonth.until(other, { largestUnit }); + }).toThrowWithMessage( + RangeError, + `${largestUnit} is not a valid value for option largestUnit` + ); + } + }); + + test("cannot compare dates from different calendars", () => { + const calendarOne = { + toString() { + return "calendarOne"; + }, + }; + + const calendarTwo = { + toString() { + return "calendarTwo"; + }, + }; + + const plainYearMonthOne = new Temporal.PlainYearMonth(1970, 1, calendarOne); + const plainYearMonthTwo = new Temporal.PlainYearMonth(1970, 1, calendarTwo); + + expect(() => { + plainYearMonthOne.until(plainYearMonthTwo); + }).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars"); + }); +});