How to use the clean function from semver

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

semver.clean is a function that takes a version string and returns a cleaned-up version string without any extraneous characters or information.

26
27
28
29
30
31
32
33
34
35
}

async function writeBoltVersionUrlsToJson(versionData) {
  const config = await getConfig();

  const lernaVersion = semver.clean(lernaConfig.version);

  // Filter out versions that are greater than the lerna version
  const versionInfo = versionData.filter(({ label }) =>
    semver.lte(label, lernaVersion),
fork icon44
star icon278
watch icon23

63
64
65
66
67
68
69
70
71
}

var versionRequirements = [
  {
    name: 'node',
    currentVersion: semver.clean(process.version),
    versionRequirement: packageConfig.engines.node
  },
]
fork icon15
star icon145
watch icon0

How does semver.clean work?

semver.clean is a function in the semver package for Node.js that takes a version string as input and returns a cleaned up version string, removing any non-numeric characters and ignoring any pre-release or build metadata. The resulting string should be a valid semantic version that can be used for comparison. If the input string is not a valid version string, the function returns null.

35
36
37
38
39
40
41
42
43
44
45
 * Cleans the version number so it can be compared
 * @param {string} version - Version number of a dependency from a package.json file
 *
 * @returns {string} - Cleaned Version number
 */
const cleanVersion = ver => semver.clean(ver.replace('^', ''))


/**
 * Gets meta-data about the taps @keg-hub versions
 * @param {Object} tapPackage - The taps package.json file
fork icon1
star icon0
watch icon0

306
307
308
309
310
311
312
313
314
315
// }, fix_packageversion);

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);
fork icon0
star icon2
watch icon2

+ 39 other calls in file

Ai Example

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

const dirtyVersion = "v1.2.3-beta+build.123";
const cleanVersion = semver.clean(dirtyVersion);

console.log(cleanVersion); // output: 1.2.3-beta

In this example, semver.clean is used to remove the pre-release and build metadata from a dirty version string. The function returns a cleaned up version string that can be used for comparison and other purposes.

187
188
189
190
191
192
193
194
195
196
197
      return true
    }
    if (!semver.valid(data.version, loose)) {
      throw new Error('Invalid version: "'+ data.version + '"')
    }
    data.version = semver.clean(data.version, loose)
    return true
  }


, fixPeople: function(data) {
fork icon0
star icon0
watch icon1

33
34
35
36
37
38
39
40
41
42
43
44
): Promise<void> {
  const files = await fs.readdir(moduleLoc);


  // clean info.version
  if (typeof info.version === 'string') {
    info.version = semver.clean(info.version, looseSemver) || info.version;
  }


  // if name or version aren't set then set them to empty strings
  info.name = info.name || '';
fork icon0
star icon0
watch icon1

106
107
108
109
110
111
112
113
114
  }
}

// similarly, if a specific version, then only that version will do
if (wanted && type === 'version') {
  const ver = semver.clean(wanted, { loose: true })
  const mani = versions[ver] || staged[ver] || restricted[ver]
  return isBefore(verTimes, ver, time) ? decorateAvoid(mani, avoid) : null
}
fork icon0
star icon0
watch icon0

28
29
30
31
32
33
34
35
36
37
38
let gitTags = execSync('git tag --list macos-v*.*.*')
    .toString()
    .split('\n')
    .map(function (tag) {
        tag = tag.replace('macos-v', '').trim();
        return semver.clean(tag);
    });
let previousVersion = semver.maxSatisfying(gitTags, "<" + currentVersion);


/*
fork icon0
star icon0
watch icon0

3
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}`;
fork icon0
star icon0
watch icon0