@kermank/slots
Version:
A TypeScript library for handling time slots, scheduling, and timezone operations
56 lines (55 loc) • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBufferRule = void 0;
/**
* Creates a rule that adds buffer time before and after matching slots
* @param matcher - Function that returns true for slots that should have buffers
* @param bufferBefore - Minutes to block before the matching slot
* @param bufferAfter - Minutes to block after the matching slot
* @returns A rule that returns buffer slots that should be forbidden
*/
const createBufferRule = (matcher, bufferBefore = 0, bufferAfter = 0) => (slots) => {
// Find all matching slots
const matchingSlots = slots.filter(matcher);
// Create buffer slots for each matching slot
const bufferSlots = [];
matchingSlots.forEach(slot => {
if (bufferBefore > 0) {
bufferSlots.push({
start: slot.start.minus({ minutes: bufferBefore }),
end: slot.start,
metadata: {
...slot.metadata,
isBuffer: true,
bufferType: 'before',
originalSlot: slot
}
});
}
if (bufferAfter > 0) {
bufferSlots.push({
start: slot.end,
end: slot.end.plus({ minutes: bufferAfter }),
metadata: {
...slot.metadata,
isBuffer: true,
bufferType: 'after',
originalSlot: slot
}
});
}
});
return bufferSlots;
};
exports.createBufferRule = createBufferRule;
// Example usage:
// const flightBufferRule = createBufferRule(
// slot => slot.metadata?.type === 'flight',
// 420, // 7 hours before
// 480 // 8 hours after
// );
// const meetingBufferRule = createBufferRule(
// slot => slot.metadata?.type === 'meeting',
// 30, // 30 minutes before
// 30 // 30 minutes after
// );