1// given a set of versions and a range, create a "simplified" range
2// that includes the same versions that the original range does
3// If the original range is shorter than the simplified one, return that.
4const satisfies = require('../functions/satisfies.js')
5const compare = require('../functions/compare.js')
6module.exports = (versions, range, options) => {
7  const set = []
8  let first = null
9  let prev = null
10  const v = versions.sort((a, b) => compare(a, b, options))
11  for (const version of v) {
12    const included = satisfies(version, range, options)
13    if (included) {
14      prev = version
15      if (!first) {
16        first = version
17      }
18    } else {
19      if (prev) {
20        set.push([first, prev])
21      }
22      prev = null
23      first = null
24    }
25  }
26  if (first) {
27    set.push([first, null])
28  }
29
30  const ranges = []
31  for (const [min, max] of set) {
32    if (min === max) {
33      ranges.push(min)
34    } else if (!max && min === v[0]) {
35      ranges.push('*')
36    } else if (!max) {
37      ranges.push(`>=${min}`)
38    } else if (min === v[0]) {
39      ranges.push(`<=${max}`)
40    } else {
41      ranges.push(`${min} - ${max}`)
42    }
43  }
44  const simplified = ranges.join(' || ')
45  const original = typeof range.raw === 'string' ? range.raw : String(range)
46  return simplified.length < original.length ? simplified : range
47}
48