(indexes: number[])
| 1 | export function groupContiguousIndexes(indexes: number[]) { |
| 2 | const contigousIndexes: { start: number; count: number }[] = []; |
| 3 | if (indexes.length === 0) { |
| 4 | return contigousIndexes; |
| 5 | } |
| 6 | indexes.sort((a, b) => a - b); |
| 7 | for (let i = 0; i < indexes.length; i++) { |
| 8 | const index = indexes[i]; |
| 9 | if (i === 0) { |
| 10 | contigousIndexes.push({ start: index, count: 1 }); |
| 11 | } else { |
| 12 | const lastContigousIndex = contigousIndexes[contigousIndexes.length - 1]; |
| 13 | if (lastContigousIndex.start + lastContigousIndex.count === index) { |
| 14 | lastContigousIndex.count++; |
| 15 | } else { |
| 16 | contigousIndexes.push({ start: index, count: 1 }); |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | return contigousIndexes; |
| 22 | } |
no outgoing calls
no test coverage detected