From 0c69881df3536ac934ad49bcbd7633c1f0f527b6 Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Wed, 12 Apr 2023 16:00:20 +0200 Subject: [PATCH 1/8] feat: add Disable Prerelease Identifier Base for creating prerelease intentifiers without appending .0 --- README.md | 23 +++++++++ bin/semver.js | 20 +++++++- classes/semver.js | 21 +++++--- tap-snapshots/test/bin/semver.js.test.cjs | 61 +++++++++++++++++++++++ test/bin/semver.js | 2 +- test/fixtures/increments.js | 17 +++++++ test/internal/parse-options.js | 6 ++- 7 files changed, 141 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5b5c2ede..6cd2bee2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/semver.js b/bin/semver.js index d71b1bf3..4e2514b0 100755 --- a/bin/semver.js +++ b/bin/semver.js @@ -25,6 +25,8 @@ let identifier let identifierBase +let disableIdentifierBase + const semver = require('../') const parseOptions = require('../internal/parse-options') @@ -77,6 +79,9 @@ const main = () => { case '-n': identifierBase = argv.shift() break + case '--noidbase': + disableIdentifierBase = true + break case '-c': case '--coerce': coerce = true break @@ -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 @@ -178,6 +183,19 @@ Options: --ltr Coerce version strings left to right (default) +-n + 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. diff --git a/classes/semver.js b/classes/semver.js index 20eb0897..526c4edc 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -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) { @@ -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}`) } diff --git a/tap-snapshots/test/bin/semver.js.test.cjs b/tap-snapshots/test/bin/semver.js.test.cjs index 64814ef6..870b9fc6 100644 --- a/tap-snapshots/test/bin/semver.js.test.cjs +++ b/tap-snapshots/test/bin/semver.js.test.cjs @@ -93,6 +93,19 @@ Object { --ltr Coerce version strings left to right (default) + -n + 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. @@ -149,6 +162,19 @@ Object { --ltr Coerce version strings left to right (default) + -n + 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. @@ -205,6 +231,19 @@ Object { --ltr Coerce version strings left to right (default) + -n + 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. @@ -261,6 +300,19 @@ Object { --ltr Coerce version strings left to right (default) + -n + 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. @@ -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, diff --git a/test/bin/semver.js b/test/bin/semver.js index 0023872d..bd6d5991 100644 --- a/test/bin/semver.js +++ b/test/bin/semver.js @@ -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(' '))))) diff --git a/test/fixtures/increments.js b/test/fixtures/increments.js index dedd02e6..c7b1e7ba 100644 --- a/test/fixtures/increments.js +++ b/test/fixtures/increments.js @@ -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'], ] diff --git a/test/internal/parse-options.js b/test/internal/parse-options.js index 2400537d..304aa03d 100644 --- a/test/internal/parse-options.js +++ b/test/internal/parse-options.js @@ -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() }) From 14a3913d1367b015d05b0c58a88a44894b6d0ba5 Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Thu, 13 Apr 2023 11:36:24 +0200 Subject: [PATCH 2/8] feat: removed Disable Prerelease Identifier Base, now using false in Prerelease Identifier Base parameter --- README.md | 19 +++++----- bin/semver.js | 17 ++------- classes/semver.js | 22 ++++++------ tap-snapshots/test/bin/semver.js.test.cjs | 42 +++++------------------ test/bin/semver.js | 2 +- test/fixtures/increments.js | 30 ++++++++-------- test/internal/parse-options.js | 6 +--- 7 files changed, 46 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 6cd2bee2..60142afb 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. +If you provide false zero will be omitted. 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,18 +259,9 @@ $ 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 +$ semver 1.2.3 -i prerelease --preid beta -n false +1.2.4-beta ``` diff --git a/bin/semver.js b/bin/semver.js index 4e2514b0..1f6657f2 100755 --- a/bin/semver.js +++ b/bin/semver.js @@ -25,8 +25,6 @@ let identifier let identifierBase -let disableIdentifierBase - const semver = require('../') const parseOptions = require('../internal/parse-options') @@ -79,9 +77,6 @@ const main = () => { case '-n': identifierBase = argv.shift() break - case '--noidbase': - disableIdentifierBase = true - break case '-c': case '--coerce': coerce = true break @@ -99,7 +94,7 @@ const main = () => { } } - options = parseOptions({ loose, includePrerelease, rtl, disableIdentifierBase }) + options = parseOptions({ loose, includePrerelease, rtl }) versions = versions.map((v) => { return coerce ? (semver.coerce(v, options) || { version: v }).version : v @@ -186,15 +181,9 @@ Options: -n Prerelease Identifier Base that will let you let your prerelease number as - zero-based or one-based.If you do not specify + zero-based or one-based. If you provide false + zero will be omitted. 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. diff --git a/classes/semver.js b/classes/semver.js index 526c4edc..446baca3 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -247,7 +247,10 @@ class SemVer { // This probably shouldn't be used publicly. // 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction. case 'pre': { - const base = Number(identifierBase) ? 1 : 0 + const base = identifierBase === false || + identifierBase === 'false' || + Number(identifierBase) ? 1 : 0 + if (this.prerelease.length === 0) { this.prerelease = [base] } else { @@ -266,21 +269,16 @@ class SemVer { if (identifier) { // 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 || identifierBase === 'false') { + prerelease = [identifier] + } if (compareIdentifiers(this.prerelease[0], identifier) === 0) { if (isNaN(this.prerelease[1])) { - if (this.options.disableIdentifierBase) { - this.prerelease = [identifier] - } else { - this.prerelease = [identifier, base] - } + this.prerelease = prerelease } } else { - this.prerelease = [identifier, base] - if (this.options.disableIdentifierBase) { - this.prerelease = [identifier] - } else { - this.prerelease = [identifier, base] - } + this.prerelease = prerelease } } break diff --git a/tap-snapshots/test/bin/semver.js.test.cjs b/tap-snapshots/test/bin/semver.js.test.cjs index 870b9fc6..b8965e2f 100644 --- a/tap-snapshots/test/bin/semver.js.test.cjs +++ b/tap-snapshots/test/bin/semver.js.test.cjs @@ -96,15 +96,9 @@ Object { -n Prerelease Identifier Base that will let you let your prerelease number as - zero-based or one-based.If you do not specify + zero-based or one-based. If you provide false + zero will be omitted. 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. @@ -165,15 +159,9 @@ Object { -n Prerelease Identifier Base that will let you let your prerelease number as - zero-based or one-based.If you do not specify + zero-based or one-based. If you provide false + zero will be omitted. 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. @@ -234,15 +222,9 @@ Object { -n Prerelease Identifier Base that will let you let your prerelease number as - zero-based or one-based.If you do not specify + zero-based or one-based. If you provide false + zero will be omitted. 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. @@ -303,15 +285,9 @@ Object { -n Prerelease Identifier Base that will let you let your prerelease number as - zero-based or one-based.If you do not specify + zero-based or one-based. If you provide false + zero will be omitted. 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. @@ -362,7 +338,7 @@ Object { } ` -exports[`test/bin/semver.js TAP inc tests > -i premajor 1.0.0 --preid=beta --noidbase 1`] = ` +exports[`test/bin/semver.js TAP inc tests > -i premajor 1.0.0 --preid=beta -n false 1`] = ` Object { "code": 0, "err": "", diff --git a/test/bin/semver.js b/test/bin/semver.js index bd6d5991..12fcbbe0 100644 --- a/test/bin/semver.js +++ b/test/bin/semver.js @@ -29,7 +29,7 @@ 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', '-n', '1'], - ['-i', 'premajor', '1.0.0', '--preid=beta', '--noidbase'], + ['-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 c7b1e7ba..cb99b4e5 100644 --- a/test/fixtures/increments.js +++ b/test/fixtures/increments.js @@ -102,20 +102,18 @@ module.exports = [ ['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'], + ['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], + ['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], ] diff --git a/test/internal/parse-options.js b/test/internal/parse-options.js index 304aa03d..2400537d 100644 --- a/test/internal/parse-options.js +++ b/test/internal/parse-options.js @@ -35,13 +35,9 @@ t.test('any object passed is returned', t => { loose: true, includePrerelease: true, }) - t.strictSame(parseOptions({ disableIdentifierBase: true }), { - disableIdentifierBase: true, - }) - t.strictSame(parseOptions({ rtl: true, includePrerelease: true, disableIdentifierBase: true }), { + t.strictSame(parseOptions({ rtl: true, includePrerelease: true }), { rtl: true, includePrerelease: true, - disableIdentifierBase: true, }) t.end() }) From 871dc9dad30cf316771517655d24159a9bd3d846 Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Thu, 13 Apr 2023 17:33:44 +0200 Subject: [PATCH 3/8] feat: updated readme, cli help, and refactor string to boolean in case of false --- README.md | 10 +++---- bin/semver.js | 8 +++--- classes/semver.js | 5 ++-- tap-snapshots/test/bin/semver.js.test.cjs | 32 +++++++++-------------- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 60142afb..317c8655 100644 --- a/README.md +++ b/README.md @@ -239,7 +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. -If you provide false zero will be omitted. +Set to `false` to omit the prerelease number altogether. If you do not specify this parameter, it will default to zero-based. ```javascript @@ -264,12 +264,10 @@ $ semver 1.2.3 -i prerelease --preid beta -n false 1.2.4-beta ``` - -if another version is created with same identifier then the prerelease -number is used based on Prerelease Identifier Base - +if 'identifierBase' is set to `false` and identifier exists next prerelease +number will be `1` ```javascript -semver.inc('1.2.3-beta', 'prerelease', { disableIdentifierBase: true } , 'beta', '1') +semver.inc('1.2.3-beta', 'prerelease', 'beta', false) // '1.2.3-beta.1' ``` diff --git a/bin/semver.js b/bin/semver.js index 1f6657f2..36c3b325 100755 --- a/bin/semver.js +++ b/bin/semver.js @@ -179,11 +179,9 @@ Options: Coerce version strings left to right (default) -n - Prerelease Identifier Base - that will let you let your prerelease number as - zero-based or one-based. If you provide false - zero will be omitted. If you do not specify - this parameter, it will default to zero-based. + Base number to be used for the prerelease identifier. + Can be either 0or 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 446baca3..792a52c0 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -247,9 +247,8 @@ class SemVer { // This probably shouldn't be used publicly. // 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction. case 'pre': { - const base = identifierBase === false || - identifierBase === 'false' || - Number(identifierBase) ? 1 : 0 + identifierBase = identifierBase === 'false' ? false : identifierBase + const base = identifierBase === false || Number(identifierBase) ? 1 : 0 if (this.prerelease.length === 0) { this.prerelease = [base] diff --git a/tap-snapshots/test/bin/semver.js.test.cjs b/tap-snapshots/test/bin/semver.js.test.cjs index b8965e2f..3bac35ad 100644 --- a/tap-snapshots/test/bin/semver.js.test.cjs +++ b/tap-snapshots/test/bin/semver.js.test.cjs @@ -94,11 +94,9 @@ Object { Coerce version strings left to right (default) -n - Prerelease Identifier Base - that will let you let your prerelease number as - zero-based or one-based. If you provide false - zero will be omitted. If you do not specify - this parameter, it will default to zero-based. + Base number to be used for the prerelease identifier. + Can be either 0or 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. @@ -157,11 +155,9 @@ Object { Coerce version strings left to right (default) -n - Prerelease Identifier Base - that will let you let your prerelease number as - zero-based or one-based. If you provide false - zero will be omitted. If you do not specify - this parameter, it will default to zero-based. + Base number to be used for the prerelease identifier. + Can be either 0or 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. @@ -220,11 +216,9 @@ Object { Coerce version strings left to right (default) -n - Prerelease Identifier Base - that will let you let your prerelease number as - zero-based or one-based. If you provide false - zero will be omitted. If you do not specify - this parameter, it will default to zero-based. + Base number to be used for the prerelease identifier. + Can be either 0or 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. @@ -283,11 +277,9 @@ Object { Coerce version strings left to right (default) -n - Prerelease Identifier Base - that will let you let your prerelease number as - zero-based or one-based. If you provide false - zero will be omitted. If you do not specify - this parameter, it will default to zero-based. + Base number to be used for the prerelease identifier. + Can be either 0or 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. From 570f32c273bd057b5bdbd33a50e7359cebacfd5c Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Thu, 13 Apr 2023 17:45:22 +0200 Subject: [PATCH 4/8] feat: updated cli help, moved string to boolen to cli --- bin/semver.js | 5 ++++- classes/semver.js | 1 - tap-snapshots/test/bin/semver.js.test.cjs | 8 ++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bin/semver.js b/bin/semver.js index 36c3b325..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 @@ -180,7 +183,7 @@ Options: -n Base number to be used for the prerelease identifier. - Can be either 0or 1, or false to omit the number altogether. + Can be either 0 or 1, or false to omit the number altogether. Defaults to 0. Program exits successfully if any valid version satisfies diff --git a/classes/semver.js b/classes/semver.js index 792a52c0..910807b6 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -247,7 +247,6 @@ class SemVer { // This probably shouldn't be used publicly. // 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction. case 'pre': { - identifierBase = identifierBase === 'false' ? false : identifierBase const base = identifierBase === false || Number(identifierBase) ? 1 : 0 if (this.prerelease.length === 0) { diff --git a/tap-snapshots/test/bin/semver.js.test.cjs b/tap-snapshots/test/bin/semver.js.test.cjs index 3bac35ad..e820ca47 100644 --- a/tap-snapshots/test/bin/semver.js.test.cjs +++ b/tap-snapshots/test/bin/semver.js.test.cjs @@ -95,7 +95,7 @@ Object { -n Base number to be used for the prerelease identifier. - Can be either 0or 1, or false to omit the number altogether. + Can be either 0 or 1, or false to omit the number altogether. Defaults to 0. Program exits successfully if any valid version satisfies @@ -156,7 +156,7 @@ Object { -n Base number to be used for the prerelease identifier. - Can be either 0or 1, or false to omit the number altogether. + Can be either 0 or 1, or false to omit the number altogether. Defaults to 0. Program exits successfully if any valid version satisfies @@ -217,7 +217,7 @@ Object { -n Base number to be used for the prerelease identifier. - Can be either 0or 1, or false to omit the number altogether. + Can be either 0 or 1, or false to omit the number altogether. Defaults to 0. Program exits successfully if any valid version satisfies @@ -278,7 +278,7 @@ Object { -n Base number to be used for the prerelease identifier. - Can be either 0or 1, or false to omit the number altogether. + Can be either 0 or 1, or false to omit the number altogether. Defaults to 0. Program exits successfully if any valid version satisfies From 4246523756eccb36f1940bcd7d3a80cc929c32da Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Thu, 13 Apr 2023 18:08:08 +0200 Subject: [PATCH 5/8] feat: removed string false check --- classes/semver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/semver.js b/classes/semver.js index 910807b6..f28bac26 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -268,7 +268,7 @@ class SemVer { // 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 || identifierBase === 'false') { + if (identifierBase === false) { prerelease = [identifier] } if (compareIdentifiers(this.prerelease[0], identifier) === 0) { From d0399b5f1e5dca76e51e7c73525157860a7cf651 Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Thu, 13 Apr 2023 20:04:42 +0200 Subject: [PATCH 6/8] feat: added more tests, restored accidental removed test --- test/bin/semver.js | 1 + test/fixtures/increments.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/test/bin/semver.js b/test/bin/semver.js index 12fcbbe0..262ca380 100644 --- a/test/bin/semver.js +++ b/test/bin/semver.js @@ -28,6 +28,7 @@ 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', '-n', 'false'], ['-i', '1.2.3'], diff --git a/test/fixtures/increments.js b/test/fixtures/increments.js index cb99b4e5..15acfe6a 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,8 +100,12 @@ 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], @@ -116,4 +121,5 @@ module.exports = [ ['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', '1.2.1-1', false, '', false], ] From 136de01a07c25273ae934bb653492393edecf435 Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Fri, 14 Apr 2023 12:11:06 +0200 Subject: [PATCH 7/8] feat: throwing when identifier base is false and if indentifier is missing or same as prerelese version --- classes/semver.js | 9 ++++++++- test/fixtures/increments.js | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/classes/semver.js b/classes/semver.js index f28bac26..ea62fa0d 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -247,7 +247,11 @@ class SemVer { // This probably shouldn't be used publicly. // 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction. case 'pre': { - const base = identifierBase === false || Number(identifierBase) ? 1 : 0 + 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 = [base] @@ -261,6 +265,9 @@ class SemVer { } if (i === -1) { // didn't increment anything + if (identifier === this.prerelease.join('.') && identifierBase === false) { + throw new Error('invalid increment argument: identifier already exists') + } this.prerelease.push(base) } } diff --git a/test/fixtures/increments.js b/test/fixtures/increments.js index 15acfe6a..1fbc4bc9 100644 --- a/test/fixtures/increments.js +++ b/test/fixtures/increments.js @@ -115,11 +115,12 @@ module.exports = [ ['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', '1.2.3-dev.1', 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', '1.2.1-1', false, '', false], + ['1.2.0', 'prerelease', null, false, '', false], ] From ac3016bfcac375969e4eada9ad732708297f5463 Mon Sep 17 00:00:00 2001 From: Luka Svalina Date: Fri, 14 Apr 2023 17:07:33 +0200 Subject: [PATCH 8/8] feat: updated readme --- README.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/README.md b/README.md index 317c8655..b52a5eb1 100644 --- a/README.md +++ b/README.md @@ -264,13 +264,6 @@ $ 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