-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add detection of versions for tags (#21)
- Loading branch information
1 parent
fca8630
commit 4e294ad
Showing
5 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { compareSemVer } from '../compareSemVer' | ||
|
||
describe('compareSemVer', () => { | ||
test('it should correctly compare version numbers', () => { | ||
expect(compareSemVer('1.0.0', '1.0.0')).toBe(0) | ||
expect(compareSemVer('1.0.0', '0.9.9')).toBe(1) | ||
expect(compareSemVer('1.0.0', '1.0.1')).toBe(-1) | ||
expect(compareSemVer('1.2.3', '1.2')).toBe(1) | ||
expect(compareSemVer('1.0', '1.0.1')).toBe(-1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Compare two semantic version strings. | ||
* | ||
* @param version1 - The first version string | ||
* @param version2 - The second version string | ||
* | ||
* @returns -1 if version1 < version2, 0 if they're equal, and 1 if version1 > version2. | ||
*/ | ||
export function compareSemVer(version1: string, version2: string): number { | ||
const v1parts = version1.split('.').map(Number) | ||
const v2parts = version2.split('.').map(Number) | ||
|
||
for (let i = 0; i < v1parts.length; ++i) { | ||
if (v2parts.length === i) { | ||
return 1 | ||
} | ||
if (v1parts[i] === v2parts[i]) { | ||
continue | ||
} else if (v1parts[i] > v2parts[i]) { | ||
return 1 | ||
} else { | ||
return -1 | ||
} | ||
} | ||
return v1parts.length !== v2parts.length ? -1 : 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters