UNPKG

@kermank/slots

Version:

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

58 lines (57 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateSlot = exports.removeSlots = exports.removeExactSlots = exports.addSlots = void 0; const validation_1 = require("../utils/validation"); const slot_helpers_1 = require("../utils/slot-helpers"); /** * Creates an operator that adds slots to the collection */ const addSlots = (newSlots) => { return (currentSlots) => { const slotsToAdd = Array.isArray(newSlots) ? newSlots : [newSlots]; // Validate all new slots for (const slot of slotsToAdd) { if (!(0, validation_1.isSlot)(slot)) { return { data: currentSlots, error: 'Invalid slot format' }; } } // Merge all slots (existing and new) return { data: (0, slot_helpers_1.mergeOverlappingSlots)([...currentSlots, ...slotsToAdd]) }; }; }; exports.addSlots = addSlots; /** * Creates an operator that removes slots that match exactly (same start and end times) * For removing overlapping slots, use removeOverlappingSlots from slot-set-operations */ const removeExactSlots = (slotsToRemove) => { return (currentSlots) => { const toRemove = Array.isArray(slotsToRemove) ? slotsToRemove : [slotsToRemove]; return { data: currentSlots.filter(currentSlot => !toRemove.some(removeSlot => removeSlot.start.equals(currentSlot.start) && removeSlot.end.equals(currentSlot.end))) }; }; }; exports.removeExactSlots = removeExactSlots; // Deprecated - use removeExactSlots instead exports.removeSlots = exports.removeExactSlots; /** * Creates an operator that updates a slot in the collection */ const updateSlot = (oldSlot, newSlot) => { return (slots) => { if (!(0, validation_1.isSlot)(newSlot)) { return { data: slots, error: 'Invalid new slot format' }; } // First remove the old slot const withoutOld = (0, exports.removeSlots)(oldSlot)(slots); if (withoutOld.error) return withoutOld; // Then add the new slot, which will handle merging with any overlapping slots return (0, exports.addSlots)(newSlot)(withoutOld.data); }; }; exports.updateSlot = updateSlot;