How to use the compare function from semver

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

semver.compare is a function in the semver library that compares two version strings and returns a value indicating if the first version is greater than, equal to, or less than the second version.

40
41
42
43
44
45
46
47
48
49
  }
}

function rcmp(a, b) {
  return icmp(a.semver, b.semver) ||
    semver.compare(a.semver, b.semver) ||
    cmp(a.operator, b.operator);
}

function min(a, b) {
fork icon3
star icon3
watch icon2

9
10
11
12
13
14
15
16
17
18
const rootPath = path.join(__dirname, '..')
const stripRange = (version) => version.replace(/^[~^]/, '')
const sortRanges = (ranges) =>
  ranges.sort((a, b) => {
    try {
      return semver.compare(stripRange(a), stripRange(b))
    } catch (err) {
      return 1
    }
  })
fork icon357
star icon0
watch icon73

How does semver.compare work?

semver.compare is a function in the semver library that compares two semver version strings according to the SemVer specification, and returns either -1, 0, or 1 to indicate whether the first version is less than, equal to, or greater than the second version. In detail, semver.compare splits each version string into its constituent major, minor, and patch numbers, and then compares them in turn, starting with the major version number. If the major version numbers are not equal, the function returns the result of comparing them. If the major version numbers are equal, the function compares the minor version numbers, and then the patch version numbers, until a difference is found or both version strings have been fully compared. If the two version strings are identical, the function returns 0. If the first version string is greater than the second, the function returns 1, and if it is less than the second, it returns -1.

353
354
355
356
357
358
359
360
361
362
        version,
        tag,
        target,
    };
})
    .filter(({ info, version, target }) => (target === null || target === void 0 ? void 0 : target['ng-update']) && semver.compare(info.installed.version, version) < 0)
    .map(({ name, info, version, tag, target }) => {
    var _a;
    // Look for packageGroup.
    const packageGroup = target['ng-update']['packageGroup'];
fork icon1
star icon0
watch icon0

335
336
337
338
339
340
341
342
343
344
        tag,
        target,
    };
})
    .filter(({ info, version, target }) => {
    return target && semver.compare(info.installed.version, version) < 0;
})
    .filter(({ target }) => {
    return target['ng-update'];
})
fork icon1
star icon0
watch icon0

Ai Example

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

const version1 = "1.2.3";
const version2 = "2.0.0";

const result = semver.compare(version1, version2);

console.log(result); // -1

In this example, we're comparing the version number 1.2.3 to 2.0.0. Since 1.2.3 is considered "less than" 2.0.0, the result of semver.compare is -1.

161
162
163
164
165
166
167
168
169
170
171
test('allTargets are sorted', function (t) {
  var electron = allTargets.filter(function (t) { return t.runtime === 'electron' })
  var node = allTargets.filter(function (t) { return t.runtime === 'node' })
  var nodeWebkit = allTargets.filter(function (t) { return t.runtime === 'node-webkit' })
  function sort (t1, t2) {
    return semver.compare(t1.target, t2.target)
  }


  t.deepEqual(electron, electron.slice().sort(sort), 'electron targets are sorted')
  t.deepEqual(node, node.slice().sort(sort), 'node targets are sorted')
fork icon0
star icon0
watch icon1

40
41
42
43
44
45
46
47
48
49
  if (version.major >= versionInfo.currentVersion.major) {
    return;
  }

  const currentMajor = majorVersions[version.major];
  if (currentMajor === undefined || semver.compare(version, currentMajor) === 1) {
    // This version is newer than the currently captured version for this major.
    majorVersions[version.major] = version;
  }
});
fork icon0
star icon0
watch icon1

+ 7 other calls in file

335
336
337
338
339
340
341
342
343
344
        tag,
        target,
    };
})
    .filter(({ name, info, version, target }) => {
    return (target && semver.compare(info.installed.version, version) < 0);
})
    .filter(({ target }) => {
    return target['ng-update'];
})
fork icon0
star icon0
watch icon0

147
148
149
150
151
152
153
154
155
156
    }
    if (semver.satisfies(description.version, range, { includePrerelease: true })) {
        migrations.push(description);
    }
}
migrations.sort((a, b) => semver.compare(a.version, b.version) || a.name.localeCompare(b.name));
if (migrations.length === 0) {
    return true;
}
this.logger.info(color_1.colors.cyan(`** Executing migrations of package '${packageName}' **\n`));
fork icon0
star icon0
watch icon0

149
150
151
152
153
154
155
156
157
158

const currentVersion = semver.parse(currentAppVersion);

let firmwareList = getFirmwareList(currentAppPlatformType, currentAppTitle);
firmwareList.sort((fileInfo1, fileInfo2) => {
  return semver.compare(fileInfo1.version, fileInfo2.version);
});

const versionList = firmwareList.map((firmware) => {
  return firmware.version;
fork icon0
star icon0
watch icon0