diff --git a/.gitignore b/.gitignore index d532e7bf..af1a159a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ coverage/ .nyc_output/ .idea -launch.json \ No newline at end of file +.vscode/launch.json \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 47e3057c..591e718a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,9 +10,9 @@ "name": "Launch Program", "program": "${workspaceFolder}/bin/semver", "args": [ - "-i", "preminor", + "-i", "prerelease", "--preid", "dev", - "-n", "1", + "-n", "0", "1.2.0" ] diff --git a/semver.js b/semver.js index 490c45f9..4b004ec3 100644 --- a/semver.js +++ b/semver.js @@ -461,8 +461,9 @@ SemVer.prototype.inc = function(release, identifier, identifierIndex) { // This probably shouldn't be used publicly. // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction. case 'pre': - if (identifierIndex) { - this.prerelease = [identifierIndex]; + if (identifierIndex && validateIdentifierIndex(identifierIndex)) { + index = identifierIndex; + this.prerelease = [identifierIndex]; } if (this.prerelease.length === 0) this.prerelease = [0]; @@ -481,10 +482,10 @@ SemVer.prototype.inc = function(release, identifier, identifierIndex) { //if user specified identifierIndex, use their value. // Zero-based or one-based. Default is zero. var index = 0; - if (typeof identifierIndex === 'number' && - isNaN(identifierIndex) == false) { - index = identifierIndex; - } + // validation for identifierIndex done above + if (identifierIndex) { + index = identifierIndex; + } // 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 (this.prerelease[0] === identifier) { @@ -510,15 +511,28 @@ function inc(version, release, loose, identifier, identifierIndex) { identifier = loose; loose = undefined; } + if (identifierIndex) identifierIndex = parseInt(identifierIndex); try { - identifierIndex = parseInt(identifierIndex); return new SemVer(version, loose).inc(release, identifier, identifierIndex).version; } catch (er) { return null; } } +function validateIdentifierIndex (identifierIndex) { + if (typeof identifierIndex === 'number' && + isNaN(identifierIndex) == false) { + if (identifierIndex === 1 || identifierIndex === 0) { + return true; + } else { + throw new TypeError('Invalid identifier number base: ' + identifierIndex); + } + } else { + throw new TypeError('Invalid identifier number base: ' + identifierIndex); + } +} + exports.diff = diff; function diff(version1, version2) { if (eq(version1, version2)) {