UNPKG

@kermank/slots

Version:

A TypeScript library for handling time slots, scheduling, and timezone operations

30 lines (29 loc) 966 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.maxSlotsPerDayRule = void 0; /** * Creates a rule that limits the number of slots per day */ const maxSlotsPerDayRule = (maxSlots) => (slots) => { // Group slots by day const slotsByDay = new Map(); // Group slots by day slots.forEach(slot => { const day = slot.start.startOf('day').toISO() || ''; if (!slotsByDay.has(day)) { slotsByDay.set(day, []); } slotsByDay.get(day).push(slot); }); // For each day that has > maxSlots, return the excess slots as forbidden return Array.from(slotsByDay.entries()) .flatMap(([_, daySlots]) => { if (daySlots.length > maxSlots) { return daySlots .sort((a, b) => a.start.toMillis() - b.start.toMillis()) .slice(maxSlots); } return []; }); }; exports.maxSlotsPerDayRule = maxSlotsPerDayRule;