Team Ai
Datasetpublic

codekingpro/portable-devtools

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