Skip to content

Commit 4a77bde

Browse files
committed
Polyfill: Extract addDaysISO from calendar helpers
Adding days to an ISO date does not have any calendar-specific behaviour so extract it out of the calendar helper objects. There were also some calls with a leftover cache parameter which was unused.
1 parent 92bbd2e commit 4a77bde

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

polyfill/lib/calendar.mjs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ function ISODateSurpasses(sign, baseDate, isoDate2, years, months, weeks, days)
151151
return false;
152152
}
153153

154+
function addDaysISO(isoDate, days) {
155+
return ES.BalanceISODate(isoDate.year, isoDate.month, isoDate.day + days);
156+
}
157+
154158
const impl = {};
155159

156160
impl['iso8601'] = {
@@ -844,7 +848,7 @@ const nonIsoHelperBase = {
844848
// that's currently worked around by a custom calendarToIsoDate
845849
// implementation in those calendars. So this optimization should be safe
846850
// for all ICU calendars.
847-
let testIsoEstimate = this.addDaysIso(isoEstimate, diffDays);
851+
let testIsoEstimate = addDaysISO(isoEstimate, diffDays);
848852
if (date.day > this.minimumMonthLength(date)) {
849853
// There's a chance that the calendar date is out of range. Throw or
850854
// constrain if so.
@@ -854,7 +858,7 @@ const nonIsoHelperBase = {
854858
throw new RangeErrorCtor(`day ${day} does not exist in month ${month} of year ${year}`);
855859
}
856860
// Back up a day at a time until we're not hanging over the month end
857-
testIsoEstimate = this.addDaysIso(testIsoEstimate, -1);
861+
testIsoEstimate = addDaysISO(testIsoEstimate, -1);
858862
testCalendarDate = this.isoToCalendarDate(testIsoEstimate, cache);
859863
}
860864
}
@@ -865,7 +869,7 @@ const nonIsoHelperBase = {
865869
let diff = simpleDateDiff(date, roundtripEstimate);
866870
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0) {
867871
const diffTotalDaysEstimate = diff.years * 365 + diff.months * 30 + diff.days;
868-
isoEstimate = this.addDaysIso(isoEstimate, diffTotalDaysEstimate);
872+
isoEstimate = addDaysISO(isoEstimate, diffTotalDaysEstimate);
869873
roundtripEstimate = this.isoToCalendarDate(isoEstimate, cache);
870874
diff = simpleDateDiff(date, roundtripEstimate);
871875
if (diff.years === 0 && diff.months === 0) {
@@ -878,7 +882,7 @@ const nonIsoHelperBase = {
878882
// distance to the target, starting with 8 days per step.
879883
let increment = 8;
880884
while (sign) {
881-
isoEstimate = this.addDaysIso(isoEstimate, sign * increment);
885+
isoEstimate = addDaysISO(isoEstimate, sign * increment);
882886
const oldRoundtripEstimate = roundtripEstimate;
883887
roundtripEstimate = this.isoToCalendarDate(isoEstimate, cache);
884888
const oldSign = sign;
@@ -905,7 +909,7 @@ const nonIsoHelperBase = {
905909
// To constrain, pick the earliest value
906910
const order = this.compareCalendarDates(roundtripEstimate, oldRoundtripEstimate);
907911
// If current value is larger, then back up to the previous value.
908-
if (order > 0) isoEstimate = this.addDaysIso(isoEstimate, -1);
912+
if (order > 0) isoEstimate = addDaysISO(isoEstimate, -1);
909913
sign = 0;
910914
}
911915
}
@@ -936,13 +940,9 @@ const nonIsoHelperBase = {
936940
const isoDate = this.calendarToIsoDate(calendarDate, overflow, cache);
937941
return this.isoToCalendarDate(isoDate, cache);
938942
},
939-
addDaysIso(isoDate, days) {
940-
const added = ES.BalanceISODate(isoDate.year, isoDate.month, isoDate.day + days);
941-
return added;
942-
},
943943
addDaysCalendar(calendarDate, days, cache) {
944944
const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
945-
const addedIso = this.addDaysIso(isoDate, days);
945+
const addedIso = addDaysISO(isoDate, days);
946946
const addedCalendar = this.isoToCalendarDate(addedIso, cache);
947947
return addedCalendar;
948948
},
@@ -956,7 +956,7 @@ const nonIsoHelperBase = {
956956
? -MathMax(day, this.daysInPreviousMonth(calendarDate, cache))
957957
: this.daysInMonth(calendarDate, cache);
958958
const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
959-
let addedIso = this.addDaysIso(isoDate, days, cache);
959+
let addedIso = addDaysISO(isoDate, days);
960960
calendarDate = this.isoToCalendarDate(addedIso, cache);
961961

962962
// Normally, we can advance one month by adding the number of days in the
@@ -967,7 +967,7 @@ const nonIsoHelperBase = {
967967
if (months > 0) {
968968
const monthsInOldYear = this.monthsInYear(oldCalendarDate, cache);
969969
while (calendarDate.month - 1 !== month % monthsInOldYear) {
970-
addedIso = this.addDaysIso(addedIso, -1, cache);
970+
addedIso = addDaysISO(addedIso, -1);
971971
calendarDate = this.isoToCalendarDate(addedIso, cache);
972972
}
973973
}
@@ -1062,11 +1062,11 @@ const nonIsoHelperBase = {
10621062
// Add enough days to get into the next month, without skipping it
10631063
const increment = day <= max - min ? max : min;
10641064
const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
1065-
const addedIsoDate = this.addDaysIso(isoDate, increment);
1065+
const addedIsoDate = addDaysISO(isoDate, increment);
10661066
const addedCalendarDate = this.isoToCalendarDate(addedIsoDate, cache);
10671067

10681068
// Now back up to the last day of the original month
1069-
const endOfMonthIso = this.addDaysIso(addedIsoDate, -addedCalendarDate.day);
1069+
const endOfMonthIso = addDaysISO(addedIsoDate, -addedCalendarDate.day);
10701070
const endOfMonthCalendar = this.isoToCalendarDate(endOfMonthIso, cache);
10711071
return endOfMonthCalendar.day;
10721072
},
@@ -1083,7 +1083,7 @@ const nonIsoHelperBase = {
10831083
if (min === max) return max;
10841084

10851085
const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
1086-
const lastDayOfPreviousMonthIso = this.addDaysIso(isoDate, -day);
1086+
const lastDayOfPreviousMonthIso = addDaysISO(isoDate, -day);
10871087
const lastDayOfPreviousMonthCalendar = this.isoToCalendarDate(lastDayOfPreviousMonthIso, cache);
10881088
return lastDayOfPreviousMonthCalendar.day;
10891089
},

0 commit comments

Comments
 (0)