Skip to content

Commit

Permalink
identifierIndex validation added
Browse files Browse the repository at this point in the history
  • Loading branch information
b-bly committed Jul 22, 2018
1 parent 950e912 commit 283ff2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
coverage/
.nyc_output/
.idea
launch.json
.vscode/launch.json
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"

]
Expand Down
28 changes: 21 additions & 7 deletions semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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) {
Expand All @@ -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)) {
Expand Down

0 comments on commit 283ff2a

Please sign in to comment.