Skip to content

Minor DRY fix for RoundDuration #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions lib/ecmascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4681,17 +4681,15 @@ export function RoundDuration(
let nanoseconds = bigInt(nanosecondsParam);
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
let calendar, zdtRelative;
// if it's a ZDT, it will be reassigned to a PDT below.
let relativeTo: Parameters<typeof MoveRelativeDate>[1] = relativeToParam;
if (relativeToParam) {
// A cast is used below because relativeTo will be either PlainDate or
// undefined for the rest of this long method (after any ZDT=>PlainDate
// conversion below), and TS isn't smart enough to know that the type has
// changed. See https://github.com/microsoft/TypeScript/issues/27706.
let relativeTo = relativeToParam as Temporal.PlainDate | undefined;
if (relativeTo) {
if (IsTemporalZonedDateTime(relativeTo)) {
zdtRelative = relativeTo;
const pdt = BuiltinTimeZoneGetPlainDateTimeFor(
GetSlot(relativeTo, TIME_ZONE),
GetSlot(relativeTo, INSTANT),
GetSlot(relativeTo, CALENDAR)
);
relativeTo = TemporalDateTimeToDate(pdt);
relativeTo = ToTemporalDate(relativeTo);
} else if (!IsTemporalDate(relativeTo)) {
throw new TypeError('starting point must be PlainDate or ZonedDateTime');
}
Expand Down