CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
simplify.js48 linesDownload Raw Back to ranges
1// given a set of versions and a range, create a "simplified" range2// that includes the same versions that the original range does3// 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 = null9  let prev = null10  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 = version15      if (!first) {16        first = version17      }18    } else {19      if (prev) {20        set.push([first, prev])21      }22      prev = null23      first = null24    }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 : range47}48