How to use the Range function from semver

Find comprehensive JavaScript semver.Range code examples handpicked from public code repositorys.

semver.Range is a class in the semver package used to represent a semantic versioning range of valid versions.

162
163
164
165
166
167
168
169
170
171
if (r || !maybe) {
        return r;
}

// need to determine if the version is greater than any range
const range = new semver.Range(str);
for (let i = 0; i < range.set.length; i++) {
        const set = range.set[i];
        for (let j = set.length - 1; j >= 0; j--) {
                if (set[j].semver instanceof semver.SemVer) {
fork icon29
star icon11
watch icon53

35
36
37
38
39
40
41
42
43
44

function superset(allowFixed, v1, v2) {
    if (v1 === v2) return true;
    if (!semver.validRange(v1) || !semver.validRange(v2)) return false;
    const r1 = new semver.Range(v1);
    const r2 = new semver.Range(v2);
    if (r1.set[0][0].operator === r2.set[0][0].operator || (allowFixed && (r2.set[0][0].operator === ''))) {
        switch (r1.set[0][0].operator) {
            case '': return semver.eq(r2.set[0][0].semver.version, r1.set[0][0].semver.version);
            case '>=':
fork icon4
star icon0
watch icon35

+ 3 other calls in file

How does semver.Range work?

semver.Range is a class in the semver package that represents a range of valid versions for a software package, which can be used to filter a list of available versions to find the best match for a given set of requirements. It allows for a wide variety of version range syntax, including comparison operators, wildcards, and hyphenated ranges.

3
4
5
6
7
8
9
10
11
12
13
14
var resolves = require('semver-resolves')
var sortCompartors = require('sort-semver-comparators')
var last = require('array-last')


exports.range = function isRangeBounded (range) {
  return new semver.Range(range).set.every(areComparatorsBounded)
}


exports.comparators = areComparatorsBounded

fork icon0
star icon0
watch icon2

47
48
49
50
51
52
53
54
55
56
const s = String(x)
let ret = cache.get(s) || null

if (!ret) {
  try {
    ret = new semver.Range(s)
  } catch (_error) {
    // Ignore parsing error.
  }
  cache.set(s, ret)
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
const semver = require("semver");

const range = new semver.Range("2.x || >= 5.0.0");

console.log(range.test("2.3.1")); // true
console.log(range.test("3.0.0")); // true
console.log(range.test("4.0.0")); // false
console.log(range.test("5.0.0")); // true
console.log(range.test("6.0.0")); // true

In this example, we create a new instance of semver.Range and pass in a range string '2.x || >= 5.0.0'. We then use the test method to check if different versions of a package match the specified range. In this case, 2.3.1, 3.0.0, 5.0.0, and 6.0.0 all match the range, but 4.0.0 does not.

123
124
125
126
127
128
129
130
131
132
},
modules: {
    ruleId: "no-modules",
    cases: [
        {
            supported: new Range("^12.17 || >=13.2"),
            messageId: "no-modules",
        },
    ],
},
fork icon0
star icon0
watch icon1

+ 9 other calls in file

86
87
88
89
90
91
92
93
94
95
//     }
//     limitList.add(triple);
// }

/** @type {semver.Range | null} */
let intersection = new semver.Range('*');

for (const [, , version] of versions) {
    intersection = getIntersection(version, intersection);
    if (intersection === null) {
fork icon0
star icon0
watch icon1

+ 11 other calls in file

489
490
491
492
493
494
495
496
497
498
// This is a temporary workaround to allow data to be passed back from the update schematic
// tslint:disable-next-line: no-any
const migrations = global.externalMigrations;
if (success && migrations) {
    for (const migration of migrations) {
        const result = await this.executeMigrations(migration.package, migration.collection, new semver.Range('>' + migration.from + ' <=' + migration.to), options.createCommits);
        if (!result) {
            return 0;
        }
    }
fork icon0
star icon0
watch icon0

+ 3 other calls in file

261
262
263
264
265
266
267
268
269
270
/**
 * @return Whether or not the migrations were performed successfully.
 */
async executeMigrations(workflow, packageName, collectionPath, from, to, commit) {
    const collection = workflow.engine.createCollection(collectionPath);
    const migrationRange = new semver.Range('>' + (semver.prerelease(from) ? from.split('-')[0] + '-0' : from) + ' <=' + to.split('-')[0]);
    const migrations = [];
    for (const name of collection.listSchematicNames()) {
        const schematic = workflow.engine.createSchematic(name, collection);
        const description = schematic.description;
fork icon0
star icon0
watch icon0