How to use the major function from semver

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

semver.major is a function that extracts the major version number from a semantic versioning string.

362
363
364
365
366
367
368
369
370
371
).raw;
let fileVersion =
  lockfileContent && lockfileContent.version
    ? semver.coerce(lockfileContent.version, true).raw
    : "1.0.0";
if (semver.major(schemaVersion) > semver.major(fileVersion)) {
  if (this.argv.force) {
    let config = {
      verbose: this.argv.verbose,
      quiet: this.argv.quiet,
fork icon255
star icon758
watch icon77

205
206
207
208
209
210
211
212
213
214
/**
 * @param {string} version
 * @returns {int}
 */
function versionToReleaseWeight(version) {
  return semver.major(version) > 0
    ? RELEASE_WEIGHT.MAJOR
    : semver.minor(version) > 0
    ? RELEASE_WEIGHT.MINOR
    : RELEASE_WEIGHT.PATCH
fork icon202
star icon563
watch icon40

+ 2 other calls in file

How does semver.major work?

semver.major is a function that takes a version string as input and returns the major version number as an integer, according to the semantic versioning standard. It does this by parsing the version string and extracting the major version number from it.

297
298
299
300
301
302
303
304
305
306
realPkgVersion = semver.maxSatisfying(allVersions, range);
if (realPkgVersion) {
  // try to use latest-{major} tag version on range
  // ^1.0.1 =range=> get 1.0.3 in (1.0.2, 1.0.3), but latest-1 tag is 1.0.2
  // finnaly we should use 1.0.2 on ^1.0.1
  const major = semver.major(realPkgVersion);
  if (major) {
    const latestMajorVersion = distTags[`latest-${major}`];
    if (latestMajorVersion && semver.satisfies(latestMajorVersion, spec)) {
      realPkgVersion = latestMajorVersion;
fork icon64
star icon476
watch icon17

829
830
831
832
833
834
835
836
837
838
839


const getDockerVersion = () => {
  try {
    const response = execSync('docker-compose -v').toString();
    const version = response.match(semver.re[3])[0];
    return semver.major(version);
  } catch (err) {
    console.error(err);
    return 1;
  }
fork icon181
star icon402
watch icon53

Ai Example

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

const version1 = "1.2.3";
const version2 = "2.0.0-beta.1";

console.log(semver.major(version1)); // Output: 1
console.log(semver.major(version2)); // Output: 2

In this example, we first require the semver module. We then define two versions, version1 and version2. We pass each version to the semver.major function, which returns the major version number of the version string. We then log the output of each function call to the console.

100
101
102
103
104
105
106
107
108
109
110
111
function useNewArgsFormat(pmdVersion) {
    return semver.gte(pmdVersion, '6.41.0');
}


function isPmd7Cli(pmdVersion) {
    return semver.major(pmdVersion) >= 7;
}


async function determinePmdRelease(pmdVersion, token) {
    core.debug(`determine release info for ${pmdVersion}`);
fork icon19
star icon20
watch icon0

+ 2 other calls in file

47
48
49
50
51
52
53
54
55
56
}

// only compare to versions smaller than the new one
if (semver.gt(ghostVersion, lastVersion)) {
    // check if the majors are the same
    if (semver.major(lastVersion) === semver.major(ghostVersion)) {
        sameMajorReleaseTags.push(lastVersion);
    } else {
        otherReleaseTags.push(lastVersion);
    }
fork icon6
star icon6
watch icon11

308
309
310
311
312
313
314
315
316
317
await self.test('using updated firebase-admin', async function () {
  let pkg = 'firebase-admin';
  // let latest = semver.clean(await getPkgVersion(pkg));
  let latest = semver.clean(cleanPackageVersion(self.packageJSON.dependencies['firebase-admin']));
  let mine = cleanPackageVersion(self.package.dependencies[pkg] || '0.0.0');
  const majorVersionMismatch = ((semver.major(latest) > semver.major(mine)));
  let bemv = cleanPackageVersion(self.packageJSON.dependencies[pkg]);
  bemPackageVersionWarning(pkg, bemv, latest);

  if (majorVersionMismatch) {
fork icon0
star icon2
watch icon2

+ 11 other calls in file

47
48
49
50
51
52
53
54
55

    // ^0.5.0
    return `^${min}`;
}

if (semver.major(min) !== semver.major(max)) {
    if (semver.major(min) === 0) {
        return '0';
    }
fork icon0
star icon0
watch icon1

33
34
35
36
37
38
39
40
41
42
43
44
45
    return false;
  }


  const hasVNextTag = !!metadata.tags?.includes('vNext');


  return semver.major(packageJson.version) >= 9 || isNightlyVersion(packageJson.version) || hasVNextTag;
}


/**
 * @param {Object} [options]
fork icon0
star icon0
watch icon0

12
13
14
15
16
17
18
19
20
21
 * @returns {object} An object with the browser as the key and the browser-specific version object
 * as the value.  For example, the version `9.6.0-beta.1` would return the object
 * `{ firefox: { version: '9.6.0.beta1' }, chrome: { version: '9.6.0.1', version_name: '9.6.0-beta.1' } }`.
 */
function getBrowserVersionMap(platforms, version) {
  const major = semver.major(version);
  const minor = semver.minor(version);
  const patch = semver.patch(version);
  const prerelease = semver.prerelease(version);
  let buildType, buildVersionSummary, buildVersion;
fork icon0
star icon0
watch icon0

79
80
81
82
83
84
85
86
87
88
89
        'isPrerelease': semver.prerelease(currentVersion)
    });
}


if (outputMode.isJazzy) {
    const minorReleaseSeries = semver.major(currentVersion) + "." + semver.minor(currentVersion) + ".0";
    const range = ">=" + minorReleaseSeries + " <" + currentVersion;
    const otherReleasesInSeries = _.filter(releaseNotes, function(release) {
        return semver.satisfies(release.version, range);
    });
fork icon0
star icon0
watch icon0

4
5
6
7
8
9
10
11
12
13
14
15
module.exports = (version) => {
  if (!semver.valid(version)) return '';


  const changelogs = 'https://github.com/nodejs/node/blob/main/doc/changelogs';
  const clean = semver.clean(version);
  const major = semver.major(clean);
  const minor = semver.minor(clean);


  if (major >= 4) return `${changelogs}/CHANGELOG_V${major}.md#${clean}`;
  if (major >= 1) return `${changelogs}/CHANGELOG_IOJS.md#${clean}`;
fork icon0
star icon0
watch icon0

408
409
410
411
412
413
414
415
416
417
    : handleResponse(cache.full.error);
}
// This is just a request sanity check, only puke if there are major differences.
else {
  // Update the valid cache for following GET requests.
  cache.partial.isValid = semver.major(document.version) === semver.major(config.schema);

  debug.sanity(`Has Partial Response is valid: ${cache.partial.isValid}`);
  return cache.partial.isValid
    ? handleResponse()
fork icon0
star icon0
watch icon0