-
-
Save mxriverlynn/77d2da52f0548f83f127 to your computer and use it in GitHub Desktop.
checking date overlaps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
start: new Date("January 2, 2003"), | |
end: new Date("March 12, 2006") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sortedRanges = dateRanges.sort((previous, current) => { | |
// get the start date from previous and current | |
var previousTime = previous.start.getTime(); | |
var currentTime = current.start.getTime(); | |
// if the previous is earlier than the current | |
if (previousTime < currentTime) { | |
return -1; | |
} | |
// if the previous time is the same as the current time | |
if (previousTime === currentTime) { | |
return 0; | |
} | |
// if the previous time is later than the current time | |
return 1; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start at index of 1 so there will | |
// always be a previous range to use | |
var length = dateRanges.length - 1; | |
for (var i = 1; i < length; i++){ | |
var previous = dateRanges[i-1]; | |
var current = dateRanges[i]; | |
// check for overlap, here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sorted.reduce((val, current, idx, arr) => { | |
if (idx === 0) { return; } | |
var previous = arr[idx-1]; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var previousEnd = previous.end.getTime(); | |
var currentStart = current.start.getTime(); | |
var overlap = (previousEnd >= currentStart) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = sorted.reduce((result, current, idx, arr) => { | |
// get the previous range | |
if (idx === 0) { return result; } | |
var previous = arr[idx-1]; | |
// check for any overlap | |
var previousEnd = previous.end.getTime(); | |
var currentStart = current.start.getTime(); | |
var overlap = (previousEnd >= currentStart); | |
// store the result | |
if (overlap) { | |
// yes, there is overlap | |
result.overlap = true; | |
// store the specific ranges that overlap | |
result.ranges.push({ | |
previous: previous, | |
current: current | |
}) | |
} | |
return result; | |
// seed the reduce | |
}, {overlap: false, ranges: []}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2.js
could be simplified to a single line of code: