Skip to content

Commit fb4b650

Browse files
committed
Add shortenVersionString util method
1 parent 33f8c31 commit fb4b650

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/common/utils/pep440Version.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ export class PEP440Version {
149149
return this.release.length > 2 ? this.release[2] : 0;
150150
}
151151

152+
/**
153+
* Returns a short display string: "X.Y.Z" if micro is present, otherwise "X.Y.x".
154+
* Parses `input` first; returns it unchanged if not a valid version.
155+
*/
156+
public static shortenVersionString(input: string): string {
157+
const v = PEP440Version.parse(input);
158+
if (!v) {
159+
return input;
160+
}
161+
return v.release.length >= 3 ? `${v.major}.${v.minor}.${v.micro}` : `${v.major}.${v.minor}.x`;
162+
}
163+
152164
/** Whether this version is a pre-release (has pre or dev segment). */
153165
public get isPreRelease(): boolean {
154166
return this.pre !== undefined || this.dev !== undefined;

0 commit comments

Comments
 (0)