Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow identifierBase to be false #548

18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -239,20 +239,38 @@ $ 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
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
$ 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
```

if 'identifierBase' is set to `false` and identifier exists next prerelease
number will be `1`
```javascript
semver.inc('1.2.3-beta', 'prerelease', 'beta', false)
// '1.2.3-beta.1'
```

### Advanced Range Syntax

Advanced range syntax desugars to primitive comparators in
Expand Down
8 changes: 8 additions & 0 deletions bin/semver.js
Expand Up @@ -76,6 +76,9 @@ const main = () => {
break
case '-n':
identifierBase = argv.shift()
if (identifierBase === 'false') {
identifierBase = false
}
break
case '-c': case '--coerce':
coerce = true
Expand Down Expand Up @@ -178,6 +181,11 @@ Options:
--ltr
Coerce version strings left to right (default)

-n <base>
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.

Expand Down
19 changes: 12 additions & 7 deletions classes/semver.js
Expand Up @@ -246,9 +246,11 @@ 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 = identifierBase === false || Number(identifierBase) ? 1 : 0

if (this.prerelease.length === 0) {
this.prerelease = [0]
this.prerelease = [base]
} else {
let i = this.prerelease.length
while (--i >= 0) {
Expand All @@ -259,23 +261,26 @@ class SemVer {
}
if (i === -1) {
// didn't increment anything
this.prerelease.push(0)
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}`)
}
Expand Down
29 changes: 29 additions & 0 deletions tap-snapshots/test/bin/semver.js.test.cjs
Expand Up @@ -93,6 +93,11 @@ Object {
--ltr
Coerce version strings left to right (default)

-n <base>
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.

Expand Down Expand Up @@ -149,6 +154,11 @@ Object {
--ltr
Coerce version strings left to right (default)

-n <base>
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.

Expand Down Expand Up @@ -205,6 +215,11 @@ Object {
--ltr
Coerce version strings left to right (default)

-n <base>
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.

Expand Down Expand Up @@ -261,6 +276,11 @@ Object {
--ltr
Coerce version strings left to right (default)

-n <base>
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.

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/bin/semver.js
Expand Up @@ -28,8 +28,8 @@ const run = args => new Promise((resolve, reject) => {
t.test('inc tests', t => Promise.all([
['-i', 'major', '1.0.0'],
['-i', 'major', '1.0.0', '1.0.1'],
['-i', 'premajor', '1.0.0', '--preid=beta'],
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
['-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(' ')))))

Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/increments.js
Expand Up @@ -101,4 +101,19 @@ module.exports = [
['1.2.3-dev.bar', 'prerelease', '1.2.3-dev.0', false, 'dev', '0'],
['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-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.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', '1.2.3-dev.1', false, 'dev', false],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nlf what do you think about this? If they have said "increase the prerelease" but also asked for there not to be a prerelease base, do we default to 1 or should we throw?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my gut reaction is we should throw because they've asked for us to do something that doesn't make sense. going from no number to adding a number when we've been asked to not add numbers feels wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, let's throw in this situation. No more guessing user intent!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to clarify do we want to throw also in:

['1.2.0', 'prerelease', '1.2.1-1', false, '', false],

?

Copy link
Member

@wraithgar wraithgar Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh good point. We sure do! "increase the prerelease with no identifier and no identifierBase" sure sounds impossible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i updated it to throw on both cases

['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],
]