From ff37a6c60654135bec3c1fbc47127d49879b00db Mon Sep 17 00:00:00 2001 From: Borut Balazek Date: Fri, 18 Oct 2024 18:51:54 +0200 Subject: [PATCH] fix: a few more fixes to satisfy tests --- packages/recurrence/src/Recurrence.ts | 34 +++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/recurrence/src/Recurrence.ts b/packages/recurrence/src/Recurrence.ts index ebfd9243..4af21463 100644 --- a/packages/recurrence/src/Recurrence.ts +++ b/packages/recurrence/src/Recurrence.ts @@ -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.');