How to use the prerelease function from semver

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

semver.prerelease is a function in the semver library that returns an array of prerelease components from a version string, if any.

18
19
20
21
22
23
24
25
26
27
// Include prelease versions, like v1.0.0-rc1 or v1.0.1-dev
if (versionConstraint == 'next') {
  version = releaseVersions[0]
} else if (versionConstraint == 'latest') {
  version = releaseVersions.filter(version => {
    const prereleaseComponents = semver.prerelease(version);
    return prereleaseComponents == null || prereleaseComponents.length == 0
  })[0];
} else if (validVersionConstraint) {
  version = matchVersion(releaseVersions, validVersionConstraint, false);
fork icon4
star icon6
watch icon0

59
60
61
62
63
64
65
66
67
68
            event.sender.send('autoUpdateNotification', 'realerror', err)
        })
    break
case 'allowPrereleaseChange':
    if(!data){
        const preRelComp = semver.prerelease(app.getVersion())
        if(preRelComp != null && preRelComp.length > 0){
            autoUpdater.allowPrerelease = true
        } else {
            autoUpdater.allowPrerelease = data
fork icon3
star icon7
watch icon0

+ 20 other calls in file

How does semver.prerelease work?

semver.prerelease is a function provided by the semver library that allows you to extract the prerelease components from a version string. To use semver.prerelease, you pass in a version string as an argument. The function returns an array containing the prerelease components of the version string, if any. Prerelease components are any additional labels added to a version number beyond the main version number, separated by a hyphen (-). For example, in the version string "1.2.3-beta.1", "beta" and "1" are the prerelease components. If the version string does not contain any prerelease components, semver.prerelease returns null. semver.prerelease is one of many functions provided by the semver library that simplify working with version numbers in JavaScript. It is particularly useful when you need to parse or manipulate version strings that contain prerelease components.

1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
 * 
 * @param {string} version The semver version to test.
 * @returns {boolean} True if the version is a prerelease, otherwise false.
 */
function isPrerelease(version){
    const preRelComp = semver.prerelease(version)
    return preRelComp != null && preRelComp.length > 0
}


/**
fork icon0
star icon1
watch icon0

178
179
180
181
182
183
184
185
186
187
let targetVersion = patchVersion;

// If we're currently in a pre-release we need to manually execute the
// patch bump up to the next version. And we also need to make sure we
// resume the releases at the same pre-release tag.
const currentPrerelease = semver.prerelease(pkg.packageJson.version);
if (currentPrerelease) {
  const parsed = semver.parse(targetVersion);
  parsed.inc('patch');
  parsed.prerelease = currentPrerelease;
fork icon0
star icon0
watch icon1

Ai Example

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

const versionString = "1.2.3-beta.1";

const prerelease = semver.prerelease(versionString);

console.log(prerelease);

In this example, we first import the semver library and define a version string that contains a prerelease component. We then use semver.prerelease to extract the prerelease components from the version string. The resulting prerelease variable contains an array with the two prerelease components "beta" and "1". Finally, we log the prerelease variable to the console using console.log. When you run this code, you should see an array containing the prerelease components "beta" and "1" logged to the console. This example demonstrates how you can use semver.prerelease to extract the prerelease components from a version string.

189
190
191
192
193
194
195
196
197
198
199
200
}


function parseRange(range) {
    var condition = regex.condition.exec(range)[1] || '=';
    var version = regex.version.exec(range)[1];
    var prerelease = semver.prerelease(version);
    return { condition, prerelease, version };
}


function union(a, b) {
fork icon0
star icon0
watch icon1

164
165
166
167
168
169
170
171
172
173
174
175
176


  return undefined;
}


function getPreviousRelease(version, allReleases) {
  const versionIsStable = semver.prerelease(version) === null;


  // Make sure versions are sorted before using them
  allReleases.sort((v1, v2) => semver.rcompare(v1.name, v2.name));

fork icon0
star icon0
watch icon1

15
16
17
18
19
20
21
22
23
24
 */
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;
  if (prerelease) {
    [buildType, buildVersionSummary] = prerelease;
    // The version could be version: '10.25.0-beta.1-flask.0',
fork icon0
star icon0
watch icon0

74
75
76
77
78
79
80
81
82
83
84
if (outputMode.isGitHub) {
    templatedReleaseNotes = ejs.render(fs.readFileSync('platform/macos/scripts/release-notes-github.md.ejs', 'utf8'), {
        'CURRENTVERSION': currentVersion,
        'PREVIOUSVERSION': previousVersion,
        'CHANGELOG': currentReleaseNotes.changelog,
        'isPrerelease': semver.prerelease(currentVersion)
    });
}


if (outputMode.isJazzy) {
fork icon0
star icon0
watch icon0

178
179
180
181
182
183
184
185
186
187
  acc[cur.packageJson.name] = { replace: cur.packageJson.name };
  return acc;
}

if (isConvergedPackage({ packagePathOrJson: cur.packageJson, projectType: 'library' })) {
  const isPrerelease = semver.prerelease(cur.packageJson.version) !== null;

  acc[cur.packageJson.name] = isPrerelease
    ? { replace: '@fluentui/react-components/unstable' }
    : { replace: '@fluentui/react-components' };
fork icon0
star icon0
watch icon272