diff --git a/package.json b/package.json index b3e71b0..f40e0c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "semver-utils", - "version": "1.0.2", + "version": "1.0.3", "description": "Tools for manipulating semver strings and objects", "main": "semver-utils.js", "scripts": { diff --git a/semver-utils-tools.js b/semver-utils-test.js similarity index 67% rename from semver-utils-tools.js rename to semver-utils-test.js index f1faa5a..9bfa0a2 100644 --- a/semver-utils-tools.js +++ b/semver-utils-test.js @@ -1,17 +1,39 @@ (function () { - var semverutils = require('semver-utils') + var semverutils = require('./semver-utils') ; 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')); - console.log(semverutils.parseRange('< v2.0.0')); - console.log(semverutils.parseRange('~v2.0.0')); + + good.every(function (range) { + var result = semverutils.parseRange(range) + ; + + 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() { + console.info('testParse'); var good , bad ; @@ -43,7 +65,7 @@ ]; bad = [ - "v1.0.0" + // "v1.0.0" now allows optional 'v' , "a.b.c" , "1" , "1.0.0b" diff --git a/semver-utils.js b/semver-utils.js index 29975f0..e6a5a39 100644 --- a/semver-utils.js +++ b/semver-utils.js @@ -1,8 +1,14 @@ (function () { "use strict"; - var reSemver = /^((\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 + // TODO break these down into escaped strings with meaningful comments and create using new RegExp() + // |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) { @@ -83,7 +89,24 @@ 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.stringify = stringifySemver; module.exports.parseRange = parseSemverRange; + module.exports.stringifyRange = stringifySemverRange; }());