-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsver.js
392 lines (376 loc) · 11.3 KB
/
sver.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
'use strict';
const shortSemverRegEx = /^([~\^])?(0|[1-9]\d*)(?:\.(0|[1-9]\d*))?$/;
const semverRegEx = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\da-z-]+(?:\.[\da-z-]+)*))?(\+[\da-z-]+)?$/i;
exports.semverRegEx = semverRegEx;
exports.shortSemverRegEx = shortSemverRegEx;
const MAJOR = Symbol('major');
const MINOR = Symbol('minor');
const PATCH = Symbol('patch');
const PRE = Symbol('pre');
const BUILD = Symbol('build');
const TAG = Symbol('tag');
let numRegEx = /^\d+$/;
class Semver {
constructor (version) {
let semver = version.match(semverRegEx);
if (!semver) {
this[TAG] = version;
return;
}
this[MAJOR] = parseInt(semver[1], 10);
this[MINOR] = parseInt(semver[2], 10);
this[PATCH] = parseInt(semver[3], 10);
this[PRE] = semver[4] && semver[4].split('.');
this[BUILD] = semver[5];
}
get major () {
return this[MAJOR];
}
get minor () {
return this[MINOR];
}
get patch () {
return this[PATCH];
}
get pre () {
return this[PRE];
}
get build () {
return this[BUILD];
}
get tag () {
return this[TAG];
}
gt (version) {
return Semver.compare(this, version) === 1;
}
lt (version) {
return Semver.compare(this, version) === -1;
}
eq (version) {
if (!(version instanceof Semver))
version = new Semver(version);
if (this[TAG] && version[TAG])
return this[TAG] === version[TAG];
if (this[TAG] || version[TAG])
return false;
if (this[MAJOR] !== version[MAJOR])
return false;
if (this[MINOR] !== version[MINOR])
return false;
if (this[PATCH] !== version[PATCH])
return false;
if (this[PRE] === undefined && version[PRE] === undefined)
return true;
if (this[PRE] === undefined || version[PRE] === undefined)
return false;
if (this[PRE].length !== version[PRE].length)
return false;
for (let i = 0; i < this[PRE].length; i++) {
if (this[PRE][i] !== version[PRE][i])
return false;
}
return this[BUILD] === version[BUILD];
}
matches (range, unstable = false) {
if (!(range instanceof SemverRange))
range = new SemverRange(range);
return range.has(this, unstable);
}
toString () {
if (this[TAG])
return this[TAG];
return this[MAJOR] + '.' + this[MINOR] + '.' + this[PATCH] + (this[PRE] ? '-' + this[PRE].join('.') : '') + (this[BUILD] ? this[BUILD] : '');
}
toJSON() {
return this.toString();
}
static isValid (version) {
let semver = version.match(semverRegEx);
return semver && semver[2] !== undefined && semver[3] !== undefined;
}
static compare (v1, v2) {
if (!(v1 instanceof Semver))
v1 = new Semver(v1);
if (!(v2 instanceof Semver))
v2 = new Semver(v2);
// not semvers - tags have equal precedence
if (v1[TAG] && v2[TAG])
return 0;
// semver beats tag version
if (v1[TAG])
return -1;
if (v2[TAG])
return 1;
// compare version numbers
if (v1[MAJOR] !== v2[MAJOR])
return v1[MAJOR] > v2[MAJOR] ? 1 : -1;
if (v1[MINOR] !== v2[MINOR])
return v1[MINOR] > v2[MINOR] ? 1 : -1;
if (v1[PATCH] !== v2[PATCH])
return v1[PATCH] > v2[PATCH] ? 1 : -1;
if (!v1[PRE] && !v2[PRE])
return 0;
if (!v1[PRE])
return 1;
if (!v2[PRE])
return -1;
// prerelease comparison
return prereleaseCompare(v1[PRE], v2[PRE]);
}
}
exports.Semver = Semver;
function prereleaseCompare (v1Pre, v2Pre) {
for (let i = 0, l = Math.min(v1Pre.length, v2Pre.length); i < l; i++) {
if (v1Pre[i] !== v2Pre[i]) {
let isNum1 = v1Pre[i].match(numRegEx);
let isNum2 = v2Pre[i].match(numRegEx);
// numeric has lower precedence
if (isNum1 && !isNum2)
return -1;
if (isNum2 && !isNum1)
return 1;
// compare parts
if (isNum1 && isNum2)
return parseInt(v1Pre[i], 10) > parseInt(v2Pre[i], 10) ? 1 : -1;
else
return v1Pre[i] > v2Pre[i] ? 1 : -1;
}
}
if (v1Pre.length === v2Pre.length)
return 0;
// more pre-release fields win if equal
return v1Pre.length > v2Pre.length ? 1 : -1;
}
const WILDCARD_RANGE = 0;
const MAJOR_RANGE = 1;
const STABLE_RANGE = 2;
const EXACT_RANGE = 3;
const TYPE = Symbol('type');
const VERSION = Symbol('version');
class SemverRange {
constructor (versionRange) {
if (versionRange === '*' || versionRange === '') {
this[TYPE] = WILDCARD_RANGE;
return;
}
let shortSemver = versionRange.match(shortSemverRegEx);
if (shortSemver) {
if (shortSemver[1])
versionRange = versionRange.substr(1);
if (shortSemver[3] === undefined) {
// ^, ~ mean the same thing for a single major
this[VERSION] = new Semver(versionRange + '.0.0');
this[TYPE] = MAJOR_RANGE;
}
else {
this[VERSION] = new Semver(versionRange + '.0');
// ^ only becomes major range for major > 0
if (shortSemver[1] === '^' && shortSemver[2] !== '0')
this[TYPE] = MAJOR_RANGE;
else
this[TYPE] = STABLE_RANGE;
}
// empty pre array === support prerelease ranges
this[VERSION][PRE] = this[VERSION][PRE] || [];
}
// forces hat on 0.x versions
else if (versionRange.startsWith('^^')) {
this[VERSION] = new Semver(versionRange.substr(2));
this[TYPE] = MAJOR_RANGE;
}
else if (versionRange[0] === '^') {
this[VERSION] = new Semver(versionRange.substr(1));
if (this[VERSION][MAJOR] === 0) {
if (this[VERSION][MINOR] === 0)
this[TYPE] = EXACT_RANGE;
else
this[TYPE] = STABLE_RANGE;
}
else {
this[TYPE] = MAJOR_RANGE;
}
}
else if (versionRange[0] === '~') {
this[VERSION] = new Semver(versionRange.substr(1));
this[TYPE] = STABLE_RANGE;
}
else {
this[VERSION] = new Semver(versionRange);
this[TYPE] = EXACT_RANGE;
}
if (this[VERSION][TAG] && this[TYPE] !== EXACT_RANGE)
this[TYPE] = EXACT_RANGE;
}
get isExact () {
return this[TYPE] === EXACT_RANGE;
}
get isExactSemver () {
return this[TYPE] === EXACT_RANGE && this.version[TAG] === undefined;
}
get isExactTag () {
return this[TYPE] === EXACT_RANGE && this.version[TAG] !== undefined;
}
get isStable () {
return this[TYPE] === STABLE_RANGE;
}
get isMajor () {
return this[TYPE] === MAJOR_RANGE;
}
get isWildcard () {
return this[TYPE] === WILDCARD_RANGE;
}
get type () {
switch (this[TYPE]) {
case WILDCARD_RANGE:
return 'wildcard';
case MAJOR_RANGE:
return 'major';
case STABLE_RANGE:
return 'stable';
case EXACT_RANGE:
return 'exact';
}
}
get version () {
return this[VERSION];
}
gt (range) {
return SemverRange.compare(this, range) === 1;
}
lt (range) {
return SemverRange.compare(this, range) === -1;
}
eq (range) {
return SemverRange.compare(this, range) === 0;
}
has (version, unstable = false) {
if (!(version instanceof Semver))
version = new Semver(version);
if (this[TYPE] === WILDCARD_RANGE)
return unstable || (!version[PRE] && !version[TAG]);
if (this[TYPE] === EXACT_RANGE)
return this[VERSION].eq(version);
if (version[TAG])
return false;
if (this[VERSION][MAJOR] !== version[MAJOR])
return false;
if (this[TYPE] === MAJOR_RANGE ? this[VERSION][MINOR] > version[MINOR] : this[VERSION][MINOR] !== version[MINOR])
return false;
if ((this[TYPE] === MAJOR_RANGE ? this[VERSION][MINOR] === version[MINOR] : true) && this[VERSION][PATCH] > version[PATCH])
return false;
if (version[PRE] === undefined || version[PRE].length === 0)
return true;
if (this[VERSION][PRE] === undefined || this[VERSION][PRE].length === 0)
return unstable;
if (unstable === false && (this[VERSION][MINOR] !== version[MINOR] || this[VERSION][PATCH] !== version[PATCH]))
return false;
return prereleaseCompare(this[VERSION][PRE], version[PRE]) !== 1;
}
contains (range) {
if (!(range instanceof SemverRange))
range = new SemverRange(range);
if (this[TYPE] === WILDCARD_RANGE)
return true;
if (range[TYPE] === WILDCARD_RANGE)
return false;
return range[TYPE] >= this[TYPE] && this.has(range[VERSION], true);
}
intersect (range) {
if (!(range instanceof SemverRange))
range = new SemverRange(range);
if (this[TYPE] === WILDCARD_RANGE && range[TYPE] === WILDCARD_RANGE)
return this;
if (this[TYPE] === WILDCARD_RANGE)
return range;
if (range[TYPE] === WILDCARD_RANGE)
return this;
if (this[TYPE] === EXACT_RANGE)
return range.has(this[VERSION], true) ? this : undefined;
if (range[TYPE] === EXACT_RANGE)
return this.has(range[VERSION], true) ? range : undefined;
let higherRange, lowerRange, polarity;
if (range[VERSION].gt(this[VERSION])) {
higherRange = range;
lowerRange = this;
polarity = true;
}
else {
higherRange = this;
lowerRange = range;
polarity = false;
}
if (!lowerRange.has(higherRange[VERSION], true))
return;
if (lowerRange[TYPE] === MAJOR_RANGE)
return polarity ? range : this;
let intersection = new SemverRange(higherRange[VERSION].toString());
intersection[TYPE] = STABLE_RANGE;
return intersection;
}
bestMatch (versions, unstable = false) {
let maxSemver;
versions.forEach(version => {
if (!(version instanceof Semver))
version = new Semver(version);
if (!this.has(version, unstable))
return;
if (!maxSemver)
maxSemver = version;
else if (Semver.compare(version, maxSemver) === 1)
maxSemver = version;
});
return maxSemver;
}
toString () {
let version = this[VERSION];
switch (this[TYPE]) {
case WILDCARD_RANGE:
return '*';
case MAJOR_RANGE:
if (version[MAJOR] === 0 && version[MINOR] === 0 && version[PATCH] === 0)
return '0';
if (version[PRE] && version[PRE].length === 0 && version[PATCH] === 0)
return '^' + version[MAJOR] + '.' + version[MINOR];
return '^' + version.toString();
case STABLE_RANGE:
if (version[PRE] && version[PRE].length === 0 && version[PATCH] === 0 || version[MAJOR] === 0 && version[MINOR] === 0)
return version[MAJOR] + '.' + version[MINOR];
return '~' + version.toString();
case EXACT_RANGE:
return version.toString();
}
}
toJSON() {
return this.toString();
}
static match (range, version, unstable = false) {
if (!(version instanceof Semver))
version = new Semver(version);
return version.matches(range, unstable);
}
static isValid (range) {
let semverRange = new SemverRange(range);
return semverRange[TYPE] !== EXACT_RANGE || semverRange[VERSION][TAG] === undefined;
}
static compare (r1, r2) {
if (!(r1 instanceof SemverRange))
r1 = new SemverRange(r1);
if (!(r2 instanceof SemverRange))
r2 = new SemverRange(r2);
if (r1[TYPE] === WILDCARD_RANGE && r2[TYPE] === WILDCARD_RANGE)
return 0;
if (r1[TYPE] === WILDCARD_RANGE)
return 1;
if (r2[TYPE] === WILDCARD_RANGE)
return -1;
let cmp = Semver.compare(r1[VERSION], r2[VERSION]);
if (cmp !== 0) {
return cmp;
}
if (r1[TYPE] === r2[TYPE])
return 0;
return r1[TYPE] > r2[TYPE] ? 1 : -1;
}
}
exports.SemverRange = SemverRange;