mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-09 17:51:46 +00:00
31 lines
696 B
JavaScript
31 lines
696 B
JavaScript
// Segments are an array [ [start, end], … ]
|
|
module.exports.sortSegments = (segments) => {
|
|
segments.sort((segment1, segment2) =>
|
|
segment1[0] === segment2[0]
|
|
? segment1[1] - segment2[1]
|
|
: segment1[0] - segment2[0],
|
|
);
|
|
|
|
const compiledSegments = [];
|
|
let currentSegment;
|
|
|
|
for (const segment of segments) {
|
|
if (!currentSegment) {
|
|
currentSegment = segment;
|
|
continue;
|
|
}
|
|
|
|
if (currentSegment[1] < segment[0]) {
|
|
compiledSegments.push(currentSegment);
|
|
currentSegment = segment;
|
|
continue;
|
|
}
|
|
|
|
currentSegment[1] = Math.max(currentSegment[1], segment[1]);
|
|
}
|
|
|
|
compiledSegments.push(currentSegment);
|
|
|
|
return compiledSegments;
|
|
};
|