11cb0ef41Sopenharmony_ci// hoisted class for cyclic dependency
21cb0ef41Sopenharmony_ciclass Range {
31cb0ef41Sopenharmony_ci  constructor (range, options) {
41cb0ef41Sopenharmony_ci    options = parseOptions(options)
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci    if (range instanceof Range) {
71cb0ef41Sopenharmony_ci      if (
81cb0ef41Sopenharmony_ci        range.loose === !!options.loose &&
91cb0ef41Sopenharmony_ci        range.includePrerelease === !!options.includePrerelease
101cb0ef41Sopenharmony_ci      ) {
111cb0ef41Sopenharmony_ci        return range
121cb0ef41Sopenharmony_ci      } else {
131cb0ef41Sopenharmony_ci        return new Range(range.raw, options)
141cb0ef41Sopenharmony_ci      }
151cb0ef41Sopenharmony_ci    }
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci    if (range instanceof Comparator) {
181cb0ef41Sopenharmony_ci      // just put it in the set and return
191cb0ef41Sopenharmony_ci      this.raw = range.value
201cb0ef41Sopenharmony_ci      this.set = [[range]]
211cb0ef41Sopenharmony_ci      this.format()
221cb0ef41Sopenharmony_ci      return this
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci    this.options = options
261cb0ef41Sopenharmony_ci    this.loose = !!options.loose
271cb0ef41Sopenharmony_ci    this.includePrerelease = !!options.includePrerelease
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    // First reduce all whitespace as much as possible so we do not have to rely
301cb0ef41Sopenharmony_ci    // on potentially slow regexes like \s*. This is then stored and used for
311cb0ef41Sopenharmony_ci    // future error messages as well.
321cb0ef41Sopenharmony_ci    this.raw = range
331cb0ef41Sopenharmony_ci      .trim()
341cb0ef41Sopenharmony_ci      .split(/\s+/)
351cb0ef41Sopenharmony_ci      .join(' ')
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    // First, split on ||
381cb0ef41Sopenharmony_ci    this.set = this.raw
391cb0ef41Sopenharmony_ci      .split('||')
401cb0ef41Sopenharmony_ci      // map the range to a 2d array of comparators
411cb0ef41Sopenharmony_ci      .map(r => this.parseRange(r.trim()))
421cb0ef41Sopenharmony_ci      // throw out any comparator lists that are empty
431cb0ef41Sopenharmony_ci      // this generally means that it was not a valid range, which is allowed
441cb0ef41Sopenharmony_ci      // in loose mode, but will still throw if the WHOLE range is invalid.
451cb0ef41Sopenharmony_ci      .filter(c => c.length)
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    if (!this.set.length) {
481cb0ef41Sopenharmony_ci      throw new TypeError(`Invalid SemVer Range: ${this.raw}`)
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    // if we have any that are not the null set, throw out null sets.
521cb0ef41Sopenharmony_ci    if (this.set.length > 1) {
531cb0ef41Sopenharmony_ci      // keep the first one, in case they're all null sets
541cb0ef41Sopenharmony_ci      const first = this.set[0]
551cb0ef41Sopenharmony_ci      this.set = this.set.filter(c => !isNullSet(c[0]))
561cb0ef41Sopenharmony_ci      if (this.set.length === 0) {
571cb0ef41Sopenharmony_ci        this.set = [first]
581cb0ef41Sopenharmony_ci      } else if (this.set.length > 1) {
591cb0ef41Sopenharmony_ci        // if we have any that are *, then the range is just *
601cb0ef41Sopenharmony_ci        for (const c of this.set) {
611cb0ef41Sopenharmony_ci          if (c.length === 1 && isAny(c[0])) {
621cb0ef41Sopenharmony_ci            this.set = [c]
631cb0ef41Sopenharmony_ci            break
641cb0ef41Sopenharmony_ci          }
651cb0ef41Sopenharmony_ci        }
661cb0ef41Sopenharmony_ci      }
671cb0ef41Sopenharmony_ci    }
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    this.format()
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  format () {
731cb0ef41Sopenharmony_ci    this.range = this.set
741cb0ef41Sopenharmony_ci      .map((comps) => comps.join(' ').trim())
751cb0ef41Sopenharmony_ci      .join('||')
761cb0ef41Sopenharmony_ci      .trim()
771cb0ef41Sopenharmony_ci    return this.range
781cb0ef41Sopenharmony_ci  }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  toString () {
811cb0ef41Sopenharmony_ci    return this.range
821cb0ef41Sopenharmony_ci  }
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  parseRange (range) {
851cb0ef41Sopenharmony_ci    // memoize range parsing for performance.
861cb0ef41Sopenharmony_ci    // this is a very hot path, and fully deterministic.
871cb0ef41Sopenharmony_ci    const memoOpts =
881cb0ef41Sopenharmony_ci      (this.options.includePrerelease && FLAG_INCLUDE_PRERELEASE) |
891cb0ef41Sopenharmony_ci      (this.options.loose && FLAG_LOOSE)
901cb0ef41Sopenharmony_ci    const memoKey = memoOpts + ':' + range
911cb0ef41Sopenharmony_ci    const cached = cache.get(memoKey)
921cb0ef41Sopenharmony_ci    if (cached) {
931cb0ef41Sopenharmony_ci      return cached
941cb0ef41Sopenharmony_ci    }
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    const loose = this.options.loose
971cb0ef41Sopenharmony_ci    // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
981cb0ef41Sopenharmony_ci    const hr = loose ? re[t.HYPHENRANGELOOSE] : re[t.HYPHENRANGE]
991cb0ef41Sopenharmony_ci    range = range.replace(hr, hyphenReplace(this.options.includePrerelease))
1001cb0ef41Sopenharmony_ci    debug('hyphen replace', range)
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci    // `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
1031cb0ef41Sopenharmony_ci    range = range.replace(re[t.COMPARATORTRIM], comparatorTrimReplace)
1041cb0ef41Sopenharmony_ci    debug('comparator trim', range)
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci    // `~ 1.2.3` => `~1.2.3`
1071cb0ef41Sopenharmony_ci    range = range.replace(re[t.TILDETRIM], tildeTrimReplace)
1081cb0ef41Sopenharmony_ci    debug('tilde trim', range)
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci    // `^ 1.2.3` => `^1.2.3`
1111cb0ef41Sopenharmony_ci    range = range.replace(re[t.CARETTRIM], caretTrimReplace)
1121cb0ef41Sopenharmony_ci    debug('caret trim', range)
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci    // At this point, the range is completely trimmed and
1151cb0ef41Sopenharmony_ci    // ready to be split into comparators.
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci    let rangeList = range
1181cb0ef41Sopenharmony_ci      .split(' ')
1191cb0ef41Sopenharmony_ci      .map(comp => parseComparator(comp, this.options))
1201cb0ef41Sopenharmony_ci      .join(' ')
1211cb0ef41Sopenharmony_ci      .split(/\s+/)
1221cb0ef41Sopenharmony_ci      // >=0.0.0 is equivalent to *
1231cb0ef41Sopenharmony_ci      .map(comp => replaceGTE0(comp, this.options))
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci    if (loose) {
1261cb0ef41Sopenharmony_ci      // in loose mode, throw out any that are not valid comparators
1271cb0ef41Sopenharmony_ci      rangeList = rangeList.filter(comp => {
1281cb0ef41Sopenharmony_ci        debug('loose invalid filter', comp, this.options)
1291cb0ef41Sopenharmony_ci        return !!comp.match(re[t.COMPARATORLOOSE])
1301cb0ef41Sopenharmony_ci      })
1311cb0ef41Sopenharmony_ci    }
1321cb0ef41Sopenharmony_ci    debug('range list', rangeList)
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci    // if any comparators are the null set, then replace with JUST null set
1351cb0ef41Sopenharmony_ci    // if more than one comparator, remove any * comparators
1361cb0ef41Sopenharmony_ci    // also, don't include the same comparator more than once
1371cb0ef41Sopenharmony_ci    const rangeMap = new Map()
1381cb0ef41Sopenharmony_ci    const comparators = rangeList.map(comp => new Comparator(comp, this.options))
1391cb0ef41Sopenharmony_ci    for (const comp of comparators) {
1401cb0ef41Sopenharmony_ci      if (isNullSet(comp)) {
1411cb0ef41Sopenharmony_ci        return [comp]
1421cb0ef41Sopenharmony_ci      }
1431cb0ef41Sopenharmony_ci      rangeMap.set(comp.value, comp)
1441cb0ef41Sopenharmony_ci    }
1451cb0ef41Sopenharmony_ci    if (rangeMap.size > 1 && rangeMap.has('')) {
1461cb0ef41Sopenharmony_ci      rangeMap.delete('')
1471cb0ef41Sopenharmony_ci    }
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci    const result = [...rangeMap.values()]
1501cb0ef41Sopenharmony_ci    cache.set(memoKey, result)
1511cb0ef41Sopenharmony_ci    return result
1521cb0ef41Sopenharmony_ci  }
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci  intersects (range, options) {
1551cb0ef41Sopenharmony_ci    if (!(range instanceof Range)) {
1561cb0ef41Sopenharmony_ci      throw new TypeError('a Range is required')
1571cb0ef41Sopenharmony_ci    }
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci    return this.set.some((thisComparators) => {
1601cb0ef41Sopenharmony_ci      return (
1611cb0ef41Sopenharmony_ci        isSatisfiable(thisComparators, options) &&
1621cb0ef41Sopenharmony_ci        range.set.some((rangeComparators) => {
1631cb0ef41Sopenharmony_ci          return (
1641cb0ef41Sopenharmony_ci            isSatisfiable(rangeComparators, options) &&
1651cb0ef41Sopenharmony_ci            thisComparators.every((thisComparator) => {
1661cb0ef41Sopenharmony_ci              return rangeComparators.every((rangeComparator) => {
1671cb0ef41Sopenharmony_ci                return thisComparator.intersects(rangeComparator, options)
1681cb0ef41Sopenharmony_ci              })
1691cb0ef41Sopenharmony_ci            })
1701cb0ef41Sopenharmony_ci          )
1711cb0ef41Sopenharmony_ci        })
1721cb0ef41Sopenharmony_ci      )
1731cb0ef41Sopenharmony_ci    })
1741cb0ef41Sopenharmony_ci  }
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci  // if ANY of the sets match ALL of its comparators, then pass
1771cb0ef41Sopenharmony_ci  test (version) {
1781cb0ef41Sopenharmony_ci    if (!version) {
1791cb0ef41Sopenharmony_ci      return false
1801cb0ef41Sopenharmony_ci    }
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci    if (typeof version === 'string') {
1831cb0ef41Sopenharmony_ci      try {
1841cb0ef41Sopenharmony_ci        version = new SemVer(version, this.options)
1851cb0ef41Sopenharmony_ci      } catch (er) {
1861cb0ef41Sopenharmony_ci        return false
1871cb0ef41Sopenharmony_ci      }
1881cb0ef41Sopenharmony_ci    }
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci    for (let i = 0; i < this.set.length; i++) {
1911cb0ef41Sopenharmony_ci      if (testSet(this.set[i], version, this.options)) {
1921cb0ef41Sopenharmony_ci        return true
1931cb0ef41Sopenharmony_ci      }
1941cb0ef41Sopenharmony_ci    }
1951cb0ef41Sopenharmony_ci    return false
1961cb0ef41Sopenharmony_ci  }
1971cb0ef41Sopenharmony_ci}
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_cimodule.exports = Range
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ciconst LRU = require('lru-cache')
2021cb0ef41Sopenharmony_ciconst cache = new LRU({ max: 1000 })
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ciconst parseOptions = require('../internal/parse-options')
2051cb0ef41Sopenharmony_ciconst Comparator = require('./comparator')
2061cb0ef41Sopenharmony_ciconst debug = require('../internal/debug')
2071cb0ef41Sopenharmony_ciconst SemVer = require('./semver')
2081cb0ef41Sopenharmony_ciconst {
2091cb0ef41Sopenharmony_ci  safeRe: re,
2101cb0ef41Sopenharmony_ci  t,
2111cb0ef41Sopenharmony_ci  comparatorTrimReplace,
2121cb0ef41Sopenharmony_ci  tildeTrimReplace,
2131cb0ef41Sopenharmony_ci  caretTrimReplace,
2141cb0ef41Sopenharmony_ci} = require('../internal/re')
2151cb0ef41Sopenharmony_ciconst { FLAG_INCLUDE_PRERELEASE, FLAG_LOOSE } = require('../internal/constants')
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ciconst isNullSet = c => c.value === '<0.0.0-0'
2181cb0ef41Sopenharmony_ciconst isAny = c => c.value === ''
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci// take a set of comparators and determine whether there
2211cb0ef41Sopenharmony_ci// exists a version which can satisfy it
2221cb0ef41Sopenharmony_ciconst isSatisfiable = (comparators, options) => {
2231cb0ef41Sopenharmony_ci  let result = true
2241cb0ef41Sopenharmony_ci  const remainingComparators = comparators.slice()
2251cb0ef41Sopenharmony_ci  let testComparator = remainingComparators.pop()
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci  while (result && remainingComparators.length) {
2281cb0ef41Sopenharmony_ci    result = remainingComparators.every((otherComparator) => {
2291cb0ef41Sopenharmony_ci      return testComparator.intersects(otherComparator, options)
2301cb0ef41Sopenharmony_ci    })
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci    testComparator = remainingComparators.pop()
2331cb0ef41Sopenharmony_ci  }
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci  return result
2361cb0ef41Sopenharmony_ci}
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci// comprised of xranges, tildes, stars, and gtlt's at this point.
2391cb0ef41Sopenharmony_ci// already replaced the hyphen ranges
2401cb0ef41Sopenharmony_ci// turn into a set of JUST comparators.
2411cb0ef41Sopenharmony_ciconst parseComparator = (comp, options) => {
2421cb0ef41Sopenharmony_ci  debug('comp', comp, options)
2431cb0ef41Sopenharmony_ci  comp = replaceCarets(comp, options)
2441cb0ef41Sopenharmony_ci  debug('caret', comp)
2451cb0ef41Sopenharmony_ci  comp = replaceTildes(comp, options)
2461cb0ef41Sopenharmony_ci  debug('tildes', comp)
2471cb0ef41Sopenharmony_ci  comp = replaceXRanges(comp, options)
2481cb0ef41Sopenharmony_ci  debug('xrange', comp)
2491cb0ef41Sopenharmony_ci  comp = replaceStars(comp, options)
2501cb0ef41Sopenharmony_ci  debug('stars', comp)
2511cb0ef41Sopenharmony_ci  return comp
2521cb0ef41Sopenharmony_ci}
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ciconst isX = id => !id || id.toLowerCase() === 'x' || id === '*'
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci// ~, ~> --> * (any, kinda silly)
2571cb0ef41Sopenharmony_ci// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0-0
2581cb0ef41Sopenharmony_ci// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0-0
2591cb0ef41Sopenharmony_ci// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0-0
2601cb0ef41Sopenharmony_ci// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
2611cb0ef41Sopenharmony_ci// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
2621cb0ef41Sopenharmony_ci// ~0.0.1 --> >=0.0.1 <0.1.0-0
2631cb0ef41Sopenharmony_ciconst replaceTildes = (comp, options) => {
2641cb0ef41Sopenharmony_ci  return comp
2651cb0ef41Sopenharmony_ci    .trim()
2661cb0ef41Sopenharmony_ci    .split(/\s+/)
2671cb0ef41Sopenharmony_ci    .map((c) => replaceTilde(c, options))
2681cb0ef41Sopenharmony_ci    .join(' ')
2691cb0ef41Sopenharmony_ci}
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ciconst replaceTilde = (comp, options) => {
2721cb0ef41Sopenharmony_ci  const r = options.loose ? re[t.TILDELOOSE] : re[t.TILDE]
2731cb0ef41Sopenharmony_ci  return comp.replace(r, (_, M, m, p, pr) => {
2741cb0ef41Sopenharmony_ci    debug('tilde', comp, _, M, m, p, pr)
2751cb0ef41Sopenharmony_ci    let ret
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci    if (isX(M)) {
2781cb0ef41Sopenharmony_ci      ret = ''
2791cb0ef41Sopenharmony_ci    } else if (isX(m)) {
2801cb0ef41Sopenharmony_ci      ret = `>=${M}.0.0 <${+M + 1}.0.0-0`
2811cb0ef41Sopenharmony_ci    } else if (isX(p)) {
2821cb0ef41Sopenharmony_ci      // ~1.2 == >=1.2.0 <1.3.0-0
2831cb0ef41Sopenharmony_ci      ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0-0`
2841cb0ef41Sopenharmony_ci    } else if (pr) {
2851cb0ef41Sopenharmony_ci      debug('replaceTilde pr', pr)
2861cb0ef41Sopenharmony_ci      ret = `>=${M}.${m}.${p}-${pr
2871cb0ef41Sopenharmony_ci      } <${M}.${+m + 1}.0-0`
2881cb0ef41Sopenharmony_ci    } else {
2891cb0ef41Sopenharmony_ci      // ~1.2.3 == >=1.2.3 <1.3.0-0
2901cb0ef41Sopenharmony_ci      ret = `>=${M}.${m}.${p
2911cb0ef41Sopenharmony_ci      } <${M}.${+m + 1}.0-0`
2921cb0ef41Sopenharmony_ci    }
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci    debug('tilde return', ret)
2951cb0ef41Sopenharmony_ci    return ret
2961cb0ef41Sopenharmony_ci  })
2971cb0ef41Sopenharmony_ci}
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci// ^ --> * (any, kinda silly)
3001cb0ef41Sopenharmony_ci// ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0-0
3011cb0ef41Sopenharmony_ci// ^2.0, ^2.0.x --> >=2.0.0 <3.0.0-0
3021cb0ef41Sopenharmony_ci// ^1.2, ^1.2.x --> >=1.2.0 <2.0.0-0
3031cb0ef41Sopenharmony_ci// ^1.2.3 --> >=1.2.3 <2.0.0-0
3041cb0ef41Sopenharmony_ci// ^1.2.0 --> >=1.2.0 <2.0.0-0
3051cb0ef41Sopenharmony_ci// ^0.0.1 --> >=0.0.1 <0.0.2-0
3061cb0ef41Sopenharmony_ci// ^0.1.0 --> >=0.1.0 <0.2.0-0
3071cb0ef41Sopenharmony_ciconst replaceCarets = (comp, options) => {
3081cb0ef41Sopenharmony_ci  return comp
3091cb0ef41Sopenharmony_ci    .trim()
3101cb0ef41Sopenharmony_ci    .split(/\s+/)
3111cb0ef41Sopenharmony_ci    .map((c) => replaceCaret(c, options))
3121cb0ef41Sopenharmony_ci    .join(' ')
3131cb0ef41Sopenharmony_ci}
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_ciconst replaceCaret = (comp, options) => {
3161cb0ef41Sopenharmony_ci  debug('caret', comp, options)
3171cb0ef41Sopenharmony_ci  const r = options.loose ? re[t.CARETLOOSE] : re[t.CARET]
3181cb0ef41Sopenharmony_ci  const z = options.includePrerelease ? '-0' : ''
3191cb0ef41Sopenharmony_ci  return comp.replace(r, (_, M, m, p, pr) => {
3201cb0ef41Sopenharmony_ci    debug('caret', comp, _, M, m, p, pr)
3211cb0ef41Sopenharmony_ci    let ret
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci    if (isX(M)) {
3241cb0ef41Sopenharmony_ci      ret = ''
3251cb0ef41Sopenharmony_ci    } else if (isX(m)) {
3261cb0ef41Sopenharmony_ci      ret = `>=${M}.0.0${z} <${+M + 1}.0.0-0`
3271cb0ef41Sopenharmony_ci    } else if (isX(p)) {
3281cb0ef41Sopenharmony_ci      if (M === '0') {
3291cb0ef41Sopenharmony_ci        ret = `>=${M}.${m}.0${z} <${M}.${+m + 1}.0-0`
3301cb0ef41Sopenharmony_ci      } else {
3311cb0ef41Sopenharmony_ci        ret = `>=${M}.${m}.0${z} <${+M + 1}.0.0-0`
3321cb0ef41Sopenharmony_ci      }
3331cb0ef41Sopenharmony_ci    } else if (pr) {
3341cb0ef41Sopenharmony_ci      debug('replaceCaret pr', pr)
3351cb0ef41Sopenharmony_ci      if (M === '0') {
3361cb0ef41Sopenharmony_ci        if (m === '0') {
3371cb0ef41Sopenharmony_ci          ret = `>=${M}.${m}.${p}-${pr
3381cb0ef41Sopenharmony_ci          } <${M}.${m}.${+p + 1}-0`
3391cb0ef41Sopenharmony_ci        } else {
3401cb0ef41Sopenharmony_ci          ret = `>=${M}.${m}.${p}-${pr
3411cb0ef41Sopenharmony_ci          } <${M}.${+m + 1}.0-0`
3421cb0ef41Sopenharmony_ci        }
3431cb0ef41Sopenharmony_ci      } else {
3441cb0ef41Sopenharmony_ci        ret = `>=${M}.${m}.${p}-${pr
3451cb0ef41Sopenharmony_ci        } <${+M + 1}.0.0-0`
3461cb0ef41Sopenharmony_ci      }
3471cb0ef41Sopenharmony_ci    } else {
3481cb0ef41Sopenharmony_ci      debug('no pr')
3491cb0ef41Sopenharmony_ci      if (M === '0') {
3501cb0ef41Sopenharmony_ci        if (m === '0') {
3511cb0ef41Sopenharmony_ci          ret = `>=${M}.${m}.${p
3521cb0ef41Sopenharmony_ci          }${z} <${M}.${m}.${+p + 1}-0`
3531cb0ef41Sopenharmony_ci        } else {
3541cb0ef41Sopenharmony_ci          ret = `>=${M}.${m}.${p
3551cb0ef41Sopenharmony_ci          }${z} <${M}.${+m + 1}.0-0`
3561cb0ef41Sopenharmony_ci        }
3571cb0ef41Sopenharmony_ci      } else {
3581cb0ef41Sopenharmony_ci        ret = `>=${M}.${m}.${p
3591cb0ef41Sopenharmony_ci        } <${+M + 1}.0.0-0`
3601cb0ef41Sopenharmony_ci      }
3611cb0ef41Sopenharmony_ci    }
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_ci    debug('caret return', ret)
3641cb0ef41Sopenharmony_ci    return ret
3651cb0ef41Sopenharmony_ci  })
3661cb0ef41Sopenharmony_ci}
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ciconst replaceXRanges = (comp, options) => {
3691cb0ef41Sopenharmony_ci  debug('replaceXRanges', comp, options)
3701cb0ef41Sopenharmony_ci  return comp
3711cb0ef41Sopenharmony_ci    .split(/\s+/)
3721cb0ef41Sopenharmony_ci    .map((c) => replaceXRange(c, options))
3731cb0ef41Sopenharmony_ci    .join(' ')
3741cb0ef41Sopenharmony_ci}
3751cb0ef41Sopenharmony_ci
3761cb0ef41Sopenharmony_ciconst replaceXRange = (comp, options) => {
3771cb0ef41Sopenharmony_ci  comp = comp.trim()
3781cb0ef41Sopenharmony_ci  const r = options.loose ? re[t.XRANGELOOSE] : re[t.XRANGE]
3791cb0ef41Sopenharmony_ci  return comp.replace(r, (ret, gtlt, M, m, p, pr) => {
3801cb0ef41Sopenharmony_ci    debug('xRange', comp, ret, gtlt, M, m, p, pr)
3811cb0ef41Sopenharmony_ci    const xM = isX(M)
3821cb0ef41Sopenharmony_ci    const xm = xM || isX(m)
3831cb0ef41Sopenharmony_ci    const xp = xm || isX(p)
3841cb0ef41Sopenharmony_ci    const anyX = xp
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ci    if (gtlt === '=' && anyX) {
3871cb0ef41Sopenharmony_ci      gtlt = ''
3881cb0ef41Sopenharmony_ci    }
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci    // if we're including prereleases in the match, then we need
3911cb0ef41Sopenharmony_ci    // to fix this to -0, the lowest possible prerelease value
3921cb0ef41Sopenharmony_ci    pr = options.includePrerelease ? '-0' : ''
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_ci    if (xM) {
3951cb0ef41Sopenharmony_ci      if (gtlt === '>' || gtlt === '<') {
3961cb0ef41Sopenharmony_ci        // nothing is allowed
3971cb0ef41Sopenharmony_ci        ret = '<0.0.0-0'
3981cb0ef41Sopenharmony_ci      } else {
3991cb0ef41Sopenharmony_ci        // nothing is forbidden
4001cb0ef41Sopenharmony_ci        ret = '*'
4011cb0ef41Sopenharmony_ci      }
4021cb0ef41Sopenharmony_ci    } else if (gtlt && anyX) {
4031cb0ef41Sopenharmony_ci      // we know patch is an x, because we have any x at all.
4041cb0ef41Sopenharmony_ci      // replace X with 0
4051cb0ef41Sopenharmony_ci      if (xm) {
4061cb0ef41Sopenharmony_ci        m = 0
4071cb0ef41Sopenharmony_ci      }
4081cb0ef41Sopenharmony_ci      p = 0
4091cb0ef41Sopenharmony_ci
4101cb0ef41Sopenharmony_ci      if (gtlt === '>') {
4111cb0ef41Sopenharmony_ci        // >1 => >=2.0.0
4121cb0ef41Sopenharmony_ci        // >1.2 => >=1.3.0
4131cb0ef41Sopenharmony_ci        gtlt = '>='
4141cb0ef41Sopenharmony_ci        if (xm) {
4151cb0ef41Sopenharmony_ci          M = +M + 1
4161cb0ef41Sopenharmony_ci          m = 0
4171cb0ef41Sopenharmony_ci          p = 0
4181cb0ef41Sopenharmony_ci        } else {
4191cb0ef41Sopenharmony_ci          m = +m + 1
4201cb0ef41Sopenharmony_ci          p = 0
4211cb0ef41Sopenharmony_ci        }
4221cb0ef41Sopenharmony_ci      } else if (gtlt === '<=') {
4231cb0ef41Sopenharmony_ci        // <=0.7.x is actually <0.8.0, since any 0.7.x should
4241cb0ef41Sopenharmony_ci        // pass.  Similarly, <=7.x is actually <8.0.0, etc.
4251cb0ef41Sopenharmony_ci        gtlt = '<'
4261cb0ef41Sopenharmony_ci        if (xm) {
4271cb0ef41Sopenharmony_ci          M = +M + 1
4281cb0ef41Sopenharmony_ci        } else {
4291cb0ef41Sopenharmony_ci          m = +m + 1
4301cb0ef41Sopenharmony_ci        }
4311cb0ef41Sopenharmony_ci      }
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ci      if (gtlt === '<') {
4341cb0ef41Sopenharmony_ci        pr = '-0'
4351cb0ef41Sopenharmony_ci      }
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_ci      ret = `${gtlt + M}.${m}.${p}${pr}`
4381cb0ef41Sopenharmony_ci    } else if (xm) {
4391cb0ef41Sopenharmony_ci      ret = `>=${M}.0.0${pr} <${+M + 1}.0.0-0`
4401cb0ef41Sopenharmony_ci    } else if (xp) {
4411cb0ef41Sopenharmony_ci      ret = `>=${M}.${m}.0${pr
4421cb0ef41Sopenharmony_ci      } <${M}.${+m + 1}.0-0`
4431cb0ef41Sopenharmony_ci    }
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ci    debug('xRange return', ret)
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ci    return ret
4481cb0ef41Sopenharmony_ci  })
4491cb0ef41Sopenharmony_ci}
4501cb0ef41Sopenharmony_ci
4511cb0ef41Sopenharmony_ci// Because * is AND-ed with everything else in the comparator,
4521cb0ef41Sopenharmony_ci// and '' means "any version", just remove the *s entirely.
4531cb0ef41Sopenharmony_ciconst replaceStars = (comp, options) => {
4541cb0ef41Sopenharmony_ci  debug('replaceStars', comp, options)
4551cb0ef41Sopenharmony_ci  // Looseness is ignored here.  star is always as loose as it gets!
4561cb0ef41Sopenharmony_ci  return comp
4571cb0ef41Sopenharmony_ci    .trim()
4581cb0ef41Sopenharmony_ci    .replace(re[t.STAR], '')
4591cb0ef41Sopenharmony_ci}
4601cb0ef41Sopenharmony_ci
4611cb0ef41Sopenharmony_ciconst replaceGTE0 = (comp, options) => {
4621cb0ef41Sopenharmony_ci  debug('replaceGTE0', comp, options)
4631cb0ef41Sopenharmony_ci  return comp
4641cb0ef41Sopenharmony_ci    .trim()
4651cb0ef41Sopenharmony_ci    .replace(re[options.includePrerelease ? t.GTE0PRE : t.GTE0], '')
4661cb0ef41Sopenharmony_ci}
4671cb0ef41Sopenharmony_ci
4681cb0ef41Sopenharmony_ci// This function is passed to string.replace(re[t.HYPHENRANGE])
4691cb0ef41Sopenharmony_ci// M, m, patch, prerelease, build
4701cb0ef41Sopenharmony_ci// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
4711cb0ef41Sopenharmony_ci// 1.2.3 - 3.4 => >=1.2.0 <3.5.0-0 Any 3.4.x will do
4721cb0ef41Sopenharmony_ci// 1.2 - 3.4 => >=1.2.0 <3.5.0-0
4731cb0ef41Sopenharmony_ciconst hyphenReplace = incPr => ($0,
4741cb0ef41Sopenharmony_ci  from, fM, fm, fp, fpr, fb,
4751cb0ef41Sopenharmony_ci  to, tM, tm, tp, tpr, tb) => {
4761cb0ef41Sopenharmony_ci  if (isX(fM)) {
4771cb0ef41Sopenharmony_ci    from = ''
4781cb0ef41Sopenharmony_ci  } else if (isX(fm)) {
4791cb0ef41Sopenharmony_ci    from = `>=${fM}.0.0${incPr ? '-0' : ''}`
4801cb0ef41Sopenharmony_ci  } else if (isX(fp)) {
4811cb0ef41Sopenharmony_ci    from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
4821cb0ef41Sopenharmony_ci  } else if (fpr) {
4831cb0ef41Sopenharmony_ci    from = `>=${from}`
4841cb0ef41Sopenharmony_ci  } else {
4851cb0ef41Sopenharmony_ci    from = `>=${from}${incPr ? '-0' : ''}`
4861cb0ef41Sopenharmony_ci  }
4871cb0ef41Sopenharmony_ci
4881cb0ef41Sopenharmony_ci  if (isX(tM)) {
4891cb0ef41Sopenharmony_ci    to = ''
4901cb0ef41Sopenharmony_ci  } else if (isX(tm)) {
4911cb0ef41Sopenharmony_ci    to = `<${+tM + 1}.0.0-0`
4921cb0ef41Sopenharmony_ci  } else if (isX(tp)) {
4931cb0ef41Sopenharmony_ci    to = `<${tM}.${+tm + 1}.0-0`
4941cb0ef41Sopenharmony_ci  } else if (tpr) {
4951cb0ef41Sopenharmony_ci    to = `<=${tM}.${tm}.${tp}-${tpr}`
4961cb0ef41Sopenharmony_ci  } else if (incPr) {
4971cb0ef41Sopenharmony_ci    to = `<${tM}.${tm}.${+tp + 1}-0`
4981cb0ef41Sopenharmony_ci  } else {
4991cb0ef41Sopenharmony_ci    to = `<=${to}`
5001cb0ef41Sopenharmony_ci  }
5011cb0ef41Sopenharmony_ci
5021cb0ef41Sopenharmony_ci  return `${from} ${to}`.trim()
5031cb0ef41Sopenharmony_ci}
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ciconst testSet = (set, version, options) => {
5061cb0ef41Sopenharmony_ci  for (let i = 0; i < set.length; i++) {
5071cb0ef41Sopenharmony_ci    if (!set[i].test(version)) {
5081cb0ef41Sopenharmony_ci      return false
5091cb0ef41Sopenharmony_ci    }
5101cb0ef41Sopenharmony_ci  }
5111cb0ef41Sopenharmony_ci
5121cb0ef41Sopenharmony_ci  if (version.prerelease.length && !options.includePrerelease) {
5131cb0ef41Sopenharmony_ci    // Find the set of versions that are allowed to have prereleases
5141cb0ef41Sopenharmony_ci    // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
5151cb0ef41Sopenharmony_ci    // That should allow `1.2.3-pr.2` to pass.
5161cb0ef41Sopenharmony_ci    // However, `1.2.4-alpha.notready` should NOT be allowed,
5171cb0ef41Sopenharmony_ci    // even though it's within the range set by the comparators.
5181cb0ef41Sopenharmony_ci    for (let i = 0; i < set.length; i++) {
5191cb0ef41Sopenharmony_ci      debug(set[i].semver)
5201cb0ef41Sopenharmony_ci      if (set[i].semver === Comparator.ANY) {
5211cb0ef41Sopenharmony_ci        continue
5221cb0ef41Sopenharmony_ci      }
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci      if (set[i].semver.prerelease.length > 0) {
5251cb0ef41Sopenharmony_ci        const allowed = set[i].semver
5261cb0ef41Sopenharmony_ci        if (allowed.major === version.major &&
5271cb0ef41Sopenharmony_ci            allowed.minor === version.minor &&
5281cb0ef41Sopenharmony_ci            allowed.patch === version.patch) {
5291cb0ef41Sopenharmony_ci          return true
5301cb0ef41Sopenharmony_ci        }
5311cb0ef41Sopenharmony_ci      }
5321cb0ef41Sopenharmony_ci    }
5331cb0ef41Sopenharmony_ci
5341cb0ef41Sopenharmony_ci    // Version has a -pre, but it's not one of the ones we like.
5351cb0ef41Sopenharmony_ci    return false
5361cb0ef41Sopenharmony_ci  }
5371cb0ef41Sopenharmony_ci
5381cb0ef41Sopenharmony_ci  return true
5391cb0ef41Sopenharmony_ci}
540