Написал работающий вариант
const timeline = {start: 100, end: 300 }
const offsets = []
const items = [
{ start: 235, end: 270 },
{ start: 135, end: 170 },
]
.sort((a,b) => {
if(a.end > b.end) {
return 1
}
if(a.end < b.end) {
return -1
}
return 0
})
items.forEach((cur, index, arr) => {
// Start
if(index === 0) {
offsets.push({
start: timeline.start,
end: timeline.start + cur.start - timeline.start
})
}
// Middle
if(arr[index+1]) {
offsets.push({
start: cur.end,
end: cur.end + arr[index+1].start - cur.end
})
}
// End
if(index === arr.length - 1) {
offsets.push({
start: cur.end,
end: timeline.end
})
}
})
console.log(offsets)
/*
Result:
[ { start: 100, end: 135 },
{ start: 170, end: 235 },
{ start: 270, end: 300 } ]
*/