11cb0ef41Sopenharmony_ci// given a set of versions and a range, create a "simplified" range
21cb0ef41Sopenharmony_ci// that includes the same versions that the original range does
31cb0ef41Sopenharmony_ci// If the original range is shorter than the simplified one, return that.
41cb0ef41Sopenharmony_ciconst satisfies = require('../functions/satisfies.js')
51cb0ef41Sopenharmony_ciconst compare = require('../functions/compare.js')
61cb0ef41Sopenharmony_cimodule.exports = (versions, range, options) => {
71cb0ef41Sopenharmony_ci  const set = []
81cb0ef41Sopenharmony_ci  let first = null
91cb0ef41Sopenharmony_ci  let prev = null
101cb0ef41Sopenharmony_ci  const v = versions.sort((a, b) => compare(a, b, options))
111cb0ef41Sopenharmony_ci  for (const version of v) {
121cb0ef41Sopenharmony_ci    const included = satisfies(version, range, options)
131cb0ef41Sopenharmony_ci    if (included) {
141cb0ef41Sopenharmony_ci      prev = version
151cb0ef41Sopenharmony_ci      if (!first) {
161cb0ef41Sopenharmony_ci        first = version
171cb0ef41Sopenharmony_ci      }
181cb0ef41Sopenharmony_ci    } else {
191cb0ef41Sopenharmony_ci      if (prev) {
201cb0ef41Sopenharmony_ci        set.push([first, prev])
211cb0ef41Sopenharmony_ci      }
221cb0ef41Sopenharmony_ci      prev = null
231cb0ef41Sopenharmony_ci      first = null
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci  if (first) {
271cb0ef41Sopenharmony_ci    set.push([first, null])
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const ranges = []
311cb0ef41Sopenharmony_ci  for (const [min, max] of set) {
321cb0ef41Sopenharmony_ci    if (min === max) {
331cb0ef41Sopenharmony_ci      ranges.push(min)
341cb0ef41Sopenharmony_ci    } else if (!max && min === v[0]) {
351cb0ef41Sopenharmony_ci      ranges.push('*')
361cb0ef41Sopenharmony_ci    } else if (!max) {
371cb0ef41Sopenharmony_ci      ranges.push(`>=${min}`)
381cb0ef41Sopenharmony_ci    } else if (min === v[0]) {
391cb0ef41Sopenharmony_ci      ranges.push(`<=${max}`)
401cb0ef41Sopenharmony_ci    } else {
411cb0ef41Sopenharmony_ci      ranges.push(`${min} - ${max}`)
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci  }
441cb0ef41Sopenharmony_ci  const simplified = ranges.join(' || ')
451cb0ef41Sopenharmony_ci  const original = typeof range.raw === 'string' ? range.raw : String(range)
461cb0ef41Sopenharmony_ci  return simplified.length < original.length ? simplified : range
471cb0ef41Sopenharmony_ci}
48