From 2870ef10c2cacc7c93013990d17e3cd2347d0640 Mon Sep 17 00:00:00 2001 From: Brendt Bly Date: Sat, 21 Jul 2018 13:53:35 -0500 Subject: [PATCH] feat: identifierBase parameter for .inc --- README.md | 21 +++++++++++++++++++++ bin/semver.js | 6 +++++- classes/semver.js | 19 ++++++++++--------- functions/inc.js | 5 +++-- test/classes/semver.js | 5 +++-- test/fixtures/increments.js | 20 +++++++++++++++++--- test/functions/inc.js | 12 ++++++------ 7 files changed, 65 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index df54e7a0..eceee28b 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,9 @@ Options: -l --loose Interpret versions and ranges loosely +-n <0|1> + This is the base to be used for the prerelease identifier. + -p --include-prerelease Always include prerelease versions in range matching @@ -232,6 +235,24 @@ $ semver 1.2.4-beta.0 -i prerelease 1.2.4-beta.1 ``` +#### Prerelease Identifier Base + +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 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' +``` + +command-line example: + +```bash +$ semver 1.2.3 -i prerelease --preid beta -n 1 +1.2.4-beta.1 +``` + ### Advanced Range Syntax Advanced range syntax desugars to primitive comparators in diff --git a/bin/semver.js b/bin/semver.js index 8d1b5572..2f2b8c22 100755 --- a/bin/semver.js +++ b/bin/semver.js @@ -23,6 +23,8 @@ let rtl = false let identifier +let identifierBase + const semver = require('../') let reverse = false @@ -71,6 +73,8 @@ const main = () => { case '-r': case '--range': range.push(argv.shift()) break + case '-n': + identifierBase = argv.shift() case '-c': case '--coerce': coerce = true break @@ -127,7 +131,7 @@ const success = () => { }).map((v) => { return semver.clean(v, options) }).map((v) => { - return inc ? semver.inc(v, inc, options, identifier) : v + return inc ? semver.inc(v, inc, options, identifier, identifierBase) : v }).forEach((v, i, _) => { console.log(v) }) diff --git a/classes/semver.js b/classes/semver.js index af629551..20eb0897 100644 --- a/classes/semver.js +++ b/classes/semver.js @@ -175,36 +175,36 @@ class SemVer { // preminor will bump the version up to the next minor release, and immediately // down to pre-release. premajor and prepatch work the same way. - inc (release, identifier) { + inc (release, identifier, identifierBase) { switch (release) { case 'premajor': this.prerelease.length = 0 this.patch = 0 this.minor = 0 this.major++ - this.inc('pre', identifier) + this.inc('pre', identifier, identifierBase) break case 'preminor': this.prerelease.length = 0 this.patch = 0 this.minor++ - this.inc('pre', identifier) + this.inc('pre', identifier, identifierBase) break case 'prepatch': // If this is already a prerelease, it will bump to the next version // drop any prereleases that might already exist, since they are not // relevant at this point. this.prerelease.length = 0 - this.inc('patch', identifier) - this.inc('pre', identifier) + this.inc('patch', identifier, identifierBase) + this.inc('pre', identifier, identifierBase) break // If the input is a non-prerelease version, this acts the same as // prepatch. case 'prerelease': if (this.prerelease.length === 0) { - this.inc('patch', identifier) + this.inc('patch', identifier, identifierBase) } - this.inc('pre', identifier) + this.inc('pre', identifier, identifierBase) break case 'major': @@ -263,14 +263,15 @@ class SemVer { } } 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, 0] + this.prerelease = [identifier, base] } } else { - this.prerelease = [identifier, 0] + this.prerelease = [identifier, base] } } break diff --git a/functions/inc.js b/functions/inc.js index 62d1da2c..7670b1be 100644 --- a/functions/inc.js +++ b/functions/inc.js @@ -1,7 +1,8 @@ const SemVer = require('../classes/semver') -const inc = (version, release, options, identifier) => { +const inc = (version, release, options, identifier, identifierBase) => { if (typeof (options) === 'string') { + identifierBase = identifier identifier = options options = undefined } @@ -10,7 +11,7 @@ const inc = (version, release, options, identifier) => { return new SemVer( version instanceof SemVer ? version.version : version, options - ).inc(release, identifier).version + ).inc(release, identifier, identifierBase).version } catch (er) { return null } diff --git a/test/classes/semver.js b/test/classes/semver.js index 50619c0f..4556434c 100644 --- a/test/classes/semver.js +++ b/test/classes/semver.js @@ -84,12 +84,13 @@ test('incrementing', t => { expect, options, id, + base, ]) => t.test(`${version} ${inc} ${id || ''}`.trim(), t => { t.plan(1) if (expect === null) { - t.throws(() => new SemVer(version, options).inc(inc, id)) + t.throws(() => new SemVer(version, options).inc(inc, id, base)) } else { - t.equal(new SemVer(version, options).inc(inc, id).version, expect) + t.equal(new SemVer(version, options).inc(inc, id, base).version, expect) } })) }) diff --git a/test/fixtures/increments.js b/test/fixtures/increments.js index 6a998b5f..eb76458d 100644 --- a/test/fixtures/increments.js +++ b/test/fixtures/increments.js @@ -1,5 +1,5 @@ -// [version, inc, result, options, identifier] -// inc(version, inc) -> result +// [version, inc, result, options, identifier, identifierBase] +// inc(version, inc, options, identifier, identifierBase) -> result module.exports = [ ['1.2.3', 'major', '2.0.0'], ['1.2.3', 'minor', '1.3.0'], @@ -82,9 +82,23 @@ module.exports = [ ['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'], - ['1.2.3-0', 'prerelease', '1.2.3-1.0', false, '1'], ['1.2.3-1.0', 'prerelease', '1.2.3-1.1', false, '1'], ['1.2.3-1.1', 'prerelease', '1.2.3-1.2', false, '1'], ['1.2.3-1.1', 'prerelease', '1.2.3-2.0', false, '2'], + + // [version, inc, result, identifierIndex, loose, identifier] + ['1.2.0-1', 'prerelease', '1.2.0-alpha.0', false, 'alpha', '0'], + ['1.2.1', 'prerelease', '1.2.2-alpha.0', false, 'alpha', '0'], + ['0.2.0', 'prerelease', '0.2.1-alpha.0', false, 'alpha', '0'], + ['1.2.2', 'prerelease', '1.2.3-alpha.1', false, 'alpha', '1'], + ['1.2.3', 'prerelease', '1.2.4-alpha.1', false, 'alpha', '1'], + ['1.2.4', 'prerelease', '1.2.5-alpha.1', false, 'alpha', '1'], + ['1.2.0', 'prepatch', '1.2.1-dev.1', false, 'dev', '1'], + ['1.2.0-1', 'prepatch', '1.2.1-dev.1', false, 'dev', '1'], + ['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.0', 'preminor', '1.3.0-dev.1', false, 'dev', '1'], + ['1.2.3-1', 'preminor', '1.3.0-dev.0', false, 'dev'] ] diff --git a/test/functions/inc.js b/test/functions/inc.js index 909debdf..e2d0768c 100644 --- a/test/functions/inc.js +++ b/test/functions/inc.js @@ -4,20 +4,20 @@ const parse = require('../../functions/parse') const increments = require('../fixtures/increments.js') test('increment versions test', (t) => { - increments.forEach(([pre, what, wanted, options, id]) => { - const found = inc(pre, what, options, id) - const cmd = `inc(${pre}, ${what}, ${id})` + increments.forEach(([pre, what, wanted, options, id, base]) => { + const found = inc(pre, what, options, id, base) + const cmd = `inc(${pre}, ${what}, ${id}, ${base})` t.equal(found, wanted, `${cmd} === ${wanted}`) const parsed = parse(pre, options) const parsedAsInput = parse(pre, options) if (wanted) { - parsed.inc(what, id) + parsed.inc(what, id, base) t.equal(parsed.version, wanted, `${cmd} object version updated`) t.equal(parsed.raw, wanted, `${cmd} object raw field updated`) const preIncObject = JSON.stringify(parsedAsInput) - inc(parsedAsInput, what, options, id) + inc(parsedAsInput, what, options, id, base) const postIncObject = JSON.stringify(parsedAsInput) t.equal( postIncObject, @@ -26,7 +26,7 @@ test('increment versions test', (t) => { ) } else if (parsed) { t.throws(() => { - parsed.inc(what, id) + parsed.inc(what, id, base) }) } else { t.equal(parsed, null)