allow 'v' prefix, implemented missing 'stringifyRange'

This commit is contained in:
AJ ONeal 2013-05-17 15:01:32 -04:00
parent 9dd782e51d
commit d1a4afb909
3 changed files with 55 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "semver-utils", "name": "semver-utils",
"version": "1.0.2", "version": "1.0.3",
"description": "Tools for manipulating semver strings and objects", "description": "Tools for manipulating semver strings and objects",
"main": "semver-utils.js", "main": "semver-utils.js",
"scripts": { "scripts": {

View File

@ -1,17 +1,39 @@
(function () { (function () {
var semverutils = require('semver-utils') var semverutils = require('./semver-utils')
; ;
function testParseRange() { function testParseRange() {
var str = '~1.0.0 || >= 1.1.7 < 2.0.0+build.1848 || v1.1.3 || 2.0.1-alpha.1227 || 1.0.0 - 1.0.x' console.info('testParseRange');
var good =
[ 'v1.0.0'
, '< v2.0.0'
, '~v2.0.0'
, '~1.0.0'
, '~1.0.0 || >= 1.1.7 < 2.0.0+build.1848'
, '~1.0.0 || >= 1.1.7 < 2.0.0+build.1848 || v1.1.3'
, '~1.0.0 || >= 1.1.7 < 2.0.0+build.1848 || v1.1.3 || 2.0.1-alpha.1227'
, '~1.0.0 || >= 1.1.7 < 2.0.0+build.1848 || v1.1.3 || 2.0.1-alpha.1227 || 1.0.0 - 1.0.x'
, '~1.0.0 || >= 1.1.7 < 2.0.0+build.1848 || v1.1.3 || 2.0.1-alpha.1227 || 1.0.0 - 1.0.x || 1.*'
]
; ;
console.log(semverutils.parseRange(str));
console.log(semverutils.parseRange('v1.0.0')); good.every(function (range) {
console.log(semverutils.parseRange('< v2.0.0')); var result = semverutils.parseRange(range)
console.log(semverutils.parseRange('~v2.0.0')); ;
if (!result || 0 === result.length) {
throw new Error("didn't parse something that should be parseable: " + range);
}
return true;
});
console.log(good[good.length - 1]);
console.log(semverutils.stringifyRange(semverutils.parseRange(good[good.length - 1])));
} }
function testParse() { function testParse() {
console.info('testParse');
var good var good
, bad , bad
; ;
@ -43,7 +65,7 @@
]; ];
bad = [ bad = [
"v1.0.0" // "v1.0.0" now allows optional 'v'
, "a.b.c" , "a.b.c"
, "1" , "1"
, "1.0.0b" , "1.0.0b"

View File

@ -1,8 +1,14 @@
(function () { (function () {
"use strict"; "use strict";
var reSemver = /^((\d+)\.(\d+)\.(\d+))(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?(?:\+([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?$/ // TODO break these down into escaped strings with meaningful comments and create using new RegExp()
, reSemverRange = /\s*((\|\||\-)|(([<>~]?=?)\s*(v)?([0-9]+)(\.(x|[0-9]+))?(\.(x|[0-9]+))?(([\-+])([a-zA-Z0-9\.]+))?))\s*/g // |optional 'v'
// | | 3 segment version
// | | |optional release prefixed by '-'
// | | | |optional build prefixed by '+'
var reSemver = /^v?((\d+)\.(\d+)\.(\d+))(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?(?:\+([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?$/
//, reSemverRange = /\s*((\|\||\-)|(([<>~]?=?)\s*(v)?([0-9]+)(\.(x|[0-9]+))?(\.(x|[0-9]+))?(([\-+])([a-zA-Z0-9\.]+))?))\s*/g
, reSemverRange = /\s*((\|\||\-)|(([<>~]?=?)\s*(v)?([0-9]+)(\.(x|\*|[0-9]+))?(\.(x|\*|[0-9]+))?(([\-+])([a-zA-Z0-9\.]+))?))\s*/g
; ;
function parseSemver(version) { function parseSemver(version) {
@ -83,7 +89,24 @@
return arr; return arr;
} }
function stringifySemverRange(arr) {
var str = ''
;
arr.forEach(function (ver) {
if (ver.operator) {
str += ver.operator + ' ';
}
if (ver.major) {
str += stringifySemver(ver) + ' ';
}
});
return str.trim();
}
module.exports.parse = parseSemver; module.exports.parse = parseSemver;
module.exports.stringify = stringifySemver; module.exports.stringify = stringifySemver;
module.exports.parseRange = parseSemverRange; module.exports.parseRange = parseSemverRange;
module.exports.stringifyRange = stringifySemverRange;
}()); }());