Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: handle comparison of two numeric pre releases #510

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/utils/__tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@
expect(versionGreaterOrEqualThan(v2, v1)).toBe(true);
});

test('can compare pre parts', () => {
const v1 = parseVersion('1.2.3-1')!;

Check warning on line 186 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
const v2 = parseVersion('1.2.3-2')!;

Check warning on line 187 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
expect(versionGreaterOrEqualThan(v1, v2)).toBe(false);
expect(versionGreaterOrEqualThan(v2, v1)).toBe(true);
});

test('throws an exception if there are build parts', () => {
const v1 = semVerFactory(0, 1, 2, undefined, 'build123');
const v2 = semVerFactory(0, 1, 2);
Expand Down
17 changes: 7 additions & 10 deletions src/utils/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,17 @@ export function parseVersion(text: string): SemVer | null {
export function versionGreaterOrEqualThan(v1: SemVer, v2: SemVer): boolean {
if (v1.major !== v2.major) {
return v1.major > v2.major;
}
if (v1.minor !== v2.minor) {
} else if (v1.minor !== v2.minor) {
return v1.minor > v2.minor;
}
if (v1.patch !== v2.patch) {
} else if (v1.patch !== v2.patch) {
return v1.patch > v2.patch;
}
if (!v1.pre && v2.pre) {
} else if (!v1.pre && v2.pre) {
return true;
}
if (v1.pre && !v2.pre) {
} else if (v1.pre && !v2.pre) {
return false;
Comment on lines 82 to 91
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing these all to elif helped the type checker know what's up here

}
if (v1.build || v2.build || v1.pre || v2.pre) {
} else if (v1.pre && v2.pre && v1.pre !== v2.pre && /^\d+$/.test(v1.pre) && /^\d+$/.test(v2.pre)) {
return v1.pre > v2.pre;
} else if (v1.build || v2.build || v1.pre || v2.pre) {
throw new Error(
`Cannot compare the two versions: "${JSON.stringify(
v1
Expand Down
Loading