title: Compute the union time ranges from two arrays of time ranges (with moment-range)
body: <p>I have two arrays of <a href="https://github.com/gf3/moment-range" rel="nofollow noreferrer">moment-ranges</a>, that represent periods when two individuals are busy.</p>
  
  <p>What's the best way to compute an array which contains the union time ranges that they are busy? If two busy periods overlap for the individuals, I need the time range that represents the whole period over which they are busy.</p>
  
  <p>Here's an example:</p>
  
  <pre><code>const firstBusyPeriods = [ 
    { start: "2017-04-05T10:00:00Z", end: "2017-04-05T12:00:00Z" },
    { start: "2017-04-05T14:00:00Z", end: "2017-04-05T15:00:00Z" }
  ]
  
  const secondBusyPeriods = [
    { start: "2017-04-05T08:00:00Z", end: "2017-04-05T11:00:00Z" },
    { start: "2017-04-05T16:00:00Z", end: "2017-04-05T17:00:00Z" }
  ]
  </code></pre>
  
  <p>The resulting array should look like this:</p>
  
  <pre><code>const result = [
    { start: "2017-04-05T08:00:00Z", end: "2017-04-05T12:00:00Z" },
    { start: "2017-04-05T14:00:00Z", end: "2017-04-05T15:00:00Z" },
    { start: "2017-04-05T16:00:00Z", end: "2017-04-05T17:00:00Z" }
  ]
  </code></pre>
  
  <p>The result contains a union of the overlapping busy period, and then includes the two periods that don't overlap between the individuals.</p>
  
  <p>Would it be best to concatenate the two arrays, sort the resulting array, and apply a reduce function?</p>
  
  <p>Or would it be best to recursively iterate through one of the two arrays and produce a stack with the union and non-intersecting periods as the result?</p>