@@ -26,19 +26,26 @@ class Range {
26
26
this . loose = ! ! options . loose
27
27
this . includePrerelease = ! ! options . includePrerelease
28
28
29
- // First, split based on boolean or ||
29
+ // First reduce all whitespace as much as possible so we do not have to rely
30
+ // on potentially slow regexes like \s*. This is then stored and used for
31
+ // future error messages as well.
30
32
this . raw = range
31
- this . set = range
33
+ . trim ( )
34
+ . split ( / \s + / )
35
+ . join ( ' ' )
36
+
37
+ // First, split on ||
38
+ this . set = this . raw
32
39
. split ( '||' )
33
40
// map the range to a 2d array of comparators
34
- . map ( r => this . parseRange ( r . trim ( ) ) )
41
+ . map ( r => this . parseRange ( r ) )
35
42
// throw out any comparator lists that are empty
36
43
// this generally means that it was not a valid range, which is allowed
37
44
// in loose mode, but will still throw if the WHOLE range is invalid.
38
45
. filter ( c => c . length )
39
46
40
47
if ( ! this . set . length ) {
41
- throw new TypeError ( `Invalid SemVer Range: ${ range } ` )
48
+ throw new TypeError ( `Invalid SemVer Range: ${ this . raw } ` )
42
49
}
43
50
44
51
// if we have any that are not the null set, throw out null sets.
@@ -64,9 +71,7 @@ class Range {
64
71
65
72
format ( ) {
66
73
this . range = this . set
67
- . map ( ( comps ) => {
68
- return comps . join ( ' ' ) . trim ( )
69
- } )
74
+ . map ( ( comps ) => comps . join ( ' ' ) . trim ( ) )
70
75
. join ( '||' )
71
76
. trim ( )
72
77
return this . range
@@ -77,8 +82,6 @@ class Range {
77
82
}
78
83
79
84
parseRange ( range ) {
80
- range = range . trim ( )
81
-
82
85
// memoize range parsing for performance.
83
86
// this is a very hot path, and fully deterministic.
84
87
const memoOpts =
@@ -105,9 +108,6 @@ class Range {
105
108
// `^ 1.2.3` => `^1.2.3`
106
109
range = range . replace ( re [ t . CARETTRIM ] , caretTrimReplace )
107
110
108
- // normalize spaces
109
- range = range . split ( / \s + / ) . join ( ' ' )
110
-
111
111
// At this point, the range is completely trimmed and
112
112
// ready to be split into comparators.
113
113
@@ -203,7 +203,7 @@ const Comparator = require('./comparator')
203
203
const debug = require ( '../internal/debug' )
204
204
const SemVer = require ( './semver' )
205
205
const {
206
- re,
206
+ safeRe : re ,
207
207
t,
208
208
comparatorTrimReplace,
209
209
tildeTrimReplace,
@@ -257,10 +257,13 @@ const isX = id => !id || id.toLowerCase() === 'x' || id === '*'
257
257
// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
258
258
// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
259
259
// ~0.0.1 --> >=0.0.1 <0.1.0-0
260
- const replaceTildes = ( comp , options ) =>
261
- comp . trim ( ) . split ( / \s + / ) . map ( ( c ) => {
262
- return replaceTilde ( c , options )
263
- } ) . join ( ' ' )
260
+ const replaceTildes = ( comp , options ) => {
261
+ return comp
262
+ . trim ( )
263
+ . split ( / \s + / )
264
+ . map ( ( c ) => replaceTilde ( c , options ) )
265
+ . join ( ' ' )
266
+ }
264
267
265
268
const replaceTilde = ( comp , options ) => {
266
269
const r = options . loose ? re [ t . TILDELOOSE ] : re [ t . TILDE ]
@@ -298,10 +301,13 @@ const replaceTilde = (comp, options) => {
298
301
// ^1.2.0 --> >=1.2.0 <2.0.0-0
299
302
// ^0.0.1 --> >=0.0.1 <0.0.2-0
300
303
// ^0.1.0 --> >=0.1.0 <0.2.0-0
301
- const replaceCarets = ( comp , options ) =>
302
- comp . trim ( ) . split ( / \s + / ) . map ( ( c ) => {
303
- return replaceCaret ( c , options )
304
- } ) . join ( ' ' )
304
+ const replaceCarets = ( comp , options ) => {
305
+ return comp
306
+ . trim ( )
307
+ . split ( / \s + / )
308
+ . map ( ( c ) => replaceCaret ( c , options ) )
309
+ . join ( ' ' )
310
+ }
305
311
306
312
const replaceCaret = ( comp , options ) => {
307
313
debug ( 'caret' , comp , options )
@@ -358,9 +364,10 @@ const replaceCaret = (comp, options) => {
358
364
359
365
const replaceXRanges = ( comp , options ) => {
360
366
debug ( 'replaceXRanges' , comp , options )
361
- return comp . split ( / \s + / ) . map ( ( c ) => {
362
- return replaceXRange ( c , options )
363
- } ) . join ( ' ' )
367
+ return comp
368
+ . split ( / \s + / )
369
+ . map ( ( c ) => replaceXRange ( c , options ) )
370
+ . join ( ' ' )
364
371
}
365
372
366
373
const replaceXRange = ( comp , options ) => {
@@ -443,12 +450,15 @@ const replaceXRange = (comp, options) => {
443
450
const replaceStars = ( comp , options ) => {
444
451
debug ( 'replaceStars' , comp , options )
445
452
// Looseness is ignored here. star is always as loose as it gets!
446
- return comp . trim ( ) . replace ( re [ t . STAR ] , '' )
453
+ return comp
454
+ . trim ( )
455
+ . replace ( re [ t . STAR ] , '' )
447
456
}
448
457
449
458
const replaceGTE0 = ( comp , options ) => {
450
459
debug ( 'replaceGTE0' , comp , options )
451
- return comp . trim ( )
460
+ return comp
461
+ . trim ( )
452
462
. replace ( re [ options . includePrerelease ? t . GTE0PRE : t . GTE0 ] , '' )
453
463
}
454
464
@@ -486,7 +496,7 @@ const hyphenReplace = incPr => ($0,
486
496
to = `<=${ to } `
487
497
}
488
498
489
- return ( `${ from } ${ to } ` ) . trim ( )
499
+ return `${ from } ${ to } ` . trim ( )
490
500
}
491
501
492
502
const testSet = ( set , version , options ) => {
0 commit comments