1414// "could not determine", which leaves the issue alone.
1515
1616const VERSION_RE =
17- / ^ v ? (?: ( \d + ) ! ) ? ( \d + (?: \. \d + ) * ) (?: [ - _ . ] ? ( a | a l p h a | b | b e t a | c | r c | p r e | p r e v i e w ) [ - _ . ] ? ( \d * ) ) ? (?: - ( \d + ) | [ - _ . ] ? (?: p o s t | r e v | r ) [ - _ . ] ? ( \d * ) ) ? (?: [ - _ . ] ? d e v [ - _ . ] ? ( \d * ) ) ? (?: \+ [ a - z 0 - 9 ] + (?: [ - _ . ] [ a - z 0 - 9 ] + ) * ) ? $ / i;
17+ / ^ v ? (?: ( \d + ) ! ) ? ( \d + (?: \. \d + ) * ) (?: [ - _ . ] ? ( a | a l p h a | b | b e t a | c | r c | p r e | p r e v i e w ) [ - _ . ] ? ( \d * ) ) ? (?: - ( \d + ) | [ - _ . ] ? (?: p o s t | r e v | r ) [ - _ . ] ? ( \d * ) ) ? (?: [ - _ . ] ? d e v [ - _ . ] ? ( \d * ) ) ? (?: \+ ( [ a - z 0 - 9 ] + (?: [ - _ . ] [ a - z 0 - 9 ] + ) * ) ) ? $ / i;
1818
1919const PRE_RANK = {
2020 a : 0 ,
@@ -29,14 +29,23 @@ const PRE_RANK = {
2929
3030/**
3131 * @param {string } version
32- * @returns {{epoch: number, release: number[], pre: [number, number] | null, post: number | null, dev: number | null} }
32+ * @returns {{epoch: number, release: number[], pre: [number, number] | null, post: number | null, dev: number | null, local: Array<number | string> | null } }
3333 * @throws on a version PEP 440 does not admit
3434 */
3535export function parseVersion ( version ) {
3636 const match = VERSION_RE . exec ( String ( version ) . trim ( ) ) ;
3737 if ( ! match ) throw new Error ( `not a PEP 440 version: "${ version } "` ) ;
38- const [ , epoch , release , preKind , preNum , postImplicit , postNum , devNum ] =
39- match ;
38+ const [
39+ ,
40+ epoch ,
41+ release ,
42+ preKind ,
43+ preNum ,
44+ postImplicit ,
45+ postNum ,
46+ devNum ,
47+ local ,
48+ ] = match ;
4049 const post =
4150 postImplicit !== undefined
4251 ? Number ( postImplicit )
@@ -51,9 +60,39 @@ export function parseVersion(version) {
5160 : null ,
5261 post,
5362 dev : devNum !== undefined ? Number ( devNum || 0 ) : null ,
63+ // Segments split on `.`, `-` or `_`: all digits compare as numbers, the
64+ // rest case-insensitively as text.
65+ local :
66+ local === undefined
67+ ? null
68+ : local
69+ . toLowerCase ( )
70+ . split ( / [ - _ . ] / )
71+ . map ( ( seg ) => ( / ^ \d + $ / . test ( seg ) ? Number ( seg ) : seg ) ) ,
5472 } ;
5573}
5674
75+ /**
76+ * PEP 440 local-label ordering: a version with a label sorts after the same
77+ * version without one; segment by segment a number beats text; and with a
78+ * matching prefix, more segments sort later.
79+ */
80+ function compareLocal ( a , b ) {
81+ if ( a === null || b === null ) return a === b ? 0 : a === null ? - 1 : 1 ;
82+ for ( let i = 0 ; i < Math . min ( a . length , b . length ) ; i ++ ) {
83+ const [ x , y ] = [ a [ i ] , b [ i ] ] ;
84+ if ( x === y ) continue ;
85+ if ( typeof x !== typeof y ) return typeof x === "number" ? 1 : - 1 ;
86+ return x < y ? - 1 : 1 ;
87+ }
88+ return a . length - b . length ;
89+ }
90+
91+ /** The public part of a version: everything before a `+local` label. */
92+ function publicVersion ( version ) {
93+ return String ( version ) . trim ( ) . replace ( / \+ .* $ / , "" ) ;
94+ }
95+
5796/** Is this a pre-release or development release? */
5897export function isPrerelease ( version ) {
5998 const v = parseVersion ( version ) ;
@@ -76,6 +115,7 @@ function sortKey(v) {
76115 : [ Infinity , 0 ] ;
77116 return {
78117 epoch : v . epoch ,
118+ local : v . local ,
79119 release,
80120 rest : [ ...pre , v . post ?? - Infinity , v . dev ?? Infinity ] ,
81121 } ;
@@ -97,7 +137,7 @@ export function compareVersions(a, b) {
97137 for ( let i = 0 ; i < x . rest . length ; i ++ ) {
98138 if ( x . rest [ i ] !== y . rest [ i ] ) return x . rest [ i ] < y . rest [ i ] ? - 1 : 1 ;
99139 }
100- return 0 ;
140+ return compareLocal ( x . local , y . local ) ;
101141}
102142
103143const CLAUSE_RE = / ^ ( ~ = | = = = | = = | ! = | < = | > = | < | > | = ) \s * ( \S + ) $ / ;
@@ -124,6 +164,14 @@ function clauseMatches(version, clause) {
124164 if ( ! match ) throw new Error ( `not a PEP 440 specifier clause: "${ clause } "` ) ;
125165 const [ , op , operand ] = match ;
126166 if ( op === "===" ) return String ( version ) . trim ( ) === operand ;
167+ // Specifiers match on the public version: a candidate's local label is
168+ // ignored unless an equality operand names one itself (PEP 440, and what
169+ // `packaging` does), so `<=1.0` admits `1.0+vendor.1`.
170+ const keepLocal =
171+ ( op === "==" || op === "=" || op === "!=" ) &&
172+ ! operand . endsWith ( ".*" ) &&
173+ parseVersion ( operand ) . local !== null ;
174+ version = keepLocal ? String ( version ) . trim ( ) : publicVersion ( version ) ;
127175 if ( op === "==" || op === "=" || op === "!=" ) {
128176 const equal = operand . endsWith ( ".*" )
129177 ? prefixMatches ( version , operand . slice ( 0 , - 2 ) )
0 commit comments