LibJS: Implement Temporal.PlainDate.prototype.since

This commit is contained in:
Luke Wilde 2021-11-15 21:05:45 +00:00 committed by Linus Groh
parent ddec3bc888
commit ac65fb40d9
Notes: sideshowbarker 2024-07-18 01:04:49 +09:00
5 changed files with 346 additions and 0 deletions

View file

@ -225,6 +225,21 @@ ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject& global_object,
return option.as_string().string();
}
// 13.9 NegateTemporalRoundingMode ( roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-negatetemporalroundingmode
StringView negate_temporal_rounding_mode(String const& rounding_mode)
{
// 1. If roundingMode is "ceil", return "floor".
if (rounding_mode == "ceil"sv)
return "floor"sv;
// 2. If roundingMode is "floor", return "ceil".
if (rounding_mode == "floor"sv)
return "ceil"sv;
// 3. Return roundingMode.
return rounding_mode;
}
// 13.10 ToTemporalOffset ( normalizedOptions, fallback ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaloffset
ThrowCompletionOr<String> to_temporal_offset(GlobalObject& global_object, Object const& normalized_options, String const& fallback)
{

View file

@ -104,6 +104,7 @@ ThrowCompletionOr<Variant<String, NumberType>> get_string_or_number_option(Globa
ThrowCompletionOr<String> to_temporal_overflow(GlobalObject&, Object const& normalized_options);
ThrowCompletionOr<String> to_temporal_disambiguation(GlobalObject&, Object const& normalized_options);
ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
StringView negate_temporal_rounding_mode(String const& rounding_mode);
ThrowCompletionOr<String> to_temporal_offset(GlobalObject&, Object const& normalized_options, String const& fallback);
ThrowCompletionOr<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
ThrowCompletionOr<String> to_show_time_zone_name_option(GlobalObject&, Object const& normalized_options);

View file

@ -61,6 +61,7 @@ void PlainDatePrototype::initialize(GlobalObject& global_object)
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.since, since, 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);
@ -504,6 +505,67 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::until)
return TRY(create_temporal_duration(global_object, years, months, weeks, days, 0, 0, 0, 0, 0, 0));
}
// 3.3.24 Temporal.PlainDate.prototype.since ( other [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaindate.prototype.since
JS_DEFINE_NATIVE_FUNCTION(PlainDatePrototype::since)
{
// 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. Set roundingMode to ! NegateTemporalRoundingMode(roundingMode).
rounding_mode = negate_temporal_rounding_mode(rounding_mode);
// 12. Let roundingIncrement be ? ToTemporalRoundingIncrement(options, undefined, false).
auto rounding_increment = TRY(to_temporal_rounding_increment(global_object, *options, {}, false));
// 13. Let untilOptions be ? MergeLargestUnitOption(options, largestUnit).
auto* until_options = TRY(merge_largest_unit_option(global_object, *options, move(largest_unit)));
// 14. 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));
// 15. If smallestUnit is "day" and roundingIncrement = 1, then
if (*smallest_unit == "day"sv && rounding_increment == 1) {
// a. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], 0, 0, 0, 0, 0, 0).
return TRY(create_temporal_duration(global_object, -result->years(), -result->months(), -result->weeks(), -result->days(), 0, 0, 0, 0, 0, 0));
}
// 16. 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()));
// 17. Set result to ? RoundDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], 0, 0, 0, 0, 0, 0, roundingIncrement, smallestUnit, roundingMode, relativeTo).
auto round_result = TRY(round_duration(global_object, result->years(), result->months(), result->weeks(), result->days(), 0, 0, 0, 0, 0, 0, rounding_increment, *smallest_unit, rounding_mode, relative_to));
// 18. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], 0, 0, 0, 0, 0, 0).
// NOTE: `result` here refers to `round_result`.
return TRY(create_temporal_duration(global_object, -round_result.years, -round_result.months, -round_result.weeks, -round_result.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

@ -43,6 +43,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(with);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(until);
JS_DECLARE_NATIVE_FUNCTION(since);
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,267 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.prototype.since).toHaveLength(1);
});
// FIXME: All the checks for years, months, weeks and days have to check for -0 instead of 0.
// This is because CreateTemporalDuration in the spec doesn't convert -0 to 0 unlike the polyfill or what
// the test262 tests expect.
// Once this has been fixed in the spec, change the -0 checks for these properties to be just 0.
test("basic functionality", () => {
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
const sinceDuration = dateTwo.since(dateOne);
expect(sinceDuration.years).toBe(-0);
expect(sinceDuration.months).toBe(-0);
expect(sinceDuration.weeks).toBe(-0);
expect(sinceDuration.days).toBe(406);
expect(sinceDuration.hours).toBe(0);
expect(sinceDuration.minutes).toBe(0);
expect(sinceDuration.seconds).toBe(0);
expect(sinceDuration.milliseconds).toBe(0);
expect(sinceDuration.microseconds).toBe(0);
expect(sinceDuration.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.since(equalDateOne));
checkResults(equalDateTwo.since(equalDateTwo));
checkResults(equalDateOne.since(equalDateTwo));
checkResults(equalDateTwo.since(equalDateOne));
});
test("negative direction", () => {
const dateOne = new Temporal.PlainDate(2021, 11, 14);
const dateTwo = new Temporal.PlainDate(2022, 12, 25);
const sinceDuration = dateOne.since(dateTwo);
expect(sinceDuration.years).toBe(-0);
expect(sinceDuration.months).toBe(-0);
expect(sinceDuration.weeks).toBe(-0);
expect(sinceDuration.days).toBe(-406);
expect(sinceDuration.hours).toBe(0);
expect(sinceDuration.minutes).toBe(0);
expect(sinceDuration.seconds).toBe(0);
expect(sinceDuration.milliseconds).toBe(0);
expect(sinceDuration.microseconds).toBe(0);
expect(sinceDuration.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 = dateTwo.since(dateOne, singularOptions);
for (const property of propertiesToCheck)
expect(positiveSingularResult[property]).toBe(durationLike[property]);
const positivePluralResult = dateTwo.since(dateOne, pluralOptions);
for (const property of propertiesToCheck)
expect(positivePluralResult[property]).toBe(durationLike[property]);
// Negative direction
const negativeSingularResult = dateOne.since(dateTwo, singularOptions);
for (const property of propertiesToCheck)
expect(negativeSingularResult[property]).toBe(-durationLike[property]);
const negativePluralResult = dateOne.since(dateTwo, 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 dateTwo = new Temporal.PlainDate(2022, 12, 25);
const sinceDuration = dateTwo.since("2021-11-14");
expect(sinceDuration.years).toBe(-0);
expect(sinceDuration.months).toBe(-0);
expect(sinceDuration.weeks).toBe(-0);
expect(sinceDuration.days).toBe(406);
expect(sinceDuration.hours).toBe(0);
expect(sinceDuration.minutes).toBe(0);
expect(sinceDuration.seconds).toBe(0);
expect(sinceDuration.milliseconds).toBe(0);
expect(sinceDuration.microseconds).toBe(0);
expect(sinceDuration.nanoseconds).toBe(0);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDate object", () => {
expect(() => {
Temporal.PlainDate.prototype.since.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.since(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.since(dateTwo, singularSmallestUnitOptions);
}).toThrowWithMessage(
RangeError,
`${smallestUnit} is not a valid value for option smallestUnit`
);
expect(() => {
dateOne.since(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.since(dateTwo, singularLargestUnitOptions);
}).toThrowWithMessage(
RangeError,
`${largestUnit} is not a valid value for option largestUnit`
);
expect(() => {
dateOne.since(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) {
for (const disallowedUnit of disallowedLargestUnits) {
const pluralSmallestUnit = `${smallestUnit}s`;
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.since(dateTwo, singularSmallestSingularDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
expect(() => {
dateOne.since(dateTwo, singularSmallestPluralDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
expect(() => {
dateOne.since(dateTwo, pluralSmallestSingularDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
expect(() => {
dateOne.since(dateTwo, pluralSmallestPluralDisallowedOptions);
}).toThrowWithMessage(
RangeError,
`Invalid unit range, ${smallestUnit} is larger than ${disallowedUnit}`
);
}
}
});
});