created SemVer class such that ver.toString() calls stringify(ver)

This commit is contained in:
AJ ONeal 2013-05-22 15:57:39 -05:00
parent fa2a82ef75
commit c80b378a8b
2 changed files with 81 additions and 43 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "semver-utils", "name": "semver-utils",
"version": "1.0.3", "version": "1.1.0",
"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

@ -11,32 +11,10 @@
, 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) {
// semver, major, minor, patch
// https://github.com/mojombo/semver/issues/32
// https://github.com/isaacs/node-semver/issues/10
// optional v
var m = reSemver.exec(version) || []
, ver = {
semver: m[0]
, version: m[1]
, major: m[2]
, minor: m[3]
, patch: m[4]
, release: m[5]
, build: m[6]
}
;
if (0 === m.length) {
ver = null;
}
return ver;
}
function stringifySemver(obj) { function stringifySemver(obj) {
var str = ''; var str = ''
;
str += obj.major || '0'; str += obj.major || '0';
str += '.'; str += '.';
str += obj.minor || '0'; str += obj.minor || '0';
@ -51,6 +29,81 @@
return str; return str;
} }
function stringifySemverRange(arr) {
var str = ''
;
function stringify(ver) {
if (ver.operator) {
str += ver.operator + ' ';
}
if (ver.major) {
str += ver.toString() + ' ';
}
}
arr.forEach(stringify);
return str.trim();
}
function SemVer(obj) {
if (!obj) {
return;
}
var me = this
;
Object.keys(obj).forEach(function (key) {
me[key] = obj[key];
});
}
SemVer.prototype.toString = function () {
return stringifySemver(this);
};
/*
function SemVerRange(obj) {
if (!obj) {
return;
}
var me = this
;
Object.keys(obj).forEach(function (key) {
me[key] = obj[key];
});
}
SemVerRange.prototype = [];
SemVerRange.prototype.toString = stringifySemverRange;
*/
function parseSemver(version) {
// semver, major, minor, patch
// https://github.com/mojombo/semver/issues/32
// https://github.com/isaacs/node-semver/issues/10
// optional v
var m = reSemver.exec(version) || []
, ver = new SemVer({
semver: m[0]
, version: m[1]
, major: m[2]
, minor: m[3]
, patch: m[4]
, release: m[5]
, build: m[6]
})
;
if (0 === m.length) {
ver = null;
}
return ver;
}
function parseSemverRange(str) { function parseSemverRange(str) {
var m var m
, arr = [] , arr = []
@ -82,29 +135,14 @@
obj.release = m[13]; obj.release = m[13];
} }
Object.keys(obj).forEach(prune); Object.keys(obj).forEach(prune);
arr.push(obj); arr.push(new SemVer(obj));
//console.log(m); //console.log(m);
} }
//return new SemVerRange(arr);
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;