source: node_modules/semver/classes/semver.js@ 2d1ec46

main
Last change on this file since 2d1ec46 was 6c7cfa6, checked in by Klimentina Efremova <klimentina08642@…>, 9 days ago

Implemented Advaced database reports

  • Property mode set to 100644
File size: 9.7 KB
Line 
1'use strict'
2
3const debug = require('../internal/debug')
4const { MAX_LENGTH, MAX_SAFE_INTEGER } = require('../internal/constants')
5const { safeRe: re, t } = require('../internal/re')
6
7const parseOptions = require('../internal/parse-options')
8const { compareIdentifiers } = require('../internal/identifiers')
9
10const isPrereleaseIdentifier = (prerelease, identifier) => {
11 const identifiers = identifier.split('.')
12 if (identifiers.length > prerelease.length) {
13 return false
14 }
15
16 for (let i = 0; i < identifiers.length; i++) {
17 if (compareIdentifiers(prerelease[i], identifiers[i]) !== 0) {
18 return false
19 }
20 }
21
22 return true
23}
24
25class SemVer {
26 constructor (version, options) {
27 options = parseOptions(options)
28
29 if (version instanceof SemVer) {
30 if (version.loose === !!options.loose &&
31 version.includePrerelease === !!options.includePrerelease) {
32 return version
33 } else {
34 version = version.version
35 }
36 } else if (typeof version !== 'string') {
37 throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`)
38 }
39
40 if (version.length > MAX_LENGTH) {
41 throw new TypeError(
42 `version is longer than ${MAX_LENGTH} characters`
43 )
44 }
45
46 debug('SemVer', version, options)
47 this.options = options
48 this.loose = !!options.loose
49 // this isn't actually relevant for versions, but keep it so that we
50 // don't run into trouble passing this.options around.
51 this.includePrerelease = !!options.includePrerelease
52
53 const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL])
54
55 if (!m) {
56 throw new TypeError(`Invalid Version: ${version}`)
57 }
58
59 this.raw = version
60
61 // these are actually numbers
62 this.major = +m[1]
63 this.minor = +m[2]
64 this.patch = +m[3]
65
66 if (this.major > MAX_SAFE_INTEGER || this.major < 0) {
67 throw new TypeError('Invalid major version')
68 }
69
70 if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) {
71 throw new TypeError('Invalid minor version')
72 }
73
74 if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) {
75 throw new TypeError('Invalid patch version')
76 }
77
78 // numberify any prerelease numeric ids
79 if (!m[4]) {
80 this.prerelease = []
81 } else {
82 this.prerelease = m[4].split('.').map((id) => {
83 if (/^[0-9]+$/.test(id)) {
84 const num = +id
85 if (num >= 0 && num < MAX_SAFE_INTEGER) {
86 return num
87 }
88 }
89 return id
90 })
91 }
92
93 this.build = m[5] ? m[5].split('.') : []
94 this.format()
95 }
96
97 format () {
98 this.version = `${this.major}.${this.minor}.${this.patch}`
99 if (this.prerelease.length) {
100 this.version += `-${this.prerelease.join('.')}`
101 }
102 return this.version
103 }
104
105 toString () {
106 return this.version
107 }
108
109 compare (other) {
110 debug('SemVer.compare', this.version, this.options, other)
111 if (!(other instanceof SemVer)) {
112 if (typeof other === 'string' && other === this.version) {
113 return 0
114 }
115 other = new SemVer(other, this.options)
116 }
117
118 if (other.version === this.version) {
119 return 0
120 }
121
122 return this.compareMain(other) || this.comparePre(other)
123 }
124
125 compareMain (other) {
126 if (!(other instanceof SemVer)) {
127 other = new SemVer(other, this.options)
128 }
129
130 if (this.major < other.major) {
131 return -1
132 }
133 if (this.major > other.major) {
134 return 1
135 }
136 if (this.minor < other.minor) {
137 return -1
138 }
139 if (this.minor > other.minor) {
140 return 1
141 }
142 if (this.patch < other.patch) {
143 return -1
144 }
145 if (this.patch > other.patch) {
146 return 1
147 }
148 return 0
149 }
150
151 comparePre (other) {
152 if (!(other instanceof SemVer)) {
153 other = new SemVer(other, this.options)
154 }
155
156 // NOT having a prerelease is > having one
157 if (this.prerelease.length && !other.prerelease.length) {
158 return -1
159 } else if (!this.prerelease.length && other.prerelease.length) {
160 return 1
161 } else if (!this.prerelease.length && !other.prerelease.length) {
162 return 0
163 }
164
165 let i = 0
166 do {
167 const a = this.prerelease[i]
168 const b = other.prerelease[i]
169 debug('prerelease compare', i, a, b)
170 if (a === undefined && b === undefined) {
171 return 0
172 } else if (b === undefined) {
173 return 1
174 } else if (a === undefined) {
175 return -1
176 } else if (a === b) {
177 continue
178 } else {
179 return compareIdentifiers(a, b)
180 }
181 } while (++i)
182 }
183
184 compareBuild (other) {
185 if (!(other instanceof SemVer)) {
186 other = new SemVer(other, this.options)
187 }
188
189 let i = 0
190 do {
191 const a = this.build[i]
192 const b = other.build[i]
193 debug('build compare', i, a, b)
194 if (a === undefined && b === undefined) {
195 return 0
196 } else if (b === undefined) {
197 return 1
198 } else if (a === undefined) {
199 return -1
200 } else if (a === b) {
201 continue
202 } else {
203 return compareIdentifiers(a, b)
204 }
205 } while (++i)
206 }
207
208 // preminor will bump the version up to the next minor release, and immediately
209 // down to pre-release. premajor and prepatch work the same way.
210 inc (release, identifier, identifierBase) {
211 if (release.startsWith('pre')) {
212 if (!identifier && identifierBase === false) {
213 throw new Error('invalid increment argument: identifier is empty')
214 }
215 // Avoid an invalid semver results
216 if (identifier) {
217 const match = `-${identifier}`.match(this.options.loose ? re[t.PRERELEASELOOSE] : re[t.PRERELEASE])
218 if (!match || match[1] !== identifier) {
219 throw new Error(`invalid identifier: ${identifier}`)
220 }
221 }
222 }
223
224 switch (release) {
225 case 'premajor':
226 this.prerelease.length = 0
227 this.patch = 0
228 this.minor = 0
229 this.major++
230 this.inc('pre', identifier, identifierBase)
231 break
232 case 'preminor':
233 this.prerelease.length = 0
234 this.patch = 0
235 this.minor++
236 this.inc('pre', identifier, identifierBase)
237 break
238 case 'prepatch':
239 // If this is already a prerelease, it will bump to the next version
240 // drop any prereleases that might already exist, since they are not
241 // relevant at this point.
242 this.prerelease.length = 0
243 this.inc('patch', identifier, identifierBase)
244 this.inc('pre', identifier, identifierBase)
245 break
246 // If the input is a non-prerelease version, this acts the same as
247 // prepatch.
248 case 'prerelease':
249 if (this.prerelease.length === 0) {
250 this.inc('patch', identifier, identifierBase)
251 }
252 this.inc('pre', identifier, identifierBase)
253 break
254 case 'release':
255 if (this.prerelease.length === 0) {
256 throw new Error(`version ${this.raw} is not a prerelease`)
257 }
258 this.prerelease.length = 0
259 break
260
261 case 'major':
262 // If this is a pre-major version, bump up to the same major version.
263 // Otherwise increment major.
264 // 1.0.0-5 bumps to 1.0.0
265 // 1.1.0 bumps to 2.0.0
266 if (
267 this.minor !== 0 ||
268 this.patch !== 0 ||
269 this.prerelease.length === 0
270 ) {
271 this.major++
272 }
273 this.minor = 0
274 this.patch = 0
275 this.prerelease = []
276 break
277 case 'minor':
278 // If this is a pre-minor version, bump up to the same minor version.
279 // Otherwise increment minor.
280 // 1.2.0-5 bumps to 1.2.0
281 // 1.2.1 bumps to 1.3.0
282 if (this.patch !== 0 || this.prerelease.length === 0) {
283 this.minor++
284 }
285 this.patch = 0
286 this.prerelease = []
287 break
288 case 'patch':
289 // If this is not a pre-release version, it will increment the patch.
290 // If it is a pre-release it will bump up to the same patch version.
291 // 1.2.0-5 patches to 1.2.0
292 // 1.2.0 patches to 1.2.1
293 if (this.prerelease.length === 0) {
294 this.patch++
295 }
296 this.prerelease = []
297 break
298 // This probably shouldn't be used publicly.
299 // 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction.
300 case 'pre': {
301 const base = Number(identifierBase) ? 1 : 0
302
303 if (this.prerelease.length === 0) {
304 this.prerelease = [base]
305 } else {
306 let i = this.prerelease.length
307 while (--i >= 0) {
308 if (typeof this.prerelease[i] === 'number') {
309 this.prerelease[i]++
310 i = -2
311 }
312 }
313 if (i === -1) {
314 // didn't increment anything
315 if (identifier === this.prerelease.join('.') && identifierBase === false) {
316 throw new Error('invalid increment argument: identifier already exists')
317 }
318 this.prerelease.push(base)
319 }
320 }
321 if (identifier) {
322 // 1.2.0-beta.1 bumps to 1.2.0-beta.2,
323 // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
324 let prerelease = [identifier, base]
325 if (identifierBase === false) {
326 prerelease = [identifier]
327 }
328 if (isPrereleaseIdentifier(this.prerelease, identifier)) {
329 const prereleaseBase = this.prerelease[identifier.split('.').length]
330 if (isNaN(prereleaseBase)) {
331 this.prerelease = prerelease
332 }
333 } else {
334 this.prerelease = prerelease
335 }
336 }
337 break
338 }
339 default:
340 throw new Error(`invalid increment argument: ${release}`)
341 }
342 this.raw = this.format()
343 if (this.build.length) {
344 this.raw += `+${this.build.join('.')}`
345 }
346 return this
347 }
348}
349
350module.exports = SemVer
Note: See TracBrowser for help on using the repository browser.