semver 6.1.2 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +83 -14
  3. package/bin/semver.js +173 -0
  4. package/classes/comparator.js +139 -0
  5. package/classes/index.js +5 -0
  6. package/classes/range.js +448 -0
  7. package/classes/semver.js +290 -0
  8. package/functions/clean.js +6 -0
  9. package/functions/cmp.js +48 -0
  10. package/functions/coerce.js +51 -0
  11. package/functions/compare-build.js +7 -0
  12. package/functions/compare-loose.js +3 -0
  13. package/functions/compare.js +5 -0
  14. package/functions/diff.js +25 -0
  15. package/functions/eq.js +3 -0
  16. package/functions/gt.js +3 -0
  17. package/functions/gte.js +3 -0
  18. package/functions/inc.js +15 -0
  19. package/functions/lt.js +3 -0
  20. package/functions/lte.js +3 -0
  21. package/functions/major.js +3 -0
  22. package/functions/minor.js +3 -0
  23. package/functions/neq.js +3 -0
  24. package/functions/parse.js +37 -0
  25. package/functions/patch.js +3 -0
  26. package/functions/prerelease.js +6 -0
  27. package/functions/rcompare.js +3 -0
  28. package/functions/rsort.js +3 -0
  29. package/functions/satisfies.js +10 -0
  30. package/functions/sort.js +3 -0
  31. package/functions/valid.js +6 -0
  32. package/index.js +64 -0
  33. package/internal/constants.js +17 -0
  34. package/internal/debug.js +9 -0
  35. package/internal/identifiers.js +23 -0
  36. package/internal/re.js +179 -0
  37. package/package.json +12 -6
  38. package/ranges/gtr.js +4 -0
  39. package/ranges/intersects.js +7 -0
  40. package/ranges/ltr.js +4 -0
  41. package/ranges/max-satisfying.js +25 -0
  42. package/ranges/min-satisfying.js +24 -0
  43. package/ranges/min-version.js +57 -0
  44. package/ranges/outside.js +80 -0
  45. package/ranges/to-comparators.js +8 -0
  46. package/ranges/valid.js +11 -0
  47. package/bin/semver +0 -160
  48. package/semver.js +0 -1552
package/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # changes log
2
2
 
3
+ ## 6.3.0
4
+
5
+ * Expose the token enum on the exports
6
+
7
+ ## 6.2.0
8
+
9
+ * Coerce numbers to strings when passed to semver.coerce()
10
+ * Add `rtl` option to coerce from right to left
11
+
12
+ ## 6.1.3
13
+
14
+ * Handle X-ranges properly in includePrerelease mode
15
+
16
+ ## 6.1.2
17
+
18
+ * Do not throw when testing invalid version strings
19
+
20
+ ## 6.1.1
21
+
22
+ * Add options support for semver.coerce()
23
+ * Handle undefined version passed to Range.test
24
+
25
+ ## 6.1.0
26
+
27
+ * Add semver.compareBuild function
28
+ * Support `*` in semver.intersects
29
+
3
30
  ## 6.0
4
31
 
5
32
  * Fix `intersects` logic.
package/README.md CHANGED
@@ -60,6 +60,12 @@ Options:
60
60
  Coerce a string into SemVer if possible
61
61
  (does not imply --loose)
62
62
 
63
+ --rtl
64
+ Coerce version strings right to left
65
+
66
+ --ltr
67
+ Coerce version strings left to right (default)
68
+
63
69
  Program exits successfully if any valid version satisfies
64
70
  all supplied ranges, and prints all satisfying versions.
65
71
 
@@ -399,25 +405,34 @@ range, use the `satisfies(version, range)` function.
399
405
 
400
406
  ### Coercion
401
407
 
402
- * `coerce(version)`: Coerces a string to semver if possible
403
-
404
- This aims to provide a very forgiving translation of a non-semver
405
- string to semver. It looks for the first digit in a string, and
406
- consumes all remaining characters which satisfy at least a partial semver
407
- (e.g., `1`, `1.2`, `1.2.3`) up to the max permitted length (256 characters).
408
- Longer versions are simply truncated (`4.6.3.9.2-alpha2` becomes `4.6.3`).
409
- All surrounding text is simply ignored (`v3.4 replaces v3.3.1` becomes `3.4.0`).
410
- Only text which lacks digits will fail coercion (`version one` is not valid).
411
- The maximum length for any semver component considered for coercion is 16 characters;
412
- longer components will be ignored (`10000000000000000.4.7.4` becomes `4.7.4`).
413
- The maximum value for any semver component is `Integer.MAX_SAFE_INTEGER || (2**53 - 1)`;
414
- higher value components are invalid (`9999999999999999.4.7.4` is likely invalid).
408
+ * `coerce(version, options)`: Coerces a string to semver if possible
409
+
410
+ This aims to provide a very forgiving translation of a non-semver string to
411
+ semver. It looks for the first digit in a string, and consumes all
412
+ remaining characters which satisfy at least a partial semver (e.g., `1`,
413
+ `1.2`, `1.2.3`) up to the max permitted length (256 characters). Longer
414
+ versions are simply truncated (`4.6.3.9.2-alpha2` becomes `4.6.3`). All
415
+ surrounding text is simply ignored (`v3.4 replaces v3.3.1` becomes
416
+ `3.4.0`). Only text which lacks digits will fail coercion (`version one`
417
+ is not valid). The maximum length for any semver component considered for
418
+ coercion is 16 characters; longer components will be ignored
419
+ (`10000000000000000.4.7.4` becomes `4.7.4`). The maximum value for any
420
+ semver component is `Number.MAX_SAFE_INTEGER || (2**53 - 1)`; higher value
421
+ components are invalid (`9999999999999999.4.7.4` is likely invalid).
422
+
423
+ If the `options.rtl` flag is set, then `coerce` will return the right-most
424
+ coercible tuple that does not share an ending index with a longer coercible
425
+ tuple. For example, `1.2.3.4` will return `2.3.4` in rtl mode, not
426
+ `4.0.0`. `1.2.3/4` will return `4.0.0`, because the `4` is not a part of
427
+ any other overlapping SemVer tuple.
415
428
 
416
429
  ### Clean
417
430
 
418
431
  * `clean(version)`: Clean a string to be a valid semver if possible
419
432
 
420
- This will return a cleaned and trimmed semver version. If the provided version is not valid a null will be returned. This does not work for ranges.
433
+ This will return a cleaned and trimmed semver version. If the provided
434
+ version is not valid a null will be returned. This does not work for
435
+ ranges.
421
436
 
422
437
  ex.
423
438
  * `s.clean(' = v 2.1.5foo')`: `null`
@@ -428,3 +443,57 @@ ex.
428
443
  * `s.clean(' =v2.1.5')`: `2.1.5`
429
444
  * `s.clean(' 2.1.5 ')`: `'2.1.5'`
430
445
  * `s.clean('~1.0.0')`: `null`
446
+
447
+ ## Exported Modules
448
+
449
+ <!--
450
+ TODO: Make sure that all of these items are documented (classes aren't,
451
+ eg), and then pull the module name into the documentation for that specific
452
+ thing.
453
+ -->
454
+
455
+ You may pull in just the part of this semver utility that you need, if you
456
+ are sensitive to packing and tree-shaking concerns. The main
457
+ `require('semver')` export uses getter functions to lazily load the parts
458
+ of the API that are used.
459
+
460
+ The following modules are available:
461
+
462
+ * `require('semver')`
463
+ * `require('semver/classes')`
464
+ * `require('semver/classes/comparator')`
465
+ * `require('semver/classes/range')`
466
+ * `require('semver/classes/semver')`
467
+ * `require('semver/functions/clean')`
468
+ * `require('semver/functions/cmp')`
469
+ * `require('semver/functions/coerce')`
470
+ * `require('semver/functions/compare')`
471
+ * `require('semver/functions/compare-build')`
472
+ * `require('semver/functions/compare-loose')`
473
+ * `require('semver/functions/diff')`
474
+ * `require('semver/functions/eq')`
475
+ * `require('semver/functions/gt')`
476
+ * `require('semver/functions/gte')`
477
+ * `require('semver/functions/inc')`
478
+ * `require('semver/functions/lt')`
479
+ * `require('semver/functions/lte')`
480
+ * `require('semver/functions/major')`
481
+ * `require('semver/functions/minor')`
482
+ * `require('semver/functions/neq')`
483
+ * `require('semver/functions/parse')`
484
+ * `require('semver/functions/patch')`
485
+ * `require('semver/functions/prerelease')`
486
+ * `require('semver/functions/rcompare')`
487
+ * `require('semver/functions/rsort')`
488
+ * `require('semver/functions/satisfies')`
489
+ * `require('semver/functions/sort')`
490
+ * `require('semver/functions/valid')`
491
+ * `require('semver/ranges/gtr')`
492
+ * `require('semver/ranges/intersects')`
493
+ * `require('semver/ranges/ltr')`
494
+ * `require('semver/ranges/max-satisfying')`
495
+ * `require('semver/ranges/min-satisfying')`
496
+ * `require('semver/ranges/min-version')`
497
+ * `require('semver/ranges/outside')`
498
+ * `require('semver/ranges/to-comparators')`
499
+ * `require('semver/ranges/valid')`
package/bin/semver.js ADDED
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env node
2
+ // Standalone semver comparison program.
3
+ // Exits successfully and prints matching version(s) if
4
+ // any supplied version is valid and passes all tests.
5
+
6
+ const argv = process.argv.slice(2)
7
+
8
+ let versions = []
9
+
10
+ const range = []
11
+
12
+ let inc = null
13
+
14
+ const version = require('../package.json').version
15
+
16
+ let loose = false
17
+
18
+ let includePrerelease = false
19
+
20
+ let coerce = false
21
+
22
+ let rtl = false
23
+
24
+ let identifier
25
+
26
+ const semver = require('../')
27
+
28
+ let reverse = false
29
+
30
+ const options = {}
31
+
32
+ const main = () => {
33
+ if (!argv.length) return help()
34
+ while (argv.length) {
35
+ let a = argv.shift()
36
+ const indexOfEqualSign = a.indexOf('=')
37
+ if (indexOfEqualSign !== -1) {
38
+ a = a.slice(0, indexOfEqualSign)
39
+ argv.unshift(a.slice(indexOfEqualSign + 1))
40
+ }
41
+ switch (a) {
42
+ case '-rv': case '-rev': case '--rev': case '--reverse':
43
+ reverse = true
44
+ break
45
+ case '-l': case '--loose':
46
+ loose = true
47
+ break
48
+ case '-p': case '--include-prerelease':
49
+ includePrerelease = true
50
+ break
51
+ case '-v': case '--version':
52
+ versions.push(argv.shift())
53
+ break
54
+ case '-i': case '--inc': case '--increment':
55
+ switch (argv[0]) {
56
+ case 'major': case 'minor': case 'patch': case 'prerelease':
57
+ case 'premajor': case 'preminor': case 'prepatch':
58
+ inc = argv.shift()
59
+ break
60
+ default:
61
+ inc = 'patch'
62
+ break
63
+ }
64
+ break
65
+ case '--preid':
66
+ identifier = argv.shift()
67
+ break
68
+ case '-r': case '--range':
69
+ range.push(argv.shift())
70
+ break
71
+ case '-c': case '--coerce':
72
+ coerce = true
73
+ break
74
+ case '--rtl':
75
+ rtl = true
76
+ break
77
+ case '--ltr':
78
+ rtl = false
79
+ break
80
+ case '-h': case '--help': case '-?':
81
+ return help()
82
+ default:
83
+ versions.push(a)
84
+ break
85
+ }
86
+ }
87
+
88
+ const options = { loose: loose, includePrerelease: includePrerelease, rtl: rtl }
89
+
90
+ versions = versions.map((v) => {
91
+ return coerce ? (semver.coerce(v, options) || { version: v }).version : v
92
+ }).filter((v) => {
93
+ return semver.valid(v)
94
+ })
95
+ if (!versions.length) return fail()
96
+ if (inc && (versions.length !== 1 || range.length)) { return failInc() }
97
+
98
+ for (let i = 0, l = range.length; i < l; i++) {
99
+ versions = versions.filter((v) => {
100
+ return semver.satisfies(v, range[i], options)
101
+ })
102
+ if (!versions.length) return fail()
103
+ }
104
+ return success(versions)
105
+ }
106
+
107
+
108
+ const failInc = () => {
109
+ console.error('--inc can only be used on a single version with no range')
110
+ fail()
111
+ }
112
+
113
+ const fail = () => process.exit(1)
114
+
115
+ const success = () => {
116
+ const compare = reverse ? 'rcompare' : 'compare'
117
+ versions.sort((a, b) => {
118
+ return semver[compare](a, b, options)
119
+ }).map((v) => {
120
+ return semver.clean(v, options)
121
+ }).map((v) => {
122
+ return inc ? semver.inc(v, inc, options, identifier) : v
123
+ }).forEach((v, i, _) => { console.log(v) })
124
+ }
125
+
126
+ const help = () => console.log(
127
+ `SemVer ${version}
128
+
129
+ A JavaScript implementation of the https://semver.org/ specification
130
+ Copyright Isaac Z. Schlueter
131
+
132
+ Usage: semver [options] <version> [<version> [...]]
133
+ Prints valid versions sorted by SemVer precedence
134
+
135
+ Options:
136
+ -r --range <range>
137
+ Print versions that match the specified range.
138
+
139
+ -i --increment [<level>]
140
+ Increment a version by the specified level. Level can
141
+ be one of: major, minor, patch, premajor, preminor,
142
+ prepatch, or prerelease. Default level is 'patch'.
143
+ Only one version may be specified.
144
+
145
+ --preid <identifier>
146
+ Identifier to be used to prefix premajor, preminor,
147
+ prepatch or prerelease version increments.
148
+
149
+ -l --loose
150
+ Interpret versions and ranges loosely
151
+
152
+ -p --include-prerelease
153
+ Always include prerelease versions in range matching
154
+
155
+ -c --coerce
156
+ Coerce a string into SemVer if possible
157
+ (does not imply --loose)
158
+
159
+ --rtl
160
+ Coerce version strings right to left
161
+
162
+ --ltr
163
+ Coerce version strings left to right (default)
164
+
165
+ Program exits successfully if any valid version satisfies
166
+ all supplied ranges, and prints all satisfying versions.
167
+
168
+ If no satisfying versions are found, then exits failure.
169
+
170
+ Versions are printed in ascending order, so supplying
171
+ multiple versions to the utility will just sort them.`)
172
+
173
+ main()
@@ -0,0 +1,139 @@
1
+ const ANY = Symbol('SemVer ANY')
2
+ // hoisted class for cyclic dependency
3
+ class Comparator {
4
+ static get ANY () {
5
+ return ANY
6
+ }
7
+ constructor (comp, options) {
8
+ if (!options || typeof options !== 'object') {
9
+ options = {
10
+ loose: !!options,
11
+ includePrerelease: false
12
+ }
13
+ }
14
+
15
+ if (comp instanceof Comparator) {
16
+ if (comp.loose === !!options.loose) {
17
+ return comp
18
+ } else {
19
+ comp = comp.value
20
+ }
21
+ }
22
+
23
+ debug('comparator', comp, options)
24
+ this.options = options
25
+ this.loose = !!options.loose
26
+ this.parse(comp)
27
+
28
+ if (this.semver === ANY) {
29
+ this.value = ''
30
+ } else {
31
+ this.value = this.operator + this.semver.version
32
+ }
33
+
34
+ debug('comp', this)
35
+ }
36
+
37
+ parse (comp) {
38
+ const r = this.options.loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
39
+ const m = comp.match(r)
40
+
41
+ if (!m) {
42
+ throw new TypeError(`Invalid comparator: ${comp}`)
43
+ }
44
+
45
+ this.operator = m[1] !== undefined ? m[1] : ''
46
+ if (this.operator === '=') {
47
+ this.operator = ''
48
+ }
49
+
50
+ // if it literally is just '>' or '' then allow anything.
51
+ if (!m[2]) {
52
+ this.semver = ANY
53
+ } else {
54
+ this.semver = new SemVer(m[2], this.options.loose)
55
+ }
56
+ }
57
+
58
+ toString () {
59
+ return this.value
60
+ }
61
+
62
+ test (version) {
63
+ debug('Comparator.test', version, this.options.loose)
64
+
65
+ if (this.semver === ANY || version === ANY) {
66
+ return true
67
+ }
68
+
69
+ if (typeof version === 'string') {
70
+ try {
71
+ version = new SemVer(version, this.options)
72
+ } catch (er) {
73
+ return false
74
+ }
75
+ }
76
+
77
+ return cmp(version, this.operator, this.semver, this.options)
78
+ }
79
+
80
+ intersects (comp, options) {
81
+ if (!(comp instanceof Comparator)) {
82
+ throw new TypeError('a Comparator is required')
83
+ }
84
+
85
+ if (!options || typeof options !== 'object') {
86
+ options = {
87
+ loose: !!options,
88
+ includePrerelease: false
89
+ }
90
+ }
91
+
92
+ if (this.operator === '') {
93
+ if (this.value === '') {
94
+ return true
95
+ }
96
+ return new Range(comp.value, options).test(this.value)
97
+ } else if (comp.operator === '') {
98
+ if (comp.value === '') {
99
+ return true
100
+ }
101
+ return new Range(this.value, options).test(comp.semver)
102
+ }
103
+
104
+ const sameDirectionIncreasing =
105
+ (this.operator === '>=' || this.operator === '>') &&
106
+ (comp.operator === '>=' || comp.operator === '>')
107
+ const sameDirectionDecreasing =
108
+ (this.operator === '<=' || this.operator === '<') &&
109
+ (comp.operator === '<=' || comp.operator === '<')
110
+ const sameSemVer = this.semver.version === comp.semver.version
111
+ const differentDirectionsInclusive =
112
+ (this.operator === '>=' || this.operator === '<=') &&
113
+ (comp.operator === '>=' || comp.operator === '<=')
114
+ const oppositeDirectionsLessThan =
115
+ cmp(this.semver, '<', comp.semver, options) &&
116
+ (this.operator === '>=' || this.operator === '>') &&
117
+ (comp.operator === '<=' || comp.operator === '<')
118
+ const oppositeDirectionsGreaterThan =
119
+ cmp(this.semver, '>', comp.semver, options) &&
120
+ (this.operator === '<=' || this.operator === '<') &&
121
+ (comp.operator === '>=' || comp.operator === '>')
122
+
123
+ return (
124
+ sameDirectionIncreasing ||
125
+ sameDirectionDecreasing ||
126
+ (sameSemVer && differentDirectionsInclusive) ||
127
+ oppositeDirectionsLessThan ||
128
+ oppositeDirectionsGreaterThan
129
+ )
130
+ }
131
+ }
132
+
133
+ module.exports = Comparator
134
+
135
+ const {re, t} = require('../internal/re')
136
+ const cmp = require('../functions/cmp')
137
+ const debug = require('../internal/debug')
138
+ const SemVer = require('./semver')
139
+ const Range = require('./range')
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ SemVer: require('./semver.js'),
3
+ Range: require('./range.js'),
4
+ Comparator: require('./comparator.js')
5
+ }