Let's imagine the following:
You have a line that starts at 1 and ends at 10000.
You're given an array of ranges like [{ start: 5, end: 10 }, { start: 15, end: 25 }]
Given an array of ranges, find the inverse.
For the above example, the inverse would be [{ start: 1, end: 4 }, { start: 11, end: 14 }, { start: 26, end: 10000 }]
Notice how the inverse is basically every other range on our line.
Below is my current solution... Is there a more elegant solution that doesn't have to explicitly deal with the edge cases?
Note, in my code ranges are named regions.
const inverseRegions = (regions) => {
  // If no regions, the inverse is the entire line.
  if (regions.length === 0) { 
    return [{ start: 1, end: 10000 }] 
  }
  let result = []
  // If the first region doesn't start at the beginning of the line
  // we need to account for the region from the 1 to the start of
  // first region
  if (regions[0].start !== 1) {
    result.push({
      start: 1,
      end: regions[0].start - 1
    })
  }
  for (let i = 1; i < regions.length; i++) {
    const previousRegion = regions[i-1]
    const region = regions[i]
    result.push({
      start: previousRegion.end + 1,
      end: region.start - 1
    })
  }
  // If the last region doesn't end at the end of the line
  // we need to account for the region from the end of the last
  // region to 10000
  if (regions[regions.length - 1].end !== 10000) {
    result.push({
      start: regions[regions.length - 1].end + 1,
      end: 10000
    })
  }
  return result
}
Expected results:
inverseRegions([]) 
  => [ { start: 1, end: 10000 } ]
inverseRegions([{ start: 1, end: 10 }, { start: 15, end: 20 }]) 
  => [ { start: 11, end: 14 }, 
       { start: 21, end: 10000 } ]
inverseRegions([{ start: 5, end: 10 }, { start: 12, end: 60 }, { start: 66, end: 10000 }]) 
  => [ { start: 1, end: 4 },
       { start: 11, end: 11 },
       { start: 61, end: 65 } ]
inverseRegions([{ start: 8, end: 12 }, { start: 16, end: 20 }, { start: 29, end: 51 }]) 
  => [ { start: 1, end: 7 },
       { start: 13, end: 15 },
       { start: 21, end: 28 },
       { start: 52, end: 10000 } ]
 
    