Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const hourToNumber = (hour) => hour.split(':').map(n => Number(n));
- const compareHour = (hourA, hourB) => {
- const [hA, mA] = hourToNumber(hourA);
- const [hB, mB] = hourToNumber(hourB);
- return hA - hB || mA - mB;
- };
- const isHourABeforeB = (hourA, hourB) => {
- return compareHour(hourA, hourB) <= 0;
- }
- const isTaskDoable = (dayAvaibility, dayCalendar, task) => {
- if (!dayAvaibility) {
- return false;
- }
- if (!isHourABeforeB(dayAvaibility.start, task.start) || !isHourABeforeB(task.end, dayAvaibility.end)) {
- return false;
- }
- const collidingTask = dayCalendar.find(plannedTaks => {
- const startAfter = isHourABeforeB(task.end, plannedTaks.start);
- const endBefore = isHourABeforeB(plannedTaks.end, task.start);
- return !startAfter && !endBefore;
- });
- return !collidingTask;
- }
- const findIndexRight = (array, predicate) => {
- for (let i = array.length - 1; i >= 0; i--) {
- if (predicate(array[i], i)) {
- return i;
- }
- }
- return -1;
- }
- const fullFillCalendar = (avaibility, tasks) => {
- const orderedTask = tasks.sort((a, b) => a.id - b.id);
- const calendarByDay = {};
- orderedTask.forEach(task => {
- if (!calendarByDay[task.day]) {
- calendarByDay[task.day] = [];
- }
- const dayCalendar = calendarByDay[task.day];
- if (isTaskDoable(avaibility.find(a => a.day === task.day), dayCalendar, task)) {
- const sortedIndex = findIndexRight(dayCalendar, plannedTask => {
- return !isHourABeforeB(plannedTask.start, task.start);
- });
- if (sortedIndex === - 1) {
- dayCalendar.push(task);
- } else {
- dayCalendar.splice(sortedIndex, 0, task);
- }
- };
- });
- return [
- { day: 0, booking: calendarByDay[0] || [] },
- { day: 1, booking: calendarByDay[1] || [] },
- { day: 2, booking: calendarByDay[2] || [] },
- { day: 3, booking: calendarByDay[3] || [] },
- { day: 4, booking: calendarByDay[4] || [] },
- { day: 5, booking: calendarByDay[5] || [] },
- { day: 6, booking: calendarByDay[6] || [] },
- ]
- }
- const bookings = [
- { "id": 0, "day": 6, "start": "14:22", "end": "15:05" },
- { "id": 1, "day": 4, "start": "12:43", "end": "13:34" },
- { "id": 2, "day": 1, "start": "10:46", "end": "11:31" },
- { "id": 3, "day": 2, "start": "14:36", "end": "17:48" },
- { "id": 4, "day": 1, "start": "09:55", "end": "13:10" },
- { "id": 5, "day": 5, "start": "11:20", "end": "14:37" },
- { "id": 6, "day": 5, "start": "12:58", "end": "13:50" },
- { "id": 7, "day": 0, "start": "10:01", "end": "11:10" },
- { "id": 8, "day": 4, "start": "09:33", "end": "10:25" },
- { "id": 9, "day": 4, "start": "11:52", "end": "13:51" },
- { "id": 10, "day": 3, "start": "09:58", "end": "17:43" },
- { "id": 11, "day": 2, "start": "10:09", "end": "15:52" },
- { "id": 12, "day": 1, "start": "10:55", "end": "14:32" },
- { "id": 13, "day": 1, "start": "15:55", "end": "16:24" },
- { "id": 14, "day": 0, "start": "15:07", "end": "17:21" },
- { "id": 15, "day": 3, "start": "12:20", "end": "14:54" },
- { "id": 16, "day": 4, "start": "12:21", "end": "16:20" },
- { "id": 17, "day": 3, "start": "14:11", "end": "17:49" },
- { "id": 18, "day": 2, "start": "09:33", "end": "14:03" },
- { "id": 19, "day": 1, "start": "13:38", "end": "15:42" }
- ]
- const doctorAvailability = [
- {
- day: 0,
- start: "9:00",
- end: "18:00"
- },
- {
- day: 1,
- start: "9:00",
- end: "18:00"
- },
- {
- day: 2,
- start: "9:00",
- end: "18:00"
- },
- {
- day: 3,
- start: "9:00",
- end: "18:00"
- },
- {
- day: 4,
- start: "9:00",
- end: "18:00"
- },
- {
- day: 5,
- start: "9:00",
- end: "18:00"
- },
- {
- day: 6,
- start: "9:00",
- end: "18:00"
- }
- ];
- console.log(JSON.stringify(fullFillCalendar(doctorAvailability, bookings), null, ' '));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement