Skip to content

Commit

Permalink
fix: a few more fixes to satisfy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobalazek committed Oct 18, 2024
1 parent e2e7966 commit ff37a6c
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions packages/recurrence/src/Recurrence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,29 +179,33 @@ export class Recurrence {
}

getDatesBetween(startDate: Date, endDate: Date): Date[] {
if (startDate > endDate) {
return [];
}

const dates: Date[] = [];
let iterations = 0;
let occurrences = 0;

let currentDate = new Date(this.options.startsAt.getTime());
const maxCount = this.options.count
? Math.min(this.options.count, Number.MAX_SAFE_INTEGER)
: Number.MAX_SAFE_INTEGER;
let iterations = 0;
let currentDate = new Date(this.options.startsAt);

const adjustedEndDate = this.options.endsAt
? new Date(Math.min(endDate.getTime(), this.options.endsAt.getTime()))
: endDate;
while (currentDate < startDate) {
currentDate = this._incrementDate(currentDate);
}

while (
currentDate <= adjustedEndDate &&
(this.options.count === undefined || occurrences < this.options.count)
) {
if (this._matchesOptions(currentDate)) {
if (currentDate >= startDate && currentDate <= adjustedEndDate) {
dates.push(new Date(currentDate.getTime()));
}
occurrences++;
while (currentDate <= endDate && dates.length < maxCount) {
if (this._matchesOptions(currentDate) && this._isWithinDateRange(currentDate)) {
dates.push(new Date(currentDate));
}

currentDate = this._incrementDate(currentDate);

if (this.options.endsAt && currentDate > this.options.endsAt) {
break;
}

iterations++;
if (iterations > this._maxIterations) {
throw new Error('Too many iterations. Infinite loop detected.');
Expand Down

0 comments on commit ff37a6c

Please sign in to comment.