diff options
author | isaacs <i@izs.me> | 2010-05-03 02:13:31 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-05-03 05:07:49 +0400 |
commit | 6256948154442ed6989d110763f974b0d942bd61 (patch) | |
tree | 60ad5535d0f512b999992846525ff789bac798ec /lib | |
parent | 09475c633c42722c38a8151201a543b76afd8cbb (diff) |
Code style
Diffstat (limited to 'lib')
-rw-r--r-- | lib/utils/semver.js | 189 |
1 files changed, 95 insertions, 94 deletions
diff --git a/lib/utils/semver.js b/lib/utils/semver.js index 8109250d1..b1c2a3b59 100644 --- a/lib/utils/semver.js +++ b/lib/utils/semver.js @@ -3,25 +3,25 @@ // This implementation is a *hair* less strict in that it allows // v1.2.3 things, and also tags that don't begin with a char. -var semver = "v?([0-9]+)\\.([0-9]+)\\.([0-9]+)([a-zA-Z-][a-zA-Z0-9-]*)?"; +var semver = "v?([0-9]+)\\.([0-9]+)\\.([0-9]+)([a-zA-Z-][a-zA-Z0-9-]*)?" -exports.parse = new RegExp("^\\s*"+semver+"\\s*$"); +exports.parse = new RegExp("^\\s*"+semver+"\\s*$") exports.parsePackage = new RegExp( - "^\\s*([a-zA-Z][a-zA-Z0-9_-]*)-(" +semver+")\\s*$"); + "^\\s*([a-zA-Z][a-zA-Z0-9_-]*)-(" +semver+")\\s*$") exports.parseRange = new RegExp( - "^\\s*(" + semver + ")\\s+-\\s+(" + semver + ")\\s*$"); -exports.rangeReplace = ">=$1 <=$6"; + "^\\s*(" + semver + ")\\s+-\\s+(" + semver + ")\\s*$") +exports.rangeReplace = ">=$1 <=$6" -exports.compare = compare; -exports.satisfies = satisfies; -exports.gt = gt; -exports.lt = lt; -exports.valid = valid; -exports.validPackage = validPackage; +exports.compare = compare +exports.satisfies = satisfies +exports.gt = gt +exports.lt = lt +exports.valid = valid +exports.validPackage = validPackage function valid (version) { return exports.parse(version) && version.trim() } function validPackage (version) { - return exports.parsePackage(version) && version.trim(); + return exports.parsePackage(version) && version.trim() } // range can be one of: @@ -30,111 +30,112 @@ function validPackage (version) { // ">=1.0.2" like 1.0.2 - 9999.9999.9999 // "<2.0.0" like 0.0.0 - 1.9999.9999 // ">1.0.2 <2.0.0" like 1.0.3 - 1.9999.9999 -var starExpression = /(<|>)?=?\*/g, - starReplace = "", - compTrimExpression = new RegExp("((<|>)?=?)\\s*("+semver+")", "g"), - compTrimReplace = "$1$3"; +var starExpression = /(<|>)?=?\*/g + , starReplace = "" + , compTrimExpression = new RegExp("((<|>)?=?)\\s*("+semver+")", "g") + , compTrimReplace = "$1$3" function satisfies (version, range) { - version = valid(version); - if (!version) return false; + version = valid(version) + if (!version) return false range = range.trim() .replace(exports.parseRange, exports.rangeReplace) .replace(compTrimExpression, compTrimReplace) .replace(starExpression, starReplace) - .split(/\s+/); + .split(/\s+/) for (var i = 0, l = range.length; i < l; i ++) if (range[i]) { - var r = range[i], - gtlt = r.charAt(0) === ">" ? gt : r.charAt(0) === "<" ? lt : false, - eq = r.charAt(!!gtlt) === "=", - sub = (!!eq) + (!!gtlt); - r = valid(r.substr(sub)); - if (!r) return false; - if (!gtlt) eq = true; - if (eq && r === version) continue; - if (gtlt && gtlt(version, r)) continue; - return false; + var r = range[i] + , gtlt = r.charAt(0) === ">" ? gt : r.charAt(0) === "<" ? lt : false + , eq = r.charAt(!!gtlt) === "=" + , sub = (!!eq) + (!!gtlt) + + r = valid(r.substr(sub)) + if (!r) return false + if (!gtlt) eq = true + if (eq && r === version) continue + if (gtlt && gtlt(version, r)) continue + return false } - return true; + return true } // return v1 > v2 ? 1 : -1 function compare (v1, v2) { - return v1 === v2 ? 0 : gt(v1, v2) ? 1 : -1; + return v1 === v2 ? 0 : gt(v1, v2) ? 1 : -1 } function lt (v1, v2) { return gt(v2, v1) } // return v1 > v2 function gt (v1, v2) { - v1 = exports.parse(v1); - v2 = exports.parse(v2); - if (!v1 || !v2) return false; + v1 = exports.parse(v1) + v2 = exports.parse(v2) + if (!v1 || !v2) return false for (var i = 1; i < 4; i ++) { - if ((+v1[i]) > (+v2[i])) return true; - else if ((+v1[i]) !== (+v2[i])) return false; + if ((+v1[i]) > (+v2[i])) return true + else if ((+v1[i]) !== (+v2[i])) return false } // no tag is > than any tag, or use lexicographical order. - var tag1 = v1[4] || "", - tag2 = v2[4] || ""; - return tag2 && (!tag1 || tag1 > tag2); + var tag1 = v1[4] || "" + , tag2 = v2[4] || "" + return tag2 && (!tag1 || tag1 > tag2) } -if (module !== process.mainModule) return; // tests below +if (module !== process.mainModule) return // tests below -var assert = require("assert"); +var assert = require("assert") -[ ["0.0.0", "0.0.0foo"] -, ["0.0.1", "0.0.0"] -, ["1.0.0", "0.9.9"] -, ["0.10.0", "0.9.0"] -, ["0.99.0", "0.10.0"] -, ["2.0.0", "1.2.3"] -, ["v0.0.0", "0.0.0foo"] -, ["v0.0.1", "0.0.0"] -, ["v1.0.0", "0.9.9"] -, ["v0.10.0", "0.9.0"] -, ["v0.99.0", "0.10.0"] -, ["v2.0.0", "1.2.3"] -, ["0.0.0", "v0.0.0foo"] -, ["0.0.1", "v0.0.0"] -, ["1.0.0", "v0.9.9"] -, ["0.10.0", "v0.9.0"] -, ["0.99.0", "v0.10.0"] -, ["2.0.0", "v1.2.3"] -].forEach(function (v) { - assert.ok(gt(v[0], v[1]), "gt('"+v[0]+"', '"+v[1]+"')"); - assert.ok(lt(v[1], v[0]), "lt('"+v[1]+"', '"+v[0]+"')"); - assert.ok(!gt(v[1], v[0]), "!gt('"+v[1]+"', '"+v[0]+"')"); - assert.ok(!lt(v[0], v[1]), "!lt('"+v[0]+"', '"+v[1]+"')"); -}); +; [ ["0.0.0", "0.0.0foo"] + , ["0.0.1", "0.0.0"] + , ["1.0.0", "0.9.9"] + , ["0.10.0", "0.9.0"] + , ["0.99.0", "0.10.0"] + , ["2.0.0", "1.2.3"] + , ["v0.0.0", "0.0.0foo"] + , ["v0.0.1", "0.0.0"] + , ["v1.0.0", "0.9.9"] + , ["v0.10.0", "0.9.0"] + , ["v0.99.0", "0.10.0"] + , ["v2.0.0", "1.2.3"] + , ["0.0.0", "v0.0.0foo"] + , ["0.0.1", "v0.0.0"] + , ["1.0.0", "v0.9.9"] + , ["0.10.0", "v0.9.0"] + , ["0.99.0", "v0.10.0"] + , ["2.0.0", "v1.2.3"] + ].forEach(function (v) { + assert.ok(gt(v[0], v[1]), "gt('"+v[0]+"', '"+v[1]+"')") + assert.ok(lt(v[1], v[0]), "lt('"+v[1]+"', '"+v[0]+"')") + assert.ok(!gt(v[1], v[0]), "!gt('"+v[1]+"', '"+v[0]+"')") + assert.ok(!lt(v[0], v[1]), "!lt('"+v[0]+"', '"+v[1]+"')") + }) -[ ["1.0.0 - 2.0.0", "1.2.3"] -, ["1.0.0", "1.0.0"] -, [">=*", "0.2.4"] -, ["", "1.0.0"] -, ["*", "1.2.3"] -, ["*", "v1.2.3-foo"] -, [">=1.0.0", "1.0.0"] -, [">=1.0.0", "1.0.1"] -, [">=1.0.0", "1.1.0"] -, [">1.0.0", "1.0.1"] -, [">1.0.0", "1.1.0"] -, ["<=2.0.0", "2.0.0"] -, ["<=2.0.0", "1.9999.9999"] -, ["<=2.0.0", "0.2.9"] -, ["<2.0.0", "1.9999.9999"] -, ["<2.0.0", "0.2.9"] -, [">= 1.0.0", "1.0.0"] -, [">= 1.0.0", "1.0.1"] -, [">= 1.0.0", "1.1.0"] -, ["> 1.0.0", "1.0.1"] -, ["> 1.0.0", "1.1.0"] -, ["<= 2.0.0", "2.0.0"] -, ["<= 2.0.0", "1.9999.9999"] -, ["<= 2.0.0", "0.2.9"] -, ["< 2.0.0", "1.9999.9999"] -, ["<\t2.0.0", "0.2.9"] -].forEach(function (v) { - assert.ok(satisfies(v[1], v[0]), v[0]+" satisfied by "+v[1]); -}); +; [ ["1.0.0 - 2.0.0", "1.2.3"] + , ["1.0.0", "1.0.0"] + , [">=*", "0.2.4"] + , ["", "1.0.0"] + , ["*", "1.2.3"] + , ["*", "v1.2.3-foo"] + , [">=1.0.0", "1.0.0"] + , [">=1.0.0", "1.0.1"] + , [">=1.0.0", "1.1.0"] + , [">1.0.0", "1.0.1"] + , [">1.0.0", "1.1.0"] + , ["<=2.0.0", "2.0.0"] + , ["<=2.0.0", "1.9999.9999"] + , ["<=2.0.0", "0.2.9"] + , ["<2.0.0", "1.9999.9999"] + , ["<2.0.0", "0.2.9"] + , [">= 1.0.0", "1.0.0"] + , [">= 1.0.0", "1.0.1"] + , [">= 1.0.0", "1.1.0"] + , ["> 1.0.0", "1.0.1"] + , ["> 1.0.0", "1.1.0"] + , ["<= 2.0.0", "2.0.0"] + , ["<= 2.0.0", "1.9999.9999"] + , ["<= 2.0.0", "0.2.9"] + , ["< 2.0.0", "1.9999.9999"] + , ["<\t2.0.0", "0.2.9"] + ].forEach(function (v) { + assert.ok(satisfies(v[1], v[0]), v[0]+" satisfied by "+v[1]) + }) |