don't merge adjacent segments in combineOverlappingSegments

closes #2896
pull/2774/head
Mikael Finstad 1 month ago
parent 260529b5bc
commit 2605308ec8
No known key found for this signature in database
GPG Key ID: 25AB36E3E81CBC26

@ -1,7 +1,53 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`combineOverlappingSegments 1`] = `
exports[`combineOverlappingSegments > adjacent 1`] = `
[
{
"end": 2,
"start": 1,
},
{
"end": 3,
"start": 2,
},
{
"end": 4,
"start": 3,
},
]
`;
exports[`combineOverlappingSegments > non touching 1`] = `
[
{
"end": 2,
"start": 1,
},
{
"end": 4,
"start": 3,
},
]
`;
exports[`combineOverlappingSegments > overlapping and non overlapping 1`] = `
[
{
"end": 3,
"start": 1,
},
{
"end": 5,
"start": 4,
},
]
`;
exports[`combineOverlappingSegments > various 1`] = `
[
{
"start": 0,
},
{
"end": 87,
"start": 0,
@ -11,9 +57,16 @@ exports[`combineOverlappingSegments 1`] = `
"start": 229,
},
{
"end": 1487,
"end": 1101,
"start": 838,
},
{
"end": 1487,
"start": 1101,
},
{
"start": 1487,
},
{
"end": 1831,
"start": 1561,

@ -101,57 +101,6 @@ it('detects overlapping segments', () => {
]);
});
test('combineOverlappingSegments', () => {
const segments = [
{
start: 0,
},
{
start: 838,
end: 1101,
},
{
start: 1101,
end: 1244,
},
{
start: 1216,
end: 1487,
},
{
start: 1487,
},
{
start: 1625,
},
{
start: 503,
end: 669,
},
{
start: 392,
end: 716,
},
{
start: 229,
end: 784,
},
{
start: 0,
end: 87,
},
{
start: 1561,
end: 1831,
},
{
start: 2027,
},
];
expect(combineOverlappingSegments(segments)).toMatchSnapshot();
});
it('detects overlapping segments, undefined end', () => {
expect(partitionIntoOverlappingRanges([
{ start: 1, end: undefined },
@ -213,3 +162,77 @@ describe('invertSegments', () => {
], false, false)).toMatchSnapshot();
});
});
describe('combineOverlappingSegments', () => {
test('non touching', () => {
expect(combineOverlappingSegments([
{ start: 1, end: 2 },
{ start: 3, end: 4 },
])).toMatchSnapshot();
});
test('overlapping and non overlapping', () => {
expect(combineOverlappingSegments([
{ start: 1, end: 2 },
{ start: 1.5, end: 3 },
{ start: 4, end: 5 },
])).toMatchSnapshot();
});
test('adjacent', () => {
expect(combineOverlappingSegments([
{ start: 1, end: 2 },
{ start: 2, end: 3 },
{ start: 3, end: 4 },
])).toMatchSnapshot();
});
test('various', () => {
const segments = [
{
start: 0,
},
{
start: 838,
end: 1101,
},
{
start: 1101,
end: 1244,
},
{
start: 1216,
end: 1487,
},
{
start: 1487,
},
{
start: 1625,
},
{
start: 503,
end: 669,
},
{
start: 392,
end: 716,
},
{
start: 229,
end: 784,
},
{
start: 0,
end: 87,
},
{
start: 1561,
end: 1831,
},
{
start: 2027,
},
];
expect(combineOverlappingSegments(segments)).toMatchSnapshot();
});
});

@ -129,8 +129,8 @@ export function combineOverlappingSegments<T extends SegmentBase>(existingSegmen
const currentSegmentEndOrStart = currentSegment.end ?? currentSegment.start;
// Check if the current segment overlaps or is adjacent to the next segment
if (currentSegmentEndOrStart >= nextSegment.start) {
// Check if the current segment overlaps with the next segment
if (currentSegmentEndOrStart > nextSegment.start) {
currentSegment = {
...currentSegment,
end: Math.max(currentSegmentEndOrStart, nextSegment.end ?? nextSegment.start),

Loading…
Cancel
Save