Last active
March 6, 2019 19:34
-
-
Save Tymek/10282cfecebdc16c962baf98e0314a57 to your computer and use it in GitHub Desktop.
Time/date (timestamps) segments overlap in Ramda
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
/** | |
* @see: https://gist.github.com/Tymek/10282cfecebdc16c962baf98e0314a57 | |
*/ | |
const { | |
apply, | |
compose, | |
lt, | |
max, | |
min, | |
transpose, | |
unapply, | |
useWith, | |
} = require('ramda') | |
// props: ([start1, end1], [start2, end2]) | |
// @see https://stackoverflow.com/q/325933/1729641 | |
const isOverlaping = compose( | |
apply( | |
useWith( | |
lt, | |
[ | |
apply(max), | |
apply(min), | |
], | |
) | |
), | |
unapply(transpose), | |
) | |
console.assert(isOverlaping([1, 2], [3, 4]) === false, 'divergent 1') | |
console.assert(isOverlaping([3, 4], [1, 2]) === false, 'divergent 2') | |
console.assert(isOverlaping([1, 2], [2, 3]) === false, 'adjacent 1') | |
console.assert(isOverlaping([2, 3], [1, 2]) === false, 'adjacent 2') | |
console.assert(isOverlaping([1, 2], [1, 2]) === true, 'equivalent') | |
console.assert(isOverlaping([1, 3], [2, 4]) === true, 'intersecting 1') | |
console.assert(isOverlaping([2, 4], [1, 3]) === true, 'intersecting 2') | |
console.assert(isOverlaping([1, 4], [2, 3]) === true, 'contained 1') | |
console.assert(isOverlaping([2, 3], [1, 4]) === true, 'contained 2') | |
console.assert(isOverlaping([3, 4], [1, 4]) === true, 'contained 3') | |
console.assert(isOverlaping([1, 4], [1, 2]) === true, 'contained 4') | |
module.exports = isOverlaping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment