File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -35,6 +35,24 @@ describe("semver helpers", () => {
3535 expect ( compareSemverVersions ( "2.1.111-beta.1" , "2.1.111" ) ) . toBeLessThan ( 0 ) ;
3636 } ) ;
3737
38+ it ( "preserves hyphens within prerelease identifiers" , ( ) => {
39+ expect ( normalizeSemverVersion ( "20-alpha-1" ) ) . toBe ( "20.0.0-alpha-1" ) ;
40+ expect ( parseSemver ( "1.2.3-alpha-1.2" ) ) . toEqual ( {
41+ major : 1 ,
42+ minor : 2 ,
43+ patch : 3 ,
44+ prerelease : [ "alpha-1" , "2" ] ,
45+ } ) ;
46+ expect ( parseSemver ( "1.2.3-x-y-z.--" ) ?. prerelease ) . toEqual ( [ "x-y-z" , "--" ] ) ;
47+ } ) ;
48+
49+ it ( "distinguishes releases whose identifiers differ after a hyphen" , ( ) => {
50+ expect ( compareSemverVersions ( "1.2.3-alpha-1" , "1.2.3-alpha-2" ) ) . toBeLessThan ( 0 ) ;
51+ expect ( compareSemverVersions ( "1.2.3-alpha-2" , "1.2.3-alpha-1" ) ) . toBeGreaterThan ( 0 ) ;
52+ expect ( compareSemverVersions ( "1.2.3-alpha-1.2" , "1.2.3-alpha-1.10" ) ) . toBeLessThan ( 0 ) ;
53+ expect ( compareSemverVersions ( "1.2.3-alpha.1+build-2" , "1.2.3-alpha.1+build-3" ) ) . toBe ( 0 ) ;
54+ } ) ;
55+
3856 it ( "supports comparison comparators" , ( ) => {
3957 expect ( satisfiesSemverRange ( "24.9.0" , ">=24.0 <24.10" ) ) . toBe ( true ) ;
4058 expect ( satisfiesSemverRange ( "24.10.0" , ">=24.0 <24.10" ) ) . toBe ( false ) ;
Original file line number Diff line number Diff line change @@ -8,7 +8,9 @@ interface ParsedSemver {
88const SEMVER_NUMBER_SEGMENT = / ^ \d + $ / ;
99
1010export function normalizeSemverVersion ( version : string ) : string {
11- const [ main , prerelease ] = version . trim ( ) . split ( "-" , 2 ) ;
11+ const [ versionWithoutBuildMetadata = "" ] = version . trim ( ) . split ( "+" , 1 ) ;
12+ const [ main , ...prereleaseParts ] = versionWithoutBuildMetadata . split ( "-" ) ;
13+ const prerelease = prereleaseParts . join ( "-" ) ;
1214 const segments : string [ ] = [ ] ;
1315 for ( const segment of ( main ?? "" ) . split ( "." ) ) {
1416 const trimmed = segment . trim ( ) ;
@@ -26,7 +28,8 @@ export function normalizeSemverVersion(version: string): string {
2628
2729export function parseSemver ( value : string ) : ParsedSemver | null {
2830 const normalized = normalizeSemverVersion ( value ) . replace ( / ^ v / , "" ) ;
29- const [ main = "" , prerelease ] = normalized . split ( "-" , 2 ) ;
31+ const [ main = "" , ...prereleaseParts ] = normalized . split ( "-" ) ;
32+ const prerelease = prereleaseParts . join ( "-" ) ;
3033 const segments = main . split ( "." ) ;
3134 if ( segments . length !== 3 ) {
3235 return null ;
You can’t perform that action at this time.
0 commit comments