diff --git a/README.md b/README.md
index 5b5c2ede..b52a5eb1 100644
--- a/README.md
+++ b/README.md
@@ -239,6 +239,7 @@ $ semver 1.2.4-beta.0 -i prerelease
The method `.inc` takes an optional parameter 'identifierBase' string
that will let you let your prerelease number as zero-based or one-based.
+Set to `false` to omit the prerelease number altogether.
If you do not specify this parameter, it will default to zero-based.
```javascript
@@ -246,6 +247,11 @@ semver.inc('1.2.3', 'prerelease', 'beta', '1')
// '1.2.4-beta.1'
```
+```javascript
+semver.inc('1.2.3', 'prerelease', 'beta', false)
+// '1.2.4-beta'
+```
+
command-line example:
```bash
@@ -253,6 +259,11 @@ $ semver 1.2.3 -i prerelease --preid beta -n 1
1.2.4-beta.1
```
+```bash
+$ semver 1.2.3 -i prerelease --preid beta -n false
+1.2.4-beta
+```
+
### Advanced Range Syntax
Advanced range syntax desugars to primitive comparators in
diff --git a/bin/semver.js b/bin/semver.js
index d71b1bf3..242b7ade 100755
--- a/bin/semver.js
+++ b/bin/semver.js
@@ -76,6 +76,9 @@ const main = () => {
break
case '-n':
identifierBase = argv.shift()
+ if (identifierBase === 'false') {
+ identifierBase = false
+ }
break
case '-c': case '--coerce':
coerce = true
@@ -178,6 +181,11 @@ Options:
--ltr
Coerce version strings left to right (default)
+-n
+ Base number to be used for the prerelease identifier.
+ Can be either 0 or 1, or false to omit the number altogether.
+ Defaults to 0.
+
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
diff --git a/classes/semver.js b/classes/semver.js
index 20eb0897..ea62fa0d 100644
--- a/classes/semver.js
+++ b/classes/semver.js
@@ -246,9 +246,15 @@ class SemVer {
break
// This probably shouldn't be used publicly.
// 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction.
- case 'pre':
+ case 'pre': {
+ const base = Number(identifierBase) ? 1 : 0
+
+ if (!identifier && identifierBase === false) {
+ throw new Error('invalid increment argument: identifier is empty')
+ }
+
if (this.prerelease.length === 0) {
- this.prerelease = [0]
+ this.prerelease = [base]
} else {
let i = this.prerelease.length
while (--i >= 0) {
@@ -259,23 +265,29 @@ class SemVer {
}
if (i === -1) {
// didn't increment anything
- this.prerelease.push(0)
+ if (identifier === this.prerelease.join('.') && identifierBase === false) {
+ throw new Error('invalid increment argument: identifier already exists')
+ }
+ this.prerelease.push(base)
}
}
if (identifier) {
- const base = Number(identifierBase) ? 1 : 0
// 1.2.0-beta.1 bumps to 1.2.0-beta.2,
// 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
+ let prerelease = [identifier, base]
+ if (identifierBase === false) {
+ prerelease = [identifier]
+ }
if (compareIdentifiers(this.prerelease[0], identifier) === 0) {
if (isNaN(this.prerelease[1])) {
- this.prerelease = [identifier, base]
+ this.prerelease = prerelease
}
} else {
- this.prerelease = [identifier, base]
+ this.prerelease = prerelease
}
}
break
-
+ }
default:
throw new Error(`invalid increment argument: ${release}`)
}
diff --git a/tap-snapshots/test/bin/semver.js.test.cjs b/tap-snapshots/test/bin/semver.js.test.cjs
index 64814ef6..e820ca47 100644
--- a/tap-snapshots/test/bin/semver.js.test.cjs
+++ b/tap-snapshots/test/bin/semver.js.test.cjs
@@ -93,6 +93,11 @@ Object {
--ltr
Coerce version strings left to right (default)
+ -n
+ Base number to be used for the prerelease identifier.
+ Can be either 0 or 1, or false to omit the number altogether.
+ Defaults to 0.
+
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
@@ -149,6 +154,11 @@ Object {
--ltr
Coerce version strings left to right (default)
+ -n
+ Base number to be used for the prerelease identifier.
+ Can be either 0 or 1, or false to omit the number altogether.
+ Defaults to 0.
+
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
@@ -205,6 +215,11 @@ Object {
--ltr
Coerce version strings left to right (default)
+ -n
+ Base number to be used for the prerelease identifier.
+ Can be either 0 or 1, or false to omit the number altogether.
+ Defaults to 0.
+
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
@@ -261,6 +276,11 @@ Object {
--ltr
Coerce version strings left to right (default)
+ -n
+ Base number to be used for the prerelease identifier.
+ Can be either 0 or 1, or false to omit the number altogether.
+ Defaults to 0.
+
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
@@ -310,6 +330,15 @@ Object {
}
`
+exports[`test/bin/semver.js TAP inc tests > -i premajor 1.0.0 --preid=beta -n false 1`] = `
+Object {
+ "code": 0,
+ "err": "",
+ "out": "2.0.0-beta\\n",
+ "signal": null,
+}
+`
+
exports[`test/bin/semver.js TAP inc tests > -i premajor 1.0.0 --preid=beta 1`] = `
Object {
"code": 0,
diff --git a/test/bin/semver.js b/test/bin/semver.js
index 0023872d..262ca380 100644
--- a/test/bin/semver.js
+++ b/test/bin/semver.js
@@ -30,6 +30,7 @@ t.test('inc tests', t => Promise.all([
['-i', 'major', '1.0.0', '1.0.1'],
['-i', 'premajor', '1.0.0', '--preid=beta'],
['-i', 'premajor', '1.0.0', '--preid=beta', '-n', '1'],
+ ['-i', 'premajor', '1.0.0', '--preid=beta', '-n', 'false'],
['-i', '1.2.3'],
].map(args => t.resolveMatchSnapshot(run(args), args.join(' ')))))
diff --git a/test/fixtures/increments.js b/test/fixtures/increments.js
index dedd02e6..1fbc4bc9 100644
--- a/test/fixtures/increments.js
+++ b/test/fixtures/increments.js
@@ -79,6 +79,7 @@ module.exports = [
['1.2.3-1', 'preminor', '1.3.0-dev.0', false, 'dev'],
['1.2.0', 'premajor', '2.0.0-dev.0', false, 'dev'],
['1.2.3-1', 'premajor', '2.0.0-dev.0', false, 'dev'],
+ ['1.2.3-1', 'premajor', '2.0.0-dev.1', false, 'dev', 1],
['1.2.0-1', 'minor', '1.2.0', false, 'dev'],
['1.0.0-1', 'major', '1.0.0', 'dev'],
['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.0', false, 'dev'],
@@ -99,6 +100,27 @@ module.exports = [
['1.2.0', 'premajor', '2.0.0-dev.0', false, 'dev', '0'],
['1.2.3-1', 'premajor', '2.0.0-dev.0', false, 'dev', '0'],
['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.0', false, 'dev', '0'],
+ ['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.1', false, 'dev', '1'],
+ ['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.bar.0', false, '', '0'],
+ ['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.bar.1', false, '', '1'],
['1.2.0', 'preminor', '1.3.0-dev.1', false, 'dev', '1'],
['1.2.3-1', 'preminor', '1.3.0-dev.0', false, 'dev'],
+ ['1.2.0', 'prerelease', '1.2.1-1', false, '', '1'],
+
+ ['1.2.0-1', 'prerelease', '1.2.0-alpha', false, 'alpha', false],
+ ['1.2.1', 'prerelease', '1.2.2-alpha', false, 'alpha', false],
+ ['1.2.2', 'prerelease', '1.2.3-alpha', false, 'alpha', false],
+ ['1.2.0', 'prepatch', '1.2.1-dev', false, 'dev', false],
+ ['1.2.0-1', 'prepatch', '1.2.1-dev', false, 'dev', false],
+ ['1.2.0', 'premajor', '2.0.0-dev', false, 'dev', false],
+ ['1.2.3-1', 'premajor', '2.0.0-dev', false, 'dev', false],
+ ['1.2.3-dev.bar', 'prerelease', '1.2.3-dev', false, 'dev', false],
+ ['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.baz', false, 'dev.baz', false],
+ ['1.2.0', 'preminor', '1.3.0-dev', false, 'dev', false],
+ ['1.2.3-1', 'preminor', '1.3.0-dev', false, 'dev', false],
+ ['1.2.3-dev', 'prerelease', null, false, 'dev', false],
+ ['1.2.0-dev', 'premajor', '2.0.0-dev', false, 'dev', false],
+ ['1.2.0-dev', 'preminor', '1.3.0-beta', false, 'beta', false],
+ ['1.2.0-dev', 'prepatch', '1.2.1-dev', false, 'dev', false],
+ ['1.2.0', 'prerelease', null, false, '', false],
]