Identify Booked time slots from generated time slo

2020-02-02 03:12发布

问题:

I have slots generated based on the start time and end time. Now I want to identify slots form generated slots that are already booked. Additionally, I also want to identify slots based on booked slots and maintenance time. maintenance time is additional time after the booking is done. when slots new slots are generated after someone's booking then next slot will be generated based on that time.

Example:

Input:

Actual time slots:

[ '10:00-10:30',
  '10:30-11:00',
  '11:00-11:30',
  '11:30-12:00',
  '12:00-12:30',
  '12:30-13:00',
  '13:00-13:30',
  '13:30-14:00',
  '14:00-14:30',
  '14:30-15:00',
  '15:00-15:30',
  '15:30-16:00'
]

if Booked Time Slots is ["11:00-13:00","14:15-15:15"], Maintenance time is 15 mins then the output should be:

[{
        "slot": "10:00-10:30",
        "isBooked": false
    },
    {
        "slot": "10:30-11:00",
        "isBooked": false
    },
    {
        "slot": "11:00-11:30",
        "isBooked": true
    },
    {
        "slot": "11:30-12:00",
        "isBooked": true
    },
    {
        "slot": "12:00-12:30",
        "isBooked": true
    },
    {
        "slot": "12:30-13:00",
        "isBooked": true
    },
    {
        "slot": "13:15-13:45",
        "isBooked": false
    },
    {
        "slot": "13:45-14:15",
        "isBooked": true
    },
    {
        "slot": "14:15-14:45",
        "isBooked": true
    },
    {
        "slot": "14:45-15:15",
        "isBooked": true
    },
    {
        "slot": "15:30-16:00",
        "isBooked": false
    }]

let's make it simple

//Booking start time 10:00, booking end time = 16:00

[ '10:00-11:00',
  '11:00-12:00',
  '12:00-13:00',
  '13:00-14:00',
  '14:00-15:00',
  '15:00-16:00'
]

if Booked Time Slots is ["11:00-13:00"], Maintenance time is 15 mins then the output should be:

[{
    "slot": "10:00-11:00",
    "isBooked": false
}
{
    "slot": "11:00-12:00",
    "isBooked": true
},
{
    "slot": "12:00-13:00",
    "isBooked": true
},{
    "slot": "13:15-14:15",
    "isBooked": false
},{
    "slot": "14:15-15:15",
    "isBooked": false
}]

Now here I cant generate slot from "15:15-16:15" because my last slot end time is of 16:00

I have tried this:

const ts = [
  "10:00-10:30",
  "10:30-11:00",
  "11:00-11:30",
  "11:30-12:00",
  "12:00-12:30",
  "12:30-13:00",
  "13:00-13:30",
  "13:30-14:00",
  "14:00-14:30",
  "14:30-15:00",
  "15:00-15:30",
  "15:30-16:00"
];

const avail = (ts, booked) =>
  ts.map(item => {
      const[start, end] = item.split('-');
      const isBooked = booked
          .map(item => item.split('-'))
          .some(([bookedStart, bookedEnd]) =>
              (start >= bookedStart && start < bookedEnd) ||
              (end > bookedStart && end <= bookedEnd) ||
              (bookedStart >= start && bookedStart < end));
      return {slot: `${start}-${end}`, isBooked};
  })

console.log(avail(ts, ["11:00-13:00", "14:00-15:30"]));

but it's not working for all the cases,