I need to compare schedules for overlap, anywhere from 2 to infinite number of schedules.
For example, an array with 3 schedules would look like this:
var dateRanges = [
{
DaysOfWeek: ['Sun', 'Mon'],
StartTime: "01:00",
StopTime: "17:00",
AllDay: false
},
{
DaysOfWeek: ['Tues', 'Wed'],
StartTime: "12:00",
StopTime: "21:59",
AllDay: true
},
{
DaysOfWeek: ['Thur', 'Sun'],
StartTime: "12:00",
StopTime: "21:59",
AllDay: true
}
]
I'm struggling to figure out how to compare all arrays with each other. So far I have this
checkScheduleForOverlap: function (dateRanges) {
var result = dateRanges.reduce((result, current, i, arr) => {
// console.log(previous, current);
// get the previous range
if (i === 0) { return result; }
var previous = arr[i - 1];
// Schedule1
var startTime1 = new Date('1970-01-01T' + previous.StartTime + 'Z');
var stopTime1 = new Date('1970-01-01T' + previous.StopTime + 'Z');
// Schedule2
var startTime2 = new Date('1970-01-01T' + current.StartTime + 'Z');
var stopTime2 = new Date('1970-01-01T' + current.StopTime + 'Z');
previous.DaysOfWeek.forEach(function (prevDay) {
console.log(prevDay);
current.DaysOfWeek.forEach(function (currDay) {
console.log(currDay);
if (prevDay === currDay) {
var overlap = (startTime1 <= stopTime2) && (stopTime1 >= startTime2);
// store the result
if (overlap) {
// yes, there is overlap
result.overlap = true;
// store the specific ranges that overlap
result.days.push(currDay);
}
}
});
});
return result;
// seed the reduce
}, { overlap: false, days: [] });
// return the final results
console.log(result);
return result;
}
But it only compares the second array with the first, and the third with the second. It also needs to compare the third with the first. (and if there were 4 schedules, each would need to compare to the other.)
Am I on the right track here, and what can be done to get each DaysOfWeek schedule to compare the StartTime and StopTime with the values in the other schedule?
I created a fake date object using a static day, and I'm only comparing the time values.
I'm open to doing it a completely different way if this is not an efficient way to do it.
infinite number of schedulesI'm pretty sure you mean finite.a user can add an infinite number of schedulesNo they can't.Compare each element in an array against every other element. You might be able to usemoment-range: github.com/rotaready/moment-range/blob/master/README.mdCompare each element in an array against every other elementYes that's correct, I didn't know I was invoking philosophical dilemmas. I updated the title :-D