https://github.com/jojoee/range-overlap
Are 2 ranges overlap ?
https://github.com/jojoee/range-overlap
date intersection interval overlap overlapping range utility
Last synced: 11 months ago
JSON representation
Are 2 ranges overlap ?
- Host: GitHub
- URL: https://github.com/jojoee/range-overlap
- Owner: jojoee
- Created: 2021-03-03T10:13:16.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-11T04:02:23.000Z (over 2 years ago)
- Last Synced: 2025-04-11T01:20:22.979Z (11 months ago)
- Topics: date, intersection, interval, overlap, overlapping, range, utility
- Language: TypeScript
- Homepage: https://jojoee.github.io/range-overlap/example/
- Size: 155 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# range-overlap
[](https://www.npmjs.com/package/range-overlap)
[](http://opensource.org/licenses/MIT)



[](https://codecov.io/github/jojoee/range-overlap)
[](https://github.com/semantic-release/semantic-release)
[](https://greenkeeper.io/)
[](https://dashboard.stryker-mutator.io/reports/github.com/jojoee/range-overlap/main)
Are 2 ranges overlap ?, [demo page](https://jojoee.github.io/range-overlap/example/).
[](https://jojoee.github.io/range-overlap/example/)
## Installation
Install with `npm install range-overlap` then
```javascript
// CommonJS
const { isRangeOverlap } = require('range-overlap')
// ES6
import { isRangeOverlap } from "range-overlap"
```
## Example usage
```javascript
// integer and floating numbers as a param
isRangeOverlap(1, 10, 2, 12) // true
isRangeOverlap(1, 10, 2, 8) // true
isRangeOverlap(100, 200, 201, 300) // false
// Date as a param
isRangeOverlap( // true
new Date(1615452500000),
new Date(1615452800000),
new Date(1615452700000),
new Date(1615452900000),
)
// array of numbers or Date(s) as a param
isRangeOverlap([1, 10], [2, 12]) // true
isRangeOverlap( // true
[new Date(1615452500000), new Date(1615452800000)],
[new Date(1615452700000), new Date(1615452900000)]
)
// custom type as a param
isRangeOverlap( // true
{ start: 1, end: 10 },
{ start: 2, end: 12 }
)
// support is-exclusive param
isRangeOverlap(1, 10, 10, 12, true) // false
isRangeOverlap( // false
new Date(1615452500000),
new Date(1615452600000),
new Date(1615452600000),
new Date(1615452800000),
true
)
isRangeOverlap([1, 10], [10, 12], true) // false
isRangeOverlap( // false
[new Date(1615452500000), new Date(1615452600000)],
[new Date(1615452600000), new Date(1615452800000)],
true
)
isRangeOverlap({ start: 1, end: 10 }, { start: 10, end: 12 }, true) // false
```
## Algorithm
The detailed logic is described [here](https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap), but in summary is explained below.
```
2 cases that will not overlapping
case1) |----range1----| |----range2----|
x1 x2 y1 y2
case2) |----range2----| |----range1----|
y1 y2 x1 x2
so isNotOverlap
= case1) or case2)
= x2 < y1 || y2 < x1
due to isOverlap
= ~isNotOverlap
= ~(x2 < y1 || y2 < x1)
= x2 >= y1 && y2 >= x1
= x1 <= y2 && y1 <= x2
```
## CMD
```bash
npm run format && npm run lint && npm run coverage.check && npm run build
npx stryker run
npm run version
npm publish --dry-run
npm publish
```