Skip to content

Instantly share code, notes, and snippets.

@TheLurps
Created September 21, 2022 08:30
Show Gist options
  • Select an option

  • Save TheLurps/ddb61ad6072944dbe1ed010b208f49a4 to your computer and use it in GitHub Desktop.

Select an option

Save TheLurps/ddb61ad6072944dbe1ed010b208f49a4 to your computer and use it in GitHub Desktop.
const { Data } = require("dataclass");
class Timespan extends Data {
start;
end;
}
open = [
Timespan.create({
start: new Date("December 12, 2022 11:00:00"),
end: new Date("December 12, 2022 22:00:00"),
}),
];
reservations = [
Timespan.create({
start: new Date("December 12, 2022 12:00:00"),
end: new Date("December 12, 2022 12:30:00"),
}),
];
bufferTime = 5;
function book(open, bufferTime, reservations, newReservation) {
function addBuffer(date, bufferMinutes) {
return new Date(date.getTime() + bufferMinutes * 60000);
}
// check if in service hours
if (
open.some(
(slot) =>
slot.start <= newReservation.start && slot.end >= newReservation.end
)
) {
console.log("in service hours");
// check if there is a reservation that overlaps with the new reservation
if (
reservations.some(
(existingReservation) =>
(existingReservation.start <= newReservation.start &&
addBuffer(existingReservation.end, bufferTime) >=
newReservation.start) ||
(existingReservation.start <= newReservation.end &&
addBuffer(existingReservation.end, bufferTime) >=
newReservation.end)
)
) {
console.log("overlapping reservation");
return false;
} else {
console.log("no overlapping reservation");
reservations.push(newReservation);
return true;
}
} else {
console.log("not in service hours");
return false;
}
}
newReservation = Timespan.create({
start: new Date("December 12, 2022 12:15:00"),
end: new Date("December 12, 2022 12:35:00"),
});
console.log(
"Should be false: " + book(open, bufferTime, reservations, newReservation)
);
newReservation = Timespan.create({
start: new Date("December 12, 2022 11:00:00"),
end: new Date("December 12, 2022 11:30:00"),
});
console.log(
"Should be true: " + book(open, bufferTime, reservations, newReservation)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment