@kermank/slots
Version:
A TypeScript library for handling time slots, scheduling, and timezone operations
43 lines (42 loc) • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeWeekendsRule = exports.keepWeekdaysOnlyRule = exports.removeWeekdaysRule = exports.keepWeekdaysRule = void 0;
/**
* Base weekday rule that handles both keeping and removing weekdays
* @param weekdays - Array of weekday numbers (1-7, where 1 is Monday and 7 is Sunday)
* @param shouldKeep - If true, keep slots on specified weekdays. If false, remove them.
* @returns A rule that returns slots that should be removed
*/
const createWeekdayRule = (weekdays, shouldKeep) => (slots) => {
// If no weekdays specified, keep all slots
if (weekdays.length === 0) {
return [];
}
return slots.filter(slot => {
const weekday = slot.start.weekday;
// For keep: return slots NOT in weekdays (to be removed)
// For remove: return slots IN weekdays (to be removed)
return shouldKeep ? !weekdays.includes(weekday) : weekdays.includes(weekday);
});
};
/**
* Creates a rule that keeps only slots on specified weekdays
* @param weekdays - Array of weekday numbers to keep (1-7, where 1 is Monday and 7 is Sunday)
* If empty, all weekdays are kept
* @returns A rule that returns slots that should be removed (those not on specified weekdays)
*/
const keepWeekdaysRule = (weekdays = []) => createWeekdayRule(weekdays, true);
exports.keepWeekdaysRule = keepWeekdaysRule;
/**
* Creates a rule that removes slots on specified weekdays
* @param weekdays - Array of weekday numbers to remove (1-7, where 1 is Monday and 7 is Sunday)
* If empty, no weekdays are removed
* @returns A rule that returns slots that should be removed (those on specified weekdays)
*/
const removeWeekdaysRule = (weekdays = []) => createWeekdayRule(weekdays, false);
exports.removeWeekdaysRule = removeWeekdaysRule;
// Convenience functions for common use cases
const keepWeekdaysOnlyRule = (weekdays = []) => (0, exports.keepWeekdaysRule)(weekdays);
exports.keepWeekdaysOnlyRule = keepWeekdaysOnlyRule;
const removeWeekendsRule = () => (0, exports.removeWeekdaysRule)([6, 7]);
exports.removeWeekendsRule = removeWeekendsRule;