diff --git a/src/index.ts b/src/index.ts index b019093..b36257d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,20 +57,21 @@ async function getNextPrayer(mosquee: string, allowRecusion = true): Promise<[st const currentDate = DateTime.now(); const times = await getTimesMosquee(mosquee); - let index = times.findIndex(it => { - const date = it; - return date >= currentDate; - }); + const sorted = prayers + .map((prayer, i) => [prayer, times[i]] as [string, DateTime]) + .sort((a, b) => a[1].toMillis() - b[1].toMillis()); + + const index = sorted.findIndex(it => it[1] >= currentDate); if(index === -1) { if(allowRecusion) { timeCache.delete(mosquee); return getNextPrayer(mosquee, false); } - return [prayers[0], times[0]]; + return sorted[0]; } - return [prayers[index], times[index]]; + return sorted[index]; } async function getNextPrayerFormatted(mosquee: string, relative: boolean, userTimeZone?: string): Promise {