Skip to content

Commit 264e4b8

Browse files
authored
Editorial: Add early return in CalendarDateUntil (#3218)
This change makes it possible to change a check to an assertion in the polyfill.
1 parent d7c62f8 commit 264e4b8

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

polyfill/lib/calendar.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,8 @@ const nonIsoHelperBase = {
10551055
case 'year': {
10561056
// Sign is -1 if calendarTwo < calendarOne, 1 if calendarTwo > calendarOne
10571057
const sign = this.compareCalendarDates(calendarTwo, calendarOne);
1058-
// If dates are equal, return 0 date duration
1059-
if (!sign) {
1060-
return { years: 0, months: 0, weeks: 0, days: 0 };
1061-
}
1058+
// If dates were equal, this would have been checked in CalendarDateUntil
1059+
assert(sign, 'equal dates should have been checked earlier');
10621060
// Take the difference between the years of the two dates
10631061
const diffYears = calendarTwo.year - calendarOne.year;
10641062
// Take the difference between the days of the two dates

polyfill/lib/ecmascript.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,9 @@ export function CalendarDateAdd(calendar, isoDate, dateDuration, overflow) {
17241724
}
17251725

17261726
export function CalendarDateUntil(calendar, isoDate, isoOtherDate, largestUnit) {
1727+
if (CompareISODate(isoDate, isoOtherDate) === 0) {
1728+
return { years: 0, months: 0, weeks: 0, days: 0 };
1729+
}
17271730
return calendarImplForID(calendar).dateUntil(isoDate, isoOtherDate, largestUnit);
17281731
}
17291732

spec/calendar.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,11 @@ <h1>
630630
</dd>
631631
</dl>
632632
<emu-alg>
633+
1. Let _sign_ be CompareISODate(_one_, _two_).
634+
1. If _sign_ is 0, then
635+
1. Return ZeroDateDuration().
633636
1. If _calendar_ is *"iso8601"*, then
634-
1. Let _sign_ be -CompareISODate(_one_, _two_).
635-
1. If _sign_ = 0, return ZeroDateDuration().
637+
1. Set _sign_ to -sign.
636638
1. Let _years_ be 0.
637639
1. If _largestUnit_ is ~year~, then
638640
1. Let _candidateYears_ be _sign_.

0 commit comments

Comments
 (0)