mirror of
https://github.com/mblanke/holiday-travel-app.git
synced 2026-03-01 13:30:20 -05:00
Initial commit: Holiday Travel App with resort comparison, trip management, and multi-provider search
This commit is contained in:
26
lib/date.ts
Normal file
26
lib/date.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { addDays, differenceInDays, parseISO, format, isBefore } from "date-fns";
|
||||
|
||||
export function enumerateDatePairs(startISO: string, endISO: string, minNights: number, maxNights: number, maxPairs = 8) {
|
||||
const start = parseISO(startISO);
|
||||
const end = parseISO(endISO);
|
||||
const pairs: {out: string, back: string, nights: number}[] = [];
|
||||
let cursor = start;
|
||||
while (!isBefore(end, cursor) && pairs.length < maxPairs) {
|
||||
for (let n = minNights; n <= maxNights && pairs.length < maxPairs; n++) {
|
||||
const back = addDays(cursor, n);
|
||||
if (isBefore(end, back)) continue;
|
||||
pairs.push({ out: format(cursor, "yyyyMMdd"), back: format(back, "yyyyMMdd"), nights: n });
|
||||
}
|
||||
cursor = addDays(cursor, Math.max(1, Math.floor((differenceInDays(end, start) || 1) / (maxPairs))));
|
||||
}
|
||||
if (pairs.length === 0) {
|
||||
// fallback to exact minNights from start
|
||||
const back = addDays(start, minNights);
|
||||
pairs.push({ out: format(start, "yyyyMMdd"), back: format(back, "yyyyMMdd"), nights: minNights });
|
||||
}
|
||||
return pairs;
|
||||
}
|
||||
|
||||
export function isoToSkyscanner(iso: string) {
|
||||
return iso.replace(/-/g, "");
|
||||
}
|
||||
Reference in New Issue
Block a user