1
1
// hoisted class for cyclic dependency
2
2
class Range {
3
3
constructor ( range , options ) {
4
- options = parseOptions ( options )
4
+ this . flagOptions = parseOptions ( options )
5
5
6
6
if ( range instanceof Range ) {
7
- if (
8
- range . loose === ! ! options . loose &&
9
- range . includePrerelease === ! ! options . includePrerelease
7
+ if ( hasFlag ( range . flagOptions , FLAG_loose ) === hasFlag ( this . flagOptions , FLAG_loose )
8
+ && hasFlag ( range . flagOptions , FLAG_includePrerelease ) === hasFlag ( this . flagOptions , FLAG_includePrerelease )
10
9
) {
10
+ // if ((range.flagOptions & ~FLAG_rtl) === (this.flagOptions & ~FLAG_rtl)) {
11
11
return range
12
12
} else {
13
- return new Range ( range . raw , options )
13
+ return new Range ( range . raw , this . flagOptions )
14
14
}
15
15
}
16
16
@@ -22,10 +22,6 @@ class Range {
22
22
return this
23
23
}
24
24
25
- this . options = options
26
- this . loose = ! ! options . loose
27
- this . includePrerelease = ! ! options . includePrerelease
28
-
29
25
// First, split based on boolean or ||
30
26
this . raw = range
31
27
this . set = range
@@ -62,6 +58,24 @@ class Range {
62
58
this . format ( )
63
59
}
64
60
61
+ get options ( ) {
62
+ return {
63
+ includePrerelease : hasFlag ( this . flagOptions , FLAG_includePrerelease ) ,
64
+ loose : hasFlag ( this . flagOptions , FLAG_loose ) ,
65
+ rtl : hasFlag ( this . flagOptions , FLAG_rtl ) ,
66
+ }
67
+ }
68
+
69
+ get loose ( ) {
70
+ return hasFlag ( this . flagOptions , FLAG_loose )
71
+ }
72
+
73
+ // this isn't actually relevant for versions, but keep it so that we
74
+ // don't run into trouble passing this.options around.
75
+ get includePrerelease ( ) {
76
+ return hasFlag ( this . flagOptions , FLAG_includePrerelease )
77
+ }
78
+
65
79
format ( ) {
66
80
this . range = this . set
67
81
. map ( ( comps ) => {
@@ -81,17 +95,17 @@ class Range {
81
95
82
96
// memoize range parsing for performance.
83
97
// this is a very hot path, and fully deterministic.
84
- const memoOpts = buildMemoKeyFromOptions ( this . options )
98
+ const memoOpts = this . flagOptions . toString ( )
85
99
const memoKey = memoOpts + range
86
100
const cached = cache . get ( memoKey )
87
101
if ( cached ) {
88
102
return cached
89
103
}
90
104
91
- const loose = this . options . loose
105
+ const loose = hasFlag ( this . flagOptions , FLAG_loose )
92
106
// `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
93
107
const hr = loose ? re [ t . HYPHENRANGELOOSE ] : re [ t . HYPHENRANGE ]
94
- range = range . replace ( hr , hyphenReplace ( this . options . includePrerelease ) )
108
+ range = range . replace ( hr , hyphenReplace ( hasFlag ( this . flagOptions , FLAG_includePrerelease ) ) )
95
109
debug ( 'hyphen replace' , range )
96
110
// `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
97
111
range = range . replace ( re [ t . COMPARATORTRIM ] , comparatorTrimReplace )
@@ -111,16 +125,16 @@ class Range {
111
125
112
126
let rangeList = range
113
127
. split ( ' ' )
114
- . map ( comp => parseComparator ( comp , this . options ) )
128
+ . map ( comp => parseComparator ( comp , this . flagOptions ) )
115
129
. join ( ' ' )
116
130
. split ( / \s + / )
117
131
// >=0.0.0 is equivalent to *
118
- . map ( comp => replaceGTE0 ( comp , this . options ) )
132
+ . map ( comp => replaceGTE0 ( comp , this . flagOptions ) )
119
133
120
134
if ( loose ) {
121
135
// in loose mode, throw out any that are not valid comparators
122
136
rangeList = rangeList . filter ( comp => {
123
- debug ( 'loose invalid filter' , comp , this . options )
137
+ debug ( 'loose invalid filter' , comp , this . flagOptions )
124
138
return ! ! comp . match ( re [ t . COMPARATORLOOSE ] )
125
139
} )
126
140
}
@@ -130,7 +144,7 @@ class Range {
130
144
// if more than one comparator, remove any * comparators
131
145
// also, don't include the same comparator more than once
132
146
const rangeMap = new Map ( )
133
- const comparators = rangeList . map ( comp => new Comparator ( comp , this . options ) )
147
+ const comparators = rangeList . map ( comp => new Comparator ( comp , this . flagOptions ) )
134
148
for ( const comp of comparators ) {
135
149
if ( isNullSet ( comp ) ) {
136
150
return [ comp ]
@@ -176,49 +190,21 @@ class Range {
176
190
177
191
if ( typeof version === 'string' ) {
178
192
try {
179
- version = new SemVer ( version , this . options )
193
+ version = new SemVer ( version , this . flagOptions )
180
194
} catch ( er ) {
181
195
return false
182
196
}
183
197
}
184
198
185
199
for ( let i = 0 ; i < this . set . length ; i ++ ) {
186
- if ( testSet ( this . set [ i ] , version , this . options ) ) {
200
+ if ( testSet ( this . set [ i ] , version , this . flagOptions ) ) {
187
201
return true
188
202
}
189
203
}
190
204
return false
191
205
}
192
206
}
193
207
194
- function buildMemoKeyFromOptions ( options ) {
195
- if ( options . includePrerelease === true ) {
196
- if ( options . loose === true && options . rtl === true ) {
197
- return '1' ;
198
- }
199
-
200
- if ( options . loose === true ) {
201
- return '2' ;
202
- }
203
-
204
- if ( options . rtl === true ) {
205
- return '3' ;
206
- }
207
-
208
- return '4' ;
209
- } else if ( options . loose === true ) {
210
- if ( options . rtl === true ) {
211
- return '5' ;
212
- }
213
-
214
- return '6' ;
215
- } else if ( options . rtl === true ) {
216
- return '7' ;
217
- } else {
218
- return '8' ;
219
- }
220
- }
221
-
222
208
module . exports = Range
223
209
224
210
const LRU = require ( 'lru-cache' )
@@ -235,6 +221,7 @@ const {
235
221
tildeTrimReplace,
236
222
caretTrimReplace,
237
223
} = require ( '../internal/re' )
224
+ const { FLAG_loose, FLAG_includePrerelease, FLAG_rtl, hasFlag } = require ( '../internal/constants' )
238
225
239
226
const isNullSet = c => c . value === '<0.0.0-0'
240
227
const isAny = c => c . value === ''
@@ -288,7 +275,7 @@ const replaceTildes = (comp, options) =>
288
275
} ) . join ( ' ' )
289
276
290
277
const replaceTilde = ( comp , options ) => {
291
- const r = options . loose ? re [ t . TILDELOOSE ] : re [ t . TILDE ]
278
+ const r = hasFlag ( options , FLAG_loose ) ? re [ t . TILDELOOSE ] : re [ t . TILDE ]
292
279
return comp . replace ( r , ( _ , M , m , p , pr ) => {
293
280
debug ( 'tilde' , comp , _ , M , m , p , pr )
294
281
let ret
@@ -330,8 +317,8 @@ const replaceCarets = (comp, options) =>
330
317
331
318
const replaceCaret = ( comp , options ) => {
332
319
debug ( 'caret' , comp , options )
333
- const r = options . loose ? re [ t . CARETLOOSE ] : re [ t . CARET ]
334
- const z = options . includePrerelease ? '-0' : ''
320
+ const r = hasFlag ( options , FLAG_loose ) ? re [ t . CARETLOOSE ] : re [ t . CARET ]
321
+ const z = hasFlag ( options , FLAG_includePrerelease ) ? '-0' : ''
335
322
return comp . replace ( r , ( _ , M , m , p , pr ) => {
336
323
debug ( 'caret' , comp , _ , M , m , p , pr )
337
324
let ret
@@ -390,7 +377,7 @@ const replaceXRanges = (comp, options) => {
390
377
391
378
const replaceXRange = ( comp , options ) => {
392
379
comp = comp . trim ( )
393
- const r = options . loose ? re [ t . XRANGELOOSE ] : re [ t . XRANGE ]
380
+ const r = hasFlag ( options , FLAG_loose ) ? re [ t . XRANGELOOSE ] : re [ t . XRANGE ]
394
381
return comp . replace ( r , ( ret , gtlt , M , m , p , pr ) => {
395
382
debug ( 'xRange' , comp , ret , gtlt , M , m , p , pr )
396
383
const xM = isX ( M )
@@ -404,7 +391,7 @@ const replaceXRange = (comp, options) => {
404
391
405
392
// if we're including prereleases in the match, then we need
406
393
// to fix this to -0, the lowest possible prerelease value
407
- pr = options . includePrerelease ? '-0' : ''
394
+ pr = hasFlag ( options , FLAG_includePrerelease ) ? '-0' : ''
408
395
409
396
if ( xM ) {
410
397
if ( gtlt === '>' || gtlt === '<' ) {
@@ -474,7 +461,7 @@ const replaceStars = (comp, options) => {
474
461
const replaceGTE0 = ( comp , options ) => {
475
462
debug ( 'replaceGTE0' , comp , options )
476
463
return comp . trim ( )
477
- . replace ( re [ options . includePrerelease ? t . GTE0PRE : t . GTE0 ] , '' )
464
+ . replace ( re [ hasFlag ( options , FLAG_loose ) ? t . GTE0PRE : t . GTE0 ] , '' )
478
465
}
479
466
480
467
// This function is passed to string.replace(re[t.HYPHENRANGE])
@@ -521,7 +508,7 @@ const testSet = (set, version, options) => {
521
508
}
522
509
}
523
510
524
- if ( version . prerelease . length && ! options . includePrerelease ) {
511
+ if ( version . prerelease . length && ! hasFlag ( options , FLAG_includePrerelease ) ) {
525
512
// Find the set of versions that are allowed to have prereleases
526
513
// For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
527
514
// That should allow `1.2.3-pr.2` to pass.
0 commit comments