Skip to content

Commit

Permalink
Polyfill: Refactor UnbalanceDateDurationRelative to match spec changes
Browse files Browse the repository at this point in the history
This applies the spec changes in the previous commits to
UnbalanceDateDurationRelative in the polyfill.
  • Loading branch information
ptomato committed Jan 31, 2024
1 parent 5c59914 commit 45b5e1f
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3445,14 +3445,18 @@ export function BalanceTimeDurationRelative(
export function UnbalanceDateDurationRelative(years, months, weeks, days, largestUnit, plainRelativeTo, calendarRec) {
// calendarRec must have looked up dateAdd and dateUntil
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
const defaultLargestUnit = DefaultTemporalLargestUnit(years, months, weeks, days, 0, 0, 0, 0, 0, 0);
const effectiveLargestUnit = LargerOfTwoTemporalUnits(largestUnit, 'day');
if (LargerOfTwoTemporalUnits(defaultLargestUnit, effectiveLargestUnit) === effectiveLargestUnit) {
// no-op
return { years, months, weeks, days };
}
if (!calendarRec) throw new RangeError(`a starting point is required for ${largestUnit}s balancing`);

switch (largestUnit) {
switch (effectiveLargestUnit) {
case 'year':
// no-op
return { years, months, weeks, days };
throw new Error('assert not reached');
case 'month': {
if (years === 0) return { years: 0, months, weeks, days };
if (!calendarRec) throw new RangeError('a starting point is required for months balancing');
// balance years down to months
const later = CalendarDateAdd(calendarRec, plainRelativeTo, new TemporalDuration(years));
const untilOptions = ObjectCreate(null);
Expand All @@ -3462,17 +3466,13 @@ export function UnbalanceDateDurationRelative(years, months, weeks, days, larges
return { years: 0, months: months + yearsInMonths, weeks, days };
}
case 'week': {
if (years === 0 && months === 0) return { years: 0, months: 0, weeks, days };
if (!calendarRec) throw new RangeError('a starting point is required for weeks balancing');
// balance years and months down to days
const later = CalendarDateAdd(calendarRec, plainRelativeTo, new TemporalDuration(years, months));
const yearsMonthsInDays = DaysUntil(plainRelativeTo, later);
return { years: 0, months: 0, weeks, days: days + yearsMonthsInDays };
}
default: {
// largestUnit is "day", or any time unit
if (years === 0 && months === 0 && weeks === 0) return { years: 0, months: 0, weeks: 0, days };
if (!calendarRec) throw new RangeError('a starting point is required for balancing calendar units');
// balance years, months, and weeks down to days
const later = CalendarDateAdd(calendarRec, plainRelativeTo, new TemporalDuration(years, months, weeks));
const yearsMonthsWeeksInDays = DaysUntil(plainRelativeTo, later);
Expand Down

0 comments on commit 45b5e1f

Please sign in to comment.