Skip to content

Commit

Permalink
feat: add Disable Prerelease Identifier Base for creating prerelease …
Browse files Browse the repository at this point in the history
…intentifiers without appending .0
  • Loading branch information
lsvalina committed Apr 12, 2023
1 parent 82aa7f6 commit 0c69881
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 9 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ $ semver 1.2.3 -i prerelease --preid beta -n 1
1.2.4-beta.1
```

#### Disable Prerelease Identifier Base
when this flag is set to true initial prerelease identifier base is disabled.

```javascript
semver.inc('1.2.3', 'prerelease', { disableIdentifierBase: true } , 'beta')
// '1.2.4-beta'
```
command-line example:
```bash
$ semver 1.2.3 -i prerelease --preid beta -noidbase
# 1.2.4-beta
```
if another version is created with same identifier then the prerelease
number is used based on Prerelease Identifier Base
```javascript
semver.inc('1.2.3-beta', 'prerelease', { disableIdentifierBase: true } , 'beta', '1')
// '1.2.3-beta.1'
```
### Advanced Range Syntax
Advanced range syntax desugars to primitive comparators in
Expand Down
20 changes: 19 additions & 1 deletion bin/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ let identifier

let identifierBase

let disableIdentifierBase

const semver = require('../')
const parseOptions = require('../internal/parse-options')

Expand Down Expand Up @@ -77,6 +79,9 @@ const main = () => {
case '-n':
identifierBase = argv.shift()
break
case '--noidbase':
disableIdentifierBase = true
break
case '-c': case '--coerce':
coerce = true
break
Expand All @@ -94,7 +99,7 @@ const main = () => {
}
}

options = parseOptions({ loose, includePrerelease, rtl })
options = parseOptions({ loose, includePrerelease, rtl, disableIdentifierBase })

versions = versions.map((v) => {
return coerce ? (semver.coerce(v, options) || { version: v }).version : v
Expand Down Expand Up @@ -178,6 +183,19 @@ Options:
--ltr
Coerce version strings left to right (default)
-n <base>
Prerelease Identifier Base
that will let you let your prerelease number as
zero-based or one-based.If you do not specify
this parameter, it will default to zero-based.
--noidbase
Disable Identifier Base
when this flag is set to true initial prerelease
identifier base is disabled, if another version
is created with same identifier then the prerelease
number is used based on Prerelease Identifier Base
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
Expand Down
21 changes: 15 additions & 6 deletions classes/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ 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 (this.prerelease.length === 0) {
this.prerelease = [0]
this.prerelease = [base]
} else {
let i = this.prerelease.length
while (--i >= 0) {
Expand All @@ -259,23 +260,31 @@ 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
if (compareIdentifiers(this.prerelease[0], identifier) === 0) {
if (isNaN(this.prerelease[1])) {
this.prerelease = [identifier, base]
if (this.options.disableIdentifierBase) {
this.prerelease = [identifier]
} else {
this.prerelease = [identifier, base]
}
}
} else {
this.prerelease = [identifier, base]
if (this.options.disableIdentifierBase) {
this.prerelease = [identifier]
} else {
this.prerelease = [identifier, base]
}
}
}
break

}
default:
throw new Error(`invalid increment argument: ${release}`)
}
Expand Down
61 changes: 61 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,19 @@ Object {
--ltr
Coerce version strings left to right (default)
-n <base>
Prerelease Identifier Base
that will let you let your prerelease number as
zero-based or one-based.If you do not specify
this parameter, it will default to zero-based.
--noidbase
Disable Identifier Base
when this flag is set to true initial prerelease
identifier base is disabled, if another version
is created with same identifier then the prerelease
number is used based on Prerelease Identifier Base
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
Expand Down Expand Up @@ -149,6 +162,19 @@ Object {
--ltr
Coerce version strings left to right (default)
-n <base>
Prerelease Identifier Base
that will let you let your prerelease number as
zero-based or one-based.If you do not specify
this parameter, it will default to zero-based.
--noidbase
Disable Identifier Base
when this flag is set to true initial prerelease
identifier base is disabled, if another version
is created with same identifier then the prerelease
number is used based on Prerelease Identifier Base
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
Expand Down Expand Up @@ -205,6 +231,19 @@ Object {
--ltr
Coerce version strings left to right (default)
-n <base>
Prerelease Identifier Base
that will let you let your prerelease number as
zero-based or one-based.If you do not specify
this parameter, it will default to zero-based.
--noidbase
Disable Identifier Base
when this flag is set to true initial prerelease
identifier base is disabled, if another version
is created with same identifier then the prerelease
number is used based on Prerelease Identifier Base
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
Expand Down Expand Up @@ -261,6 +300,19 @@ Object {
--ltr
Coerce version strings left to right (default)
-n <base>
Prerelease Identifier Base
that will let you let your prerelease number as
zero-based or one-based.If you do not specify
this parameter, it will default to zero-based.
--noidbase
Disable Identifier Base
when this flag is set to true initial prerelease
identifier base is disabled, if another version
is created with same identifier then the prerelease
number is used based on Prerelease Identifier Base
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
Expand Down Expand Up @@ -310,6 +362,15 @@ Object {
}
`

exports[`test/bin/semver.js TAP inc tests > -i premajor 1.0.0 --preid=beta --noidbase 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
Original file line number Diff line number Diff line change
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'],
['-i', 'premajor', '1.0.0', '--preid=beta', '-n', '1'],
['-i', 'premajor', '1.0.0', '--preid=beta', '--noidbase'],
['-i', '1.2.3'],
].map(args => t.resolveMatchSnapshot(run(args), args.join(' ')))))

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

// disableIdentifierBase
['1.2.0-1', 'prerelease', '1.2.0-alpha', { disableIdentifierBase: true }, 'alpha', '0'],
['1.2.1', 'prerelease', '1.2.2-alpha', { disableIdentifierBase: true }, 'alpha', '0'],
['1.2.2', 'prerelease', '1.2.3-alpha', { disableIdentifierBase: true }, 'alpha', '1'],
['1.2.0', 'prepatch', '1.2.1-dev', { disableIdentifierBase: true }, 'dev', '1'],
['1.2.0-1', 'prepatch', '1.2.1-dev', { disableIdentifierBase: true }, 'dev', '1'],
['1.2.0', 'premajor', '2.0.0-dev', { disableIdentifierBase: true }, 'dev', '0'],
['1.2.3-1', 'premajor', '2.0.0-dev', { disableIdentifierBase: true }, 'dev', '0'],
['1.2.3-dev.bar', 'prerelease', '1.2.3-dev', { disableIdentifierBase: true }, 'dev', '0'],
['1.2.0', 'preminor', '1.3.0-dev', { disableIdentifierBase: true }, 'dev', '1'],
['1.2.3-1', 'preminor', '1.3.0-dev', { disableIdentifierBase: true }, 'dev'],
['1.2.3-dev', 'prerelease', '1.2.3-dev.0', { disableIdentifierBase: true }, 'dev', '0'],
['1.2.3-dev', 'prerelease', '1.2.3-dev.1', { disableIdentifierBase: true }, 'dev', '1'],
['1.2.0-dev', 'premajor', '2.0.0-dev', { disableIdentifierBase: true }, 'dev', '0'],
['1.2.0-dev', 'preminor', '1.3.0-beta', { disableIdentifierBase: true }, 'beta', '1'],
['1.2.0-dev', 'prepatch', '1.2.1-dev', { disableIdentifierBase: true }, 'dev', '1'],
]
6 changes: 5 additions & 1 deletion test/internal/parse-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ t.test('any object passed is returned', t => {
loose: true,
includePrerelease: true,
})
t.strictSame(parseOptions({ rtl: true, includePrerelease: true }), {
t.strictSame(parseOptions({ disableIdentifierBase: true }), {
disableIdentifierBase: true,
})
t.strictSame(parseOptions({ rtl: true, includePrerelease: true, disableIdentifierBase: true }), {
rtl: true,
includePrerelease: true,
disableIdentifierBase: true,
})
t.end()
})

0 comments on commit 0c69881

Please sign in to comment.