semver-utils.js
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(function () {
"use strict";
// 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
;
// Returns a new object with all of the undefined properties removed from the given object
function pruned(obj) {
var o = {};
for(var key in obj) {
if ('undefined' !== typeof obj[key]) {
o[key] = obj[key];
}
}
return o;
}
function stringifySemver(obj) {
var str = ''
;
str += obj.major || '0';
str += '.';
str += obj.minor || '0';
str += '.';
str += obj.patch || '0';
if (obj.release) {
str += '-' + obj.release;
}
if (obj.build) {
str += '+' + obj.build;
}
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(pruned({
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) {
var m
, arr = []
, obj
;
while (m = reSemverRange.exec(str)) {
obj = {
semver: m[3]
, operator: m[4] || m[2]
, major: m[6]
, minor: m[8]
, patch: m[10]
};
if ('+' === m[12]) {
obj.build = m[13];
}
if ('-' === m[12]) {
obj.release = m[13];
}
arr.push(new SemVer(pruned(obj)));
//console.log(m);
}
//return new SemVerRange(arr);
return arr;
}
module.exports.parse = parseSemver;
module.exports.stringify = stringifySemver;
module.exports.parseRange = parseSemverRange;
module.exports.stringifyRange = stringifySemverRange;
}());