semver 7.7.3 → 7.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -56,6 +56,7 @@ const semverCompareLoose = require('semver/functions/compare-loose')
56
56
  const semverCompareBuild = require('semver/functions/compare-build')
57
57
  const semverSort = require('semver/functions/sort')
58
58
  const semverRsort = require('semver/functions/rsort')
59
+ const semverTruncate = require('semver/functions/truncate')
59
60
 
60
61
  // low-level comparators between versions
61
62
  const semverGt = require('semver/functions/gt')
@@ -110,8 +111,9 @@ Options:
110
111
  -l --loose
111
112
  Interpret versions and ranges loosely
112
113
 
113
- -n <0|1>
114
- This is the base to be used for the prerelease identifier.
114
+ -n <0|1|false>
115
+ Base number for prerelease identifier (default: 0).
116
+ Use false to omit the number altogether.
115
117
 
116
118
  -p --include-prerelease
117
119
  Always include prerelease versions in range matching
@@ -398,12 +400,19 @@ nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
398
400
  tilde ::= '~' partial
399
401
  caret ::= '^' partial
400
402
  qualifier ::= ( '-' pre )? ( '+' build )?
401
- pre ::= parts
402
- build ::= parts
403
- parts ::= part ( '.' part ) *
404
- part ::= nr | [-0-9A-Za-z]+
403
+ pre ::= prepart ( '.' prepart ) *
404
+ prepart ::= nr | alphanumid
405
+ build ::= buildid ( '.' buildid ) *
406
+ alphanumid ::= ( ['0'-'9'] ) * [-A-Za-z] [-0-9A-Za-z] *
407
+ buildid ::= [-0-9A-Za-z]+
405
408
  ```
406
409
 
410
+ Note: Prerelease identifiers (`pre`) use `nr` for numeric parts, which
411
+ disallows leading zeros (e.g., `1.2.3-00` is invalid). Build metadata
412
+ identifiers (`build`) allow any alphanumeric string including leading
413
+ zeros (e.g., `1.2.3+00` is valid). This matches the
414
+ [SemVer 2.0.0 specification](https://semver.org/#spec-item-9).
415
+
407
416
  ## Functions
408
417
 
409
418
  All methods and classes take a final `options` object argument. All
@@ -448,6 +457,12 @@ strings that they parse.
448
457
  or comparators intersect.
449
458
  * `parse(v)`: Attempt to parse a string as a semantic version, returning either
450
459
  a `SemVer` object or `null`.
460
+ * `truncate(v, releaseType)`: Return the version with components _lower_
461
+ than `releaseType` dropped off, e.g.:
462
+ * `major` removes build & prerelease info and sets minor & patch to 0.
463
+ * `minor` removes build & prerelease info, and sets patch to 0
464
+ * `patch` removes build & prerelease info
465
+ * All prerelease types remove build info only
451
466
 
452
467
  ### Comparison
453
468
 
@@ -649,6 +664,7 @@ The following modules are available:
649
664
  * `require('semver/functions/rsort')`
650
665
  * `require('semver/functions/satisfies')`
651
666
  * `require('semver/functions/sort')`
667
+ * `require('semver/functions/truncate')`
652
668
  * `require('semver/functions/valid')`
653
669
  * `require('semver/ranges/gtr')`
654
670
  * `require('semver/ranges/intersects')`
package/bin/semver.js CHANGED
@@ -46,6 +46,7 @@ const main = () => {
46
46
  a = a.slice(0, indexOfEqualSign)
47
47
  argv.unshift(value)
48
48
  }
49
+
49
50
  switch (a) {
50
51
  case '-rv': case '-rev': case '--rev': case '--reverse':
51
52
  reverse = true
@@ -60,15 +61,10 @@ const main = () => {
60
61
  versions.push(argv.shift())
61
62
  break
62
63
  case '-i': case '--inc': case '--increment':
63
- switch (argv[0]) {
64
- case 'major': case 'minor': case 'patch': case 'prerelease':
65
- case 'premajor': case 'preminor': case 'prepatch':
66
- case 'release':
67
- inc = argv.shift()
68
- break
69
- default:
70
- inc = 'patch'
71
- break
64
+ if (semver.RELEASE_TYPES.includes(argv[0]) || (argv[0] === 'release')) {
65
+ inc = { value: argv.shift(), maybeErrantValue: null, option: a }
66
+ } else {
67
+ inc = { value: 'patch', maybeErrantValue: argv[0], option: a }
72
68
  }
73
69
  break
74
70
  case '--preid':
@@ -102,10 +98,18 @@ const main = () => {
102
98
 
103
99
  options = parseOptions({ loose, includePrerelease, rtl })
104
100
 
101
+ if (
102
+ inc &&
103
+ versions.includes(inc.maybeErrantValue) &&
104
+ !semver.valid(inc.maybeErrantValue, options)
105
+ ) {
106
+ console.warn(`Invalid value for ${inc.option}; defaulting to 'patch'. This may become a failure in future major versions.`)
107
+ }
108
+
105
109
  versions = versions.map((v) => {
106
110
  return coerce ? (semver.coerce(v, options) || { version: v }).version : v
107
111
  }).filter((v) => {
108
- return semver.valid(v)
112
+ return semver.valid(v, options)
109
113
  })
110
114
  if (!versions.length) {
111
115
  return fail()
@@ -125,7 +129,7 @@ const main = () => {
125
129
  versions
126
130
  .sort((a, b) => semver[reverse ? 'rcompare' : 'compare'](a, b, options))
127
131
  .map(v => semver.clean(v, options))
128
- .map(v => inc ? semver.inc(v, inc, options, identifier, identifierBase) : v)
132
+ .map(v => inc ? semver.inc(v, inc.value, options, identifier, identifierBase) : v)
129
133
  .forEach(v => console.log(v))
130
134
  }
131
135
 
package/functions/diff.js CHANGED
@@ -53,7 +53,7 @@ const diff = (version1, version2) => {
53
53
  return prefix + 'patch'
54
54
  }
55
55
 
56
- // high and low are preleases
56
+ // high and low are prereleases
57
57
  return 'prerelease'
58
58
  }
59
59
 
@@ -0,0 +1,48 @@
1
+ 'use strict'
2
+
3
+ const parse = require('./parse')
4
+ const constants = require('../internal/constants')
5
+ const SemVer = require('../classes/semver')
6
+
7
+ const truncate = (version, truncation, options) => {
8
+ if (!constants.RELEASE_TYPES.includes(truncation)) {
9
+ return null
10
+ }
11
+
12
+ const clonedVersion = cloneInputVersion(version, options)
13
+ return clonedVersion && doTruncation(clonedVersion, truncation)
14
+ }
15
+
16
+ const cloneInputVersion = (version, options) => {
17
+ const versionStringToParse = (
18
+ version instanceof SemVer ? version.version : version
19
+ )
20
+
21
+ return parse(versionStringToParse, options)
22
+ }
23
+
24
+ const doTruncation = (version, truncation) => {
25
+ if (isPrerelease(truncation)) {
26
+ return version.version
27
+ }
28
+
29
+ version.prerelease = []
30
+
31
+ switch (truncation) {
32
+ case 'major':
33
+ version.minor = 0
34
+ version.patch = 0
35
+ break
36
+ case 'minor':
37
+ version.patch = 0
38
+ break
39
+ }
40
+
41
+ return version.format()
42
+ }
43
+
44
+ const isPrerelease = (type) => {
45
+ return type.startsWith('pre')
46
+ }
47
+
48
+ module.exports = truncate
package/index.js CHANGED
@@ -28,6 +28,7 @@ const gte = require('./functions/gte')
28
28
  const lte = require('./functions/lte')
29
29
  const cmp = require('./functions/cmp')
30
30
  const coerce = require('./functions/coerce')
31
+ const truncate = require('./functions/truncate')
31
32
  const Comparator = require('./classes/comparator')
32
33
  const Range = require('./classes/range')
33
34
  const satisfies = require('./functions/satisfies')
@@ -66,6 +67,7 @@ module.exports = {
66
67
  lte,
67
68
  cmp,
68
69
  coerce,
70
+ truncate,
69
71
  Comparator,
70
72
  Range,
71
73
  satisfies,
package/internal/re.js CHANGED
@@ -78,8 +78,8 @@ createToken('MAINVERSIONLOOSE', `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
78
78
 
79
79
  // ## Pre-release Version Identifier
80
80
  // A numeric identifier, or a non-numeric identifier.
81
- // Non-numberic identifiers include numberic identifiers but can be longer.
82
- // Therefore non-numberic identifiers must go first.
81
+ // Non-numeric identifiers include numeric identifiers but can be longer.
82
+ // Therefore non-numeric identifiers must go first.
83
83
 
84
84
  createToken('PRERELEASEIDENTIFIER', `(?:${src[t.NONNUMERICIDENTIFIER]
85
85
  }|${src[t.NUMERICIDENTIFIER]})`)
@@ -136,7 +136,7 @@ createToken('LOOSE', `^${src[t.LOOSEPLAIN]}$`)
136
136
  createToken('GTLT', '((?:<|>)?=?)')
137
137
 
138
138
  // Something like "2.*" or "1.2.x".
139
- // Note that "x.x" is a valid xRange identifer, meaning "any version"
139
+ // Note that "x.x" is a valid xRange identifier, meaning "any version"
140
140
  // Only the first item is strictly required.
141
141
  createToken('XRANGEIDENTIFIERLOOSE', `${src[t.NUMERICIDENTIFIERLOOSE]}|x|X|\\*`)
142
142
  createToken('XRANGEIDENTIFIER', `${src[t.NUMERICIDENTIFIER]}|x|X|\\*`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "semver",
3
- "version": "7.7.3",
3
+ "version": "7.8.0",
4
4
  "description": "The semantic version parser used by npm.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,8 +14,8 @@
14
14
  "eslint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\""
15
15
  },
16
16
  "devDependencies": {
17
- "@npmcli/eslint-config": "^5.0.0",
18
- "@npmcli/template-oss": "4.25.1",
17
+ "@npmcli/eslint-config": "^6.0.0",
18
+ "@npmcli/template-oss": "5.0.0",
19
19
  "benchmark": "^2.1.4",
20
20
  "tap": "^16.0.0"
21
21
  },
@@ -52,7 +52,7 @@
52
52
  "author": "GitHub Inc.",
53
53
  "templateOSS": {
54
54
  "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
55
- "version": "4.25.1",
55
+ "version": "5.0.0",
56
56
  "engines": ">=10",
57
57
  "distPaths": [
58
58
  "classes/",
package/range.bnf CHANGED
@@ -10,7 +10,8 @@ nr ::= '0' | [1-9] ( [0-9] ) *
10
10
  tilde ::= '~' partial
11
11
  caret ::= '^' partial
12
12
  qualifier ::= ( '-' pre )? ( '+' build )?
13
- pre ::= parts
14
- build ::= parts
15
- parts ::= part ( '.' part ) *
16
- part ::= nr | [-0-9A-Za-z]+
13
+ pre ::= prepart ( '.' prepart ) *
14
+ prepart ::= nr | alphanumid
15
+ build ::= buildid ( '.' buildid ) *
16
+ alphanumid ::= ( [0-9] ) * [A-Za-z-] [-0-9A-Za-z] *
17
+ buildid ::= [-0-9A-Za-z]+
package/ranges/subset.js CHANGED
@@ -38,7 +38,7 @@ const compare = require('../functions/compare.js')
38
38
  // - If LT
39
39
  // - If LT.semver is greater than any < or <= comp in C, return false
40
40
  // - If LT is <=, and LT.semver does not satisfy every C, return false
41
- // - If GT.semver has a prerelease, and not in prerelease mode
41
+ // - If LT.semver has a prerelease, and not in prerelease mode
42
42
  // - If no C has a prerelease and the LT.semver tuple, return false
43
43
  // - Else return true
44
44