Skip to content

Commit

Permalink
feat: allow identifierBase to be false (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsvalina authored Apr 14, 2023
1 parent fc2f3df commit 503a4e5
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,31 @@ $ 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
```

### Advanced Range Syntax

Advanced range syntax desugars to primitive comparators in
Expand Down
8 changes: 8 additions & 0 deletions bin/semver.js
Original file line number Diff line number Diff line change
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
26 changes: 19 additions & 7 deletions classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`)
}
Expand Down
29 changes: 29 additions & 0 deletions tap-snapshots/test/bin/semver.js.test.cjs
Original file line number Diff line number Diff line change
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
1 change: 1 addition & 0 deletions test/bin/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ')))))

Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/increments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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],
]

0 comments on commit 503a4e5

Please sign in to comment.