How to use the eq function from semver

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

The semver.eq method compares two semantic version numbers for equality.

41
42
43
44
45
46
47
48
49
50
 * @param {String} v1 - The first version to compare
 * @param {String} v2 - The second version to compare
 * @returns {Boolean} True if the versions are equal
 */
exports.eq = function eq(v1, v2) {
        return semver.eq(format(v1, 3, 3), format(v2, 3, 3));
};

/**
 * Converts two versions into 3 segment format, then checks if the first version is less than the
fork icon29
star icon11
watch icon53

38
39
40
41
42
43
44
45
46
47
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 '>=':
        case '>': return semver.gte(r2.set[0][0].semver.version, r1.set[0][0].semver.version);
    }
}
fork icon4
star icon0
watch icon35

How does semver.eq work?

The semver.eq method is part of the SemVer library, which provides a way to compare and manipulate semantic version numbers in JavaScript. The eq method takes two version numbers as input and compares them for equality. The version numbers must be in the format of major.minor.patch and may include a pre-release or build metadata. The comparison is done by comparing each part of the version number, starting with the major version. If any part of the version number is not equal, the comparison stops and returns false. If all parts of the version number are equal, the comparison returns true. The comparison is done according to the SemVer specification, which defines rules for comparing and ordering version numbers. For example, a version number with a higher major version number is considered to be greater than a version number with a lower major version number, regardless of the values of the minor and patch versions. The eq method also supports loose mode, which allows for more lenient comparisons of version numbers. In loose mode, trailing zeros in the version number are ignored, and pre-release and build metadata are ignored when comparing versions. Overall, the eq method provides a simple way to check whether two version numbers are equal according to the SemVer specification.

179
180
181
182
183
184
185
186
187
188
var comparator = condition.startsWith('<') ? semver.lt : semver.gt;
var strict = condition === '<' || condition === '>';

if (comparator(version, boundingVersion)) {
    return range;
} else if (strict && semver.eq(version, boundingVersion)) {
    return range;
} else {
    return bound;
}
fork icon0
star icon0
watch icon1

12
13
14
15
16
17
18
19
20
21

const releases = await octokit.repos.getReleases({
  owner: 'atom',
  repo: 'atom'
});
const release = releases.data.find(r => semver.eq(r.name, releaseVersion));

return {
  exists: release !== undefined,
  isDraft: release && release.draft,
fork icon0
star icon0
watch icon1

Ai Example

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

const version1 = "1.2.3";
const version2 = "1.2.3-beta";

const isEqual = semver.eq(version1, version2);

console.log(isEqual);
// Output: false

In this example, we have two version numbers: 1.2.3 and 1.2.3-beta. We use semver.eq to compare the two version numbers for equality, storing the result in a boolean variable isEqual. The output of the console.log statement shows that the two version numbers are not equal, according to the SemVer specification. This is because the second version number includes a pre-release identifier, which is not present in the first version number. If we were to remove the -beta suffix from the second version number, the comparison would return true because both version numbers would be identical.

346
347
348
349
350
351
352
353
354
355
let actualFunc = semverFunc

// if we're asked to infer, we examine outputs to make a best guess
if (actualFunc === 'infer') {
  if (valueIsVersion && attrIsVersion) {
    // two versions -> semver.eq
    actualFunc = 'eq'
  } else if (!valueIsVersion && !attrIsVersion) {
    // two ranges -> semver.intersects
    actualFunc = 'intersects'
fork icon0
star icon0
watch icon1

+ 4 other calls in file

138
139
140
141
142
143
144
145
146
147
{
  title: 'dynamic importing js file from NON module package is not supported',
  testCase: 'knexfile-imports',
  knexfile: 'knexfile2.mjs',
  knexArgs: ['migrate:latest'],
  expectedErrorMessage: semver.eq(process.version, 'v14.13.0')
    ? 'Unexpected export statement in CJS module'
    : semver.gte(process.version, 'v14.14.0')
    ? 'Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.'
    : "Unexpected token 'export'",
fork icon0
star icon0
watch icon0

167
168
169
170
171
172
173
174
175
176
  !lockVersion &&
  targetVersion !== null &&
  semver.neq(targetVersion, currentVersion)
) {
  const targetFileInfo = firmwareList.find((fileInfo) => {
    return semver.eq(fileInfo.version, targetVersion);
  });

  const targetPath = path.join(
    esp32BaseRepository,
fork icon0
star icon0
watch icon0