1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibJS: Implement Temporal.PlainDate.prototype.until

This commit is contained in:
Luke Wilde 2021-11-15 01:29:58 +00:00 committed by Linus Groh
parent 99c15741ba
commit ddec3bc888
4 changed files with 333 additions and 0 deletions

View file

@ -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\"") \

View file

@ -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)
{

View file

@ -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);

View file

@ -0,0 +1,263 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.prototype.until).toHaveLength(1);
});
test("basic functionality", () => {
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
const untilDuration = dateOne.until(dateTwo);
expect(untilDuration.years).toBe(0);
expect(untilDuration.months).toBe(0);
expect(untilDuration.weeks).toBe(0);
expect(untilDuration.days).toBe(406);
expect(untilDuration.hours).toBe(0);
expect(untilDuration.minutes).toBe(0);
expect(untilDuration.seconds).toBe(0);
expect(untilDuration.milliseconds).toBe(0);
expect(untilDuration.microseconds).toBe(0);
expect(untilDuration.nanoseconds).toBe(0);
});
test("equal dates", () => {
const equalDateOne = new Temporal.PlainDate(1, 1, 1);
const equalDateTwo = new Temporal.PlainDate(1, 1, 1);
const checkResults = result => {
expect(result.years).toBe(0);
expect(result.months).toBe(0);
expect(result.weeks).toBe(0);
expect(result.days).toBe(0);
expect(result.hours).toBe(0);
expect(result.minutes).toBe(0);
expect(result.seconds).toBe(0);
expect(result.milliseconds).toBe(0);
expect(result.microseconds).toBe(0);
expect(result.nanoseconds).toBe(0);
};
checkResults(equalDateOne.until(equalDateOne));
checkResults(equalDateTwo.until(equalDateTwo));
checkResults(equalDateOne.until(equalDateTwo));
checkResults(equalDateTwo.until(equalDateOne));
});
test("negative direction", () => {
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
const untilDuration = dateTwo.until(dateOne);
expect(untilDuration.years).toBe(0);
expect(untilDuration.months).toBe(0);
expect(untilDuration.weeks).toBe(0);
expect(untilDuration.days).toBe(-406);
expect(untilDuration.hours).toBe(0);
expect(untilDuration.minutes).toBe(0);
expect(untilDuration.seconds).toBe(0);
expect(untilDuration.milliseconds).toBe(0);
expect(untilDuration.microseconds).toBe(0);
expect(untilDuration.nanoseconds).toBe(0);
});
test("largestUnit option", () => {
const values = [
["year", { years: 1, months: 1, days: 11 }],
["month", { months: 13, days: 11 }],
["week", { weeks: 58 }],
["day", { days: 406 }],
];
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
for (const [largestUnit, durationLike] of values) {
const singularOptions = { largestUnit };
const pluralOptions = { largestUnit: `${largestUnit}s` };
const propertiesToCheck = Object.keys(durationLike);
// Positive direction
const positiveSingularResult = dateOne.until(dateTwo, singularOptions);
for (const property of propertiesToCheck)
expect(positiveSingularResult[property]).toBe(durationLike[property]);
const positivePluralResult = dateOne.until(dateTwo, pluralOptions);
for (const property of propertiesToCheck)
expect(positivePluralResult[property]).toBe(durationLike[property]);
// Negative direction
const negativeSingularResult = dateTwo.until(dateOne, singularOptions);
for (const property of propertiesToCheck)
expect(negativeSingularResult[property]).toBe(-durationLike[property]);
const negativePluralResult = dateTwo.until(dateOne, pluralOptions);
for (const property of propertiesToCheck)
expect(negativePluralResult[property]).toBe(-durationLike[property]);
}
});
// FIXME: Unskip when plain date string parsing is implemented.
test.skip("PlainDate string argument", () => {
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const untilDuration = dateOne.until("2022-12-25");
expect(untilDuration.years).toBe(0);
expect(untilDuration.months).toBe(0);
expect(untilDuration.weeks).toBe(0);
expect(untilDuration.days).toBe(406);
expect(untilDuration.hours).toBe(0);
expect(untilDuration.minutes).toBe(0);
expect(untilDuration.seconds).toBe(0);
expect(untilDuration.milliseconds).toBe(0);
expect(untilDuration.microseconds).toBe(0);
expect(untilDuration.nanoseconds).toBe(0);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDate object", () => {
expect(() => {
Temporal.PlainDate.prototype.until.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
});
test("cannot compare dates from different calendars", () => {
const calendarOne = {
toString() {
return "calendarOne";
},
};
const calendarTwo = {
toString() {
return "calendarTwo";
},
};
const dateOneWithCalendar = new Temporal.PlainDate(2021, 11, 14, calendarOne);
const dateTwoWithCalendar = new Temporal.PlainDate(2022, 12, 25, calendarTwo);
expect(() => {
dateOneWithCalendar.until(dateTwoWithCalendar);
}).toThrowWithMessage(RangeError, "Cannot compare dates from two different calendars");
});
test("disallowed units", () => {
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
const disallowedUnits = [
"hour",
"minute",
"second",
"millisecond",
"microsecond",
"nanosecond",
];
for (const smallestUnit of disallowedUnits) {
const singularSmallestUnitOptions = { smallestUnit };
const pluralSmallestUnitOptions = { smallestUnit: `${smallestUnit}s` };
expect(() => {
dateOne.until(dateTwo, singularSmallestUnitOptions);
}).toThrowWithMessage(
RangeError,
`${smallestUnit} is not a valid value for option smallestUnit`
);
expect(() => {
dateOne.until(dateTwo, pluralSmallestUnitOptions);
}).toThrowWithMessage(
RangeError,
`${smallestUnit} is not a valid value for option smallestUnit`
);
}
for (const largestUnit of disallowedUnits) {
const singularLargestUnitOptions = { largestUnit };
const pluralLargestUnitOptions = { largestUnit: `${largestUnit}s` };
expect(() => {
dateOne.until(dateTwo, singularLargestUnitOptions);
}).toThrowWithMessage(
RangeError,
`${largestUnit} is not a valid value for option largestUnit`
);
expect(() => {
dateOne.until(dateTwo, pluralLargestUnitOptions);
}).toThrowWithMessage(
RangeError,
`${largestUnit} is not a valid value for option largestUnit`
);
}
});
test("invalid unit range", () => {
// smallestUnit -> disallowed largestUnits, see validate_temporal_unit_range
// Note that all the "smallestUnits" are all the allowedUnits.
const invalidRanges = [
["year", ["month", "week", "day"]],
["month", ["week", "day"]],
["week", ["day"]],
];
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
for (const [smallestUnit, disallowedLargestUnits] of invalidRanges) {
const pluralSmallestUnit = `${smallestUnit}s`;
for (const disallowedUnit of disallowedLargestUnits) {
const pluralDisallowedUnit = `${disallowedUnit}s`;
const singularSmallestSingularDisallowedOptions = {
smallestUnit,
largestUnit: disallowedUnit,
};
const singularSmallestPluralDisallowedOptions = {
smallestUnit,
largestUnit: pluralDisallowedUnit,
};
const pluralSmallestSingularDisallowedOptions = {
smallestUnit: pluralSmallestUnit,
largestUnit: disallowedUnit,
};
const pluralSmallestPluralDisallowedOptions = {
smallestUnit: pluralSmallestUnit,
largestUnit: disallowedUnit,
};
expect(() => {
dateOne.until(dateTwo, singularSmallestSingularDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
expect(() => {
dateOne.until(dateTwo, singularSmallestPluralDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
expect(() => {
dateOne.until(dateTwo, pluralSmallestSingularDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
expect(() => {
dateOne.until(dateTwo, pluralSmallestPluralDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
}
}
});
});