Extract filterOverlappingMatches function from ratingViewPlugin.ts

This commit is contained in:
Filip Noetzel 2025-06-10 11:52:24 +02:00
parent 7c9ac9d5cd
commit 785efc88ed

View file

@ -0,0 +1,22 @@
import { RatingMatch } from '../RatingMatch';
/**
* Filter overlapping matches, keeping the first one in each overlapping group
*/
export function filterOverlappingMatches(matches: RatingMatch[]): RatingMatch[] {
// Sort matches by start position (required by RangeSetBuilder)
const sortedMatches = [...matches].sort((a, b) => a.start - b.start);
// Remove overlapping matches (keep the first one)
const filteredMatches: RatingMatch[] = [];
let lastEnd = -1;
for (const match of sortedMatches) {
if (match.start >= lastEnd) {
filteredMatches.push(match);
lastEnd = match.end;
}
}
return filteredMatches;
}