forked from mallocator/Elasticsearch-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver.js
127 lines (115 loc) · 3.96 KB
/
semver.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
/**
* The JSON representation of a semantic version object. Defined here so it can be specified as a valid input for a
* new SemVer object.
* @typedef {Object} SemVerJson
* @property {number} major
* @property {number} minor
* @property {number} patch
* @property {string} original
*/
/**
* A class that parses a semantic versions and offers compare functions.
* @property {number} major The first version number encountered in a semver string.
* @property {number} minor The second version number encountered in a semver string.
* @property {number} patch The third version number encountered in a semver string.
* @property {string} suffix Any string suffixes at the end of the version.
*/
class SemVer {
/**
* @param {SemVer|SemVerJson|string|number} version
*/
constructor(version) {
if (version instanceof SemVer || version instanceof Object) {
this.major = version.major;
this.minor = version.minor || 0;
this.patch = version.patch || 0;
version = version.original;
}
this.original = version;
let parts = version.toString().split('.');
this.major = this.major || parseInt(parts[0]);
this.minor = this.minor || parts[1] ? parseInt(parts[1]) : 0;
this.patch = this.patch || parts[2] ? parseInt(parts[2]) : 0;
}
/**
* Returns true if both versions are the same.
* @param {SemVer|SemVerJson|string|number} version
* @returns {boolean}
*/
eq(version) {
version = version instanceof SemVer ? version : new SemVer(version);
return version.major == this.major && version.minor == this.minor && version.patch == this.patch;
}
/**
* Returns false if both versions are the same.
* @param {SemVer|SemVerJson|string|number} version
* @returns {boolean}
*/
ne(version) {
return !this.eq(version);
}
/**
* Returns true if this version is less than the passed in version.
* @param {SemVer|SemVerJson|string|number} version
* @returns {boolean}
*/
lt(version) {
version = version instanceof SemVer ? version : new SemVer(version);
if (this.major < version.major) {
return true;
}
if (this.major == version.major && this.minor < version.minor) {
return true;
}
return !!(this.major == version.major && this.minor == version.minor && this.patch < version.patch);
}
/**
* Returns true if this version is greater than the passed in version.
* @param {SemVer|SemVerJson|string|number} version
* @returns {boolean}
*/
gt(version) {
return !this.le(version);
}
/**
* Returns true if this version is less or equal than the passed in version.
* @param {SemVer|SemVerJson|string|number} version
* @returns {boolean}
*/
le(version) {
return this.eq(version) || this.lt(version);
}
/**
* Returns true if this version is greater or equal than the passed in version.
* @param {SemVer|SemVerJson|string|number} version
* @returns {boolean}
*/
ge(version) {
return this.eq(version) || this.gt(version);
}
/**
* Inclusive range comparison (version1 <= this <= version2).
* @param {SemVer|SemVerJson|string|number} version1
* @param {SemVer|SemVerJson|string|number} version2
* @returns boolean
*/
within(version1, version2) {
return this.ge(version1) && this.le(version2);
}
/**
* exclusive range comparison (version1 < this < version2)
* @param {SemVer|SemVerJson|string|number} version1
* @param {SemVer|SemVerJson|string|number} version2
* @returns boolean
*/
between(version1, version2) {
return this.gt(version1) && this.lt(version2);
}
valueOf() {
return this.major * 1000000 + this.minor * 1000 + this.patch;
}
toString() {
return this.original;
}
}
module.exports = SemVer;