Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add inc function

  • Loading branch information...
commit 77138bcf316f2eab9b88abf66a907a6147ecfaa6 1 parent 6e8da86
@chellmuth authored
Showing with 60 additions and 4 deletions.
  1. +2 −0  README.md
  2. +58 −4 semver.js
View
2  README.md
@@ -88,6 +88,8 @@ Ranges can be joined with either a space (which implies "and") or a
## Functions
* valid(v): Return the parsed version, or null if it's not valid.
+* inc(v, release): Return the version incremented by the release type
+ (major, minor, patch, or build), or null if it's not valid.
### Comparison
View
62 semver.js
@@ -36,18 +36,24 @@ exports.lte = lte
exports.eq = eq
exports.neq = neq
exports.cmp = cmp
+exports.inc = inc
exports.valid = valid
exports.validPackage = validPackage
exports.validRange = validRange
exports.maxSatisfying = maxSatisfying
-function clean (ver) {
- v = exports.parse(ver)
- if (!v) return v
+function stringify (version) {
+ var v = version
return [v[1]||'', v[2]||'', v[3]||''].join(".") + (v[4]||'') + (v[5]||'')
}
+function clean (version) {
+ version = exports.parse(version)
+ if (!version) return version
+ return stringify(version)
+}
+
function valid (version) {
return exports.parse(version) && version.trim().replace(/^[v=]+/, '')
}
@@ -257,12 +263,37 @@ function gt (v1, v2) {
: tag1 > tag2
}
+function inc (version, release) {
+ version = exports.parse(version)
+ if (!version) return null
+
+ var parsedIndexLookup =
+ { 'major': 1
+ , 'minor': 2
+ , 'patch': 3
+ , 'build': 4 }
+ var incIndex = parsedIndexLookup[release]
+ if (incIndex === undefined) return null
+
+ var current = num(version[incIndex])
+ version[incIndex] = current === -1 ? 1 : current + 1
+
+ for (var i = incIndex + 1; i < 5; i ++) {
+ if (num(version[i]) !== -1) version[i] = "0"
+ }
+
+ if (version[4]) version[4] = "-" + version[4]
+ version[5] = ""
+
+ return stringify(version)
+}
+
if (module === require.main) { // tests below
var tap = require("tap")
, test = tap.test
-tap.plan(4)
+tap.plan(5)
test("comparison tests", function (t) {
; [ ["0.0.0", "0.0.0foo"]
@@ -461,4 +492,27 @@ test("negative range tests", function (t) {
t.end()
})
+test("increment versions test", function (t) {
+; [ [ "1.2.3", "major", "2.0.0" ]
+ , [ "1.2.3", "minor", "1.3.0" ]
+ , [ "1.2.3", "patch", "1.2.4" ]
+ , [ "1.2.3", "build", "1.2.3-1" ]
+ , [ "1.2.3-4", "build", "1.2.3-5" ]
+
+ , [ "1.2.3tag", "major", "2.0.0" ]
+ , [ "1.2.3-tag", "major", "2.0.0" ]
+ , [ "1.2.3tag", "build", "1.2.3-1" ]
+ , [ "1.2.3-tag", "build", "1.2.3-1" ]
+ , [ "1.2.3-4-tag", "build", "1.2.3-5" ]
+ , [ "1.2.3-4tag", "build", "1.2.3-5" ]
+
+ , [ "1.2.3", "fake", null ]
+ , [ "fake", "major", null ]
+ ].forEach(function (v) {
+ t.equal(inc(v[0], v[1]), v[2], "inc("+v[0]+", "+v[1]+") === "+v[2])
+ })
+
+ t.end()
+})
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.