range.ts 606 B

123456789101112131415161718192021222324
  1. export class Range {
  2. from: number
  3. to: number
  4. constructor(from: number, to: number) {
  5. this.from = from
  6. this.to = to
  7. }
  8. contains(pos: number, allowBoundaries = true) {
  9. return allowBoundaries
  10. ? pos >= this.from && pos <= this.to
  11. : pos > this.from && pos < this.to
  12. }
  13. // Ranges that touch but don't overlap are not considered to intersect
  14. intersects(range: Range) {
  15. return this.contains(range.from, false) || this.contains(range.to, false)
  16. }
  17. touchesOrIntersects(range: Range) {
  18. return this.contains(range.from, true) || this.contains(range.to, true)
  19. }
  20. }