Skip to content

Commit f8e01f1

Browse files
committed
Refactoring
1 parent ab8daaf commit f8e01f1

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

src/common/pythonVersion.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
export type PythonReleaseLevel = 'alpha' | 'beta' | 'candidate' | 'final';
1+
type PythonReleaseLevel = 'alpha' | 'beta' | 'candidate' | 'final';
22

3-
const VERSION_PATTERN =
4-
/^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:(?:\.(alpha|beta|candidate|final)\.(\d+))|(?:(a|b|rc)(\d+)))?$/i;
3+
export class PythonVersion {
4+
private static readonly VERSION_PATTERN =
5+
/^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:(?:\.(alpha|beta|candidate|final)\.(\d+))|(?:(a|b|rc)(\d+)))?$/i;
56

6-
const releaseLevelOrder: Record<PythonReleaseLevel, number> = {
7-
alpha: 0,
8-
beta: 1,
9-
candidate: 2,
10-
final: 3,
11-
};
7+
private static readonly RELEASE_LEVEL_ALIASES: Readonly<Record<string, PythonReleaseLevel>> = {
8+
a: 'alpha',
9+
alpha: 'alpha',
10+
b: 'beta',
11+
beta: 'beta',
12+
rc: 'candidate',
13+
candidate: 'candidate',
14+
final: 'final',
15+
};
16+
17+
private static readonly RELEASE_LEVEL_ORDER: Readonly<Record<PythonReleaseLevel, number>> = {
18+
alpha: 0,
19+
beta: 1,
20+
candidate: 2,
21+
final: 3,
22+
};
1223

13-
export class PythonVersion {
1424
/**
1525
* Creates a normalized Python release version.
1626
*
@@ -22,15 +32,15 @@ export class PythonVersion {
2232
* @param version A Python release version.
2333
*/
2434
constructor(version: string) {
25-
const match = VERSION_PATTERN.exec(version.trim());
35+
const match = PythonVersion.VERSION_PATTERN.exec(version.trim());
2636
if (!match) {
2737
throw new TypeError(`Invalid Python version: ${version}`);
2838
}
2939

3040
this.major = parseNumericComponent(match[1], version);
3141
this.minor = parseNumericComponent(match[2], version);
3242
this.patch = parseNumericComponent(match[3], version);
33-
this.releaseLevel = normalizeReleaseLevel(match[4] ?? match[6]);
43+
this.releaseLevel = PythonVersion.normalizeReleaseLevel(match[4] ?? match[6]);
3444
this.releaseSerial = parseNumericComponent(match[5] ?? match[7], version);
3545
}
3646

@@ -70,7 +80,10 @@ export class PythonVersion {
7080
compareNumbers(this.major, other.major) ||
7181
compareNumbers(this.minor, other.minor) ||
7282
compareNumbers(this.patch, other.patch) ||
73-
compareNumbers(releaseLevelOrder[this.releaseLevel], releaseLevelOrder[other.releaseLevel]) ||
83+
compareNumbers(
84+
PythonVersion.RELEASE_LEVEL_ORDER[this.releaseLevel],
85+
PythonVersion.RELEASE_LEVEL_ORDER[other.releaseLevel],
86+
) ||
7487
compareNumbers(this.releaseSerial, other.releaseSerial)
7588
);
7689
}
@@ -89,6 +102,10 @@ export class PythonVersion {
89102
return release;
90103
}
91104
}
105+
106+
private static normalizeReleaseLevel(value: string | undefined): PythonReleaseLevel {
107+
return value ? (PythonVersion.RELEASE_LEVEL_ALIASES[value.toLowerCase()] ?? 'final') : 'final';
108+
}
92109
}
93110

94111
function parseNumericComponent(value: string | undefined, version: string): number {
@@ -102,19 +119,3 @@ function parseNumericComponent(value: string | undefined, version: string): numb
102119
function compareNumbers(left: number, right: number): number {
103120
return left === right ? 0 : left < right ? -1 : 1;
104121
}
105-
106-
function normalizeReleaseLevel(value: string | undefined): PythonReleaseLevel {
107-
switch (value?.toLowerCase()) {
108-
case 'a':
109-
case 'alpha':
110-
return 'alpha';
111-
case 'b':
112-
case 'beta':
113-
return 'beta';
114-
case 'rc':
115-
case 'candidate':
116-
return 'candidate';
117-
default:
118-
return 'final';
119-
}
120-
}

0 commit comments

Comments
 (0)