Skip to content

Overlapping ranges

datesAll subjects

Two bookings overlap if they share any instant. The ranges are half-open — a range that ends exactly when the next begins does not collide with it, which is what makes back-to-back bookings possible at all.

The module under test

1
export function overlaps(a, b) {
2
if (!a || !b) {
3
return false;
4
}
5
return a.start < b.end && b.start < a.end;
6
}
7
8
export function merge(ranges) {
9
const sorted = ranges.slice().sort((x, y) => x.start - y.start);
10
const merged = [];
11
for (const range of sorted) {
12
const last = merged[merged.length - 1];
13
if (last && range.start <= last.end) {
14
last.end = Math.max(last.end, range.end);
15
} else {
16
merged.push({ start: range.start, end: range.end });
17
}
18
}
19
return merged;
20
}
21

Your tests

Mutants

Mutation score
Survived
0
Killed
0 / 0
Line coverage
Tests
0
Assertion width
Cycle
    survivedkilledtimed outequivalentnot run