@@ -26,19 +26,26 @@ class Range {
2626 this . loose = ! ! options . loose
2727 this . includePrerelease = ! ! options . includePrerelease
2828
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.
3032 this . raw = range
31- this . set = range
33+ . trim ( )
34+ . split ( / \s + / )
35+ . join ( ' ' )
36+
37+ // First, split on ||
38+ this . set = this . raw
3239 . split ( '||' )
3340 // map the range to a 2d array of comparators
34- . map ( r => this . parseRange ( r . trim ( ) ) )
41+ . map ( r => this . parseRange ( r ) )
3542 // throw out any comparator lists that are empty
3643 // this generally means that it was not a valid range, which is allowed
3744 // in loose mode, but will still throw if the WHOLE range is invalid.
3845 . filter ( c => c . length )
3946
4047 if ( ! this . set . length ) {
41- throw new TypeError ( `Invalid SemVer Range: ${ range } ` )
48+ throw new TypeError ( `Invalid SemVer Range: ${ this . raw } ` )
4249 }
4350
4451 // if we have any that are not the null set, throw out null sets.
@@ -64,9 +71,7 @@ class Range {
6471
6572 format ( ) {
6673 this . range = this . set
67- . map ( ( comps ) => {
68- return comps . join ( ' ' ) . trim ( )
69- } )
74+ . map ( ( comps ) => comps . join ( ' ' ) . trim ( ) )
7075 . join ( '||' )
7176 . trim ( )
7277 return this . range
@@ -77,8 +82,6 @@ class Range {
7782 }
7883
7984 parseRange ( range ) {
80- range = range . trim ( )
81-
8285 // memoize range parsing for performance.
8386 // this is a very hot path, and fully deterministic.
8487 const memoOpts =
@@ -105,9 +108,6 @@ class Range {
105108 // `^ 1.2.3` => `^1.2.3`
106109 range = range . replace ( re [ t . CARETTRIM ] , caretTrimReplace )
107110
108- // normalize spaces
109- range = range . split ( / \s + / ) . join ( ' ' )
110-
111111 // At this point, the range is completely trimmed and
112112 // ready to be split into comparators.
113113
@@ -203,7 +203,7 @@ const Comparator = require('./comparator')
203203const debug = require ( '../internal/debug' )
204204const SemVer = require ( './semver' )
205205const {
206- re,
206+ safeRe : re ,
207207 t,
208208 comparatorTrimReplace,
209209 tildeTrimReplace,
@@ -257,10 +257,13 @@ const isX = id => !id || id.toLowerCase() === 'x' || id === '*'
257257// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
258258// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
259259// ~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+ }
264267
265268const replaceTilde = ( comp , options ) => {
266269 const r = options . loose ? re [ t . TILDELOOSE ] : re [ t . TILDE ]
@@ -298,10 +301,13 @@ const replaceTilde = (comp, options) => {
298301// ^1.2.0 --> >=1.2.0 <2.0.0-0
299302// ^0.0.1 --> >=0.0.1 <0.0.2-0
300303// ^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+ }
305311
306312const replaceCaret = ( comp , options ) => {
307313 debug ( 'caret' , comp , options )
@@ -358,9 +364,10 @@ const replaceCaret = (comp, options) => {
358364
359365const replaceXRanges = ( comp , options ) => {
360366 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 ( ' ' )
364371}
365372
366373const replaceXRange = ( comp , options ) => {
@@ -443,12 +450,15 @@ const replaceXRange = (comp, options) => {
443450const replaceStars = ( comp , options ) => {
444451 debug ( 'replaceStars' , comp , options )
445452 // 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 ] , '' )
447456}
448457
449458const replaceGTE0 = ( comp , options ) => {
450459 debug ( 'replaceGTE0' , comp , options )
451- return comp . trim ( )
460+ return comp
461+ . trim ( )
452462 . replace ( re [ options . includePrerelease ? t . GTE0PRE : t . GTE0 ] , '' )
453463}
454464
@@ -486,7 +496,7 @@ const hyphenReplace = incPr => ($0,
486496 to = `<=${ to } `
487497 }
488498
489- return ( `${ from } ${ to } ` ) . trim ( )
499+ return `${ from } ${ to } ` . trim ( )
490500}
491501
492502const testSet = ( set , version , options ) => {
0 commit comments