@@ -3879,35 +3879,43 @@ const coerce = (version, options) => {
38793879
38803880 let match = null
38813881 if ( ! options . rtl ) {
3882- match = version . match ( re [ t . COERCE ] )
3882+ match = version . match ( options . includePrerelease ? re [ t . COERCEFULL ] : re [ t . COERCE ] )
38833883 } else {
38843884 // Find the right-most coercible string that does not share
38853885 // a terminus with a more left-ward coercible string.
38863886 // Eg, '1.2.3.4' wants to coerce '2.3.4', not '3.4' or '4'
3887+ // With includePrerelease option set, '1.2.3.4-rc' wants to coerce '2.3.4-rc', not '2.3.4'
38873888 //
38883889 // Walk through the string checking with a /g regexp
38893890 // Manually set the index so as to pick up overlapping matches.
38903891 // Stop when we get a match that ends at the string end, since no
38913892 // coercible string can be more right-ward without the same terminus.
3893+ const coerceRtlRegex = options . includePrerelease ? re [ t . COERCERTLFULL ] : re [ t . COERCERTL ]
38923894 let next
3893- while ( ( next = re [ t . COERCERTL ] . exec ( version ) ) &&
3895+ while ( ( next = coerceRtlRegex . exec ( version ) ) &&
38943896 ( ! match || match . index + match [ 0 ] . length !== version . length )
38953897 ) {
38963898 if ( ! match ||
38973899 next . index + next [ 0 ] . length !== match . index + match [ 0 ] . length ) {
38983900 match = next
38993901 }
3900- re [ t . COERCERTL ] . lastIndex = next . index + next [ 1 ] . length + next [ 2 ] . length
3902+ coerceRtlRegex . lastIndex = next . index + next [ 1 ] . length + next [ 2 ] . length
39013903 }
39023904 // leave it in a clean state
3903- re [ t . COERCERTL ] . lastIndex = - 1
3905+ coerceRtlRegex . lastIndex = - 1
39043906 }
39053907
39063908 if ( match === null ) {
39073909 return null
39083910 }
39093911
3910- return parse ( `${ match [ 2 ] } .${ match [ 3 ] || '0' } .${ match [ 4 ] || '0' } ` , options )
3912+ const major = match [ 2 ]
3913+ const minor = match [ 3 ] || '0'
3914+ const patch = match [ 4 ] || '0'
3915+ const prerelease = options . includePrerelease && match [ 5 ] ? `-${ match [ 5 ] } ` : ''
3916+ const build = options . includePrerelease && match [ 6 ] ? `+${ match [ 6 ] } ` : ''
3917+
3918+ return parse ( `${ major } .${ minor } .${ patch } ${ prerelease } ${ build } ` , options )
39113919}
39123920module . exports = coerce
39133921
@@ -4599,12 +4607,17 @@ createToken('XRANGELOOSE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAINLOOSE]}$`)
45994607
46004608// Coercion.
46014609// Extract anything that could conceivably be a part of a valid semver
4602- createToken ( 'COERCE ' , `${ '(^|[^\\d])' +
4610+ createToken ( 'COERCEPLAIN ' , `${ '(^|[^\\d])' +
46034611 '(\\d{1,' } ${ MAX_SAFE_COMPONENT_LENGTH } })` +
46044612 `(?:\\.(\\d{1,${ MAX_SAFE_COMPONENT_LENGTH } }))?` +
4605- `(?:\\.(\\d{1,${ MAX_SAFE_COMPONENT_LENGTH } }))?` +
4613+ `(?:\\.(\\d{1,${ MAX_SAFE_COMPONENT_LENGTH } }))?` )
4614+ createToken ( 'COERCE' , `${ src [ t . COERCEPLAIN ] } (?:$|[^\\d])` )
4615+ createToken ( 'COERCEFULL' , src [ t . COERCEPLAIN ] +
4616+ `(?:${ src [ t . PRERELEASE ] } )?` +
4617+ `(?:${ src [ t . BUILD ] } )?` +
46064618 `(?:$|[^\\d])` )
46074619createToken ( 'COERCERTL' , src [ t . COERCE ] , true )
4620+ createToken ( 'COERCERTLFULL' , src [ t . COERCEFULL ] , true )
46084621
46094622// Tilde ranges.
46104623// Meaning is "reasonably at or greater than"
0 commit comments