-
Notifications
You must be signed in to change notification settings - Fork 52
/
semver.js
40 lines (33 loc) · 1.26 KB
/
semver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import semver from 'semver'
// Compare two versions by their major versions.
// Takes into account the special rules for `0.*.*` and `0.0.*` versions.
export const isPreviousMajor = function (versionA, versionB) {
return semver.lt(getMajor(versionA), getMajor(versionB))
}
// Remove minor/patch numbers
const getMajor = function (version) {
return semver.minVersion(getMajorVersion(version)).version
}
// According to semver, the second number is the major release number for
// `0.*.*` versions and the third for `0.0.*`. This is how `^` behaves with the
// `semver` module which is used by `npm`.
export const getMajorVersion = function (version) {
if (!version || semver.clean(version) === null) {
return
}
const majorVersion = semver.major(version)
if (majorVersion !== 0) {
return `${majorVersion}`
}
const minorVersion = semver.minor(version)
if (minorVersion !== 0) {
return `${majorVersion}.${minorVersion}`
}
const patchVersion = semver.patch(version)
return `${majorVersion}.${minorVersion}.${patchVersion}`
}
export const isPrerelease = function (version) {
// Prerelease versions are excluded from ranges unless `includePrerelease` is
// set to `true`.
return !semver.satisfies(version, '>=0.0.0', { includePrerelease: false })
}