-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: correctly sort bazel module versions (#145)
- Loading branch information
Showing
6 changed files
with
178 additions
and
58 deletions.
There are no files selected for viewing
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
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,72 @@ | ||
import { compareVersions } from "./version"; | ||
|
||
describe("compareVersions", () => { | ||
it("should sort semvers", () => { | ||
expect( | ||
[ | ||
"2.0.0", | ||
"1.0.0", | ||
"0.32.1", | ||
"0.32.11", | ||
"2.11.0", | ||
"1.0.0-rc1", | ||
"1.0.0-rc0", | ||
"1.0.0-rc23", | ||
"1.0.1-rc1", | ||
"2.10.1", | ||
].sort(compareVersions) | ||
).toEqual([ | ||
"0.32.1", | ||
"0.32.11", | ||
"1.0.0-rc0", | ||
"1.0.0-rc1", | ||
"1.0.0-rc23", | ||
"1.0.0", | ||
"1.0.1-rc1", | ||
"2.0.0", | ||
"2.10.1", | ||
"2.11.0", | ||
]); | ||
}); | ||
|
||
it("should sort versions with more than 3 components", () => { | ||
expect(["6.4.0.2", "6.4.0", "6.4.0.2-rc0"].sort(compareVersions)).toEqual([ | ||
"6.4.0", | ||
"6.4.0.2-rc0", | ||
"6.4.0.2", | ||
]); | ||
}); | ||
|
||
it("should sort duplciates", () => { | ||
expect(["1.0.0", "2.0.0", "1.0.0"].sort(compareVersions)).toEqual([ | ||
"1.0.0", | ||
"1.0.0", | ||
"2.0.0", | ||
]); | ||
}); | ||
|
||
it("should sort versions with non-numeric identifiers", () => { | ||
expect( | ||
["z", "b.aa.b", "a.ab.b-rcfoo", "a.ab.b", "a.ab.a", "a.aa.b", "x.y"].sort( | ||
compareVersions | ||
) | ||
).toEqual([ | ||
"a.aa.b", | ||
"a.ab.a", | ||
"a.ab.b-rcfoo", | ||
"a.ab.b", | ||
"b.aa.b", | ||
"x.y", | ||
"z", | ||
]); | ||
}); | ||
|
||
it("should sort numeric and non-numeric identifiers", () => { | ||
expect(["x.7.z", "1.2.3", "x.6.y", "a.b.c"].sort(compareVersions)).toEqual([ | ||
"1.2.3", | ||
"a.b.c", | ||
"x.6.y", | ||
"x.7.z", | ||
]); | ||
}); | ||
}); |
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,93 @@ | ||
/** | ||
* Compare bazel module versions | ||
* | ||
* Adapted from https://github.com/bazelbuild/bazel-central-registry/blob/127d91703baf4e39eb66fc907d255b37d6162792/tools/registry.py#L85 | ||
*/ | ||
export function compareVersions(a: string, b: string) { | ||
return Version.compare(new Version(a), new Version(b)); | ||
} | ||
|
||
class Version { | ||
public static compare(a: Version, b: Version): number { | ||
const result = Version.compareIdentifiers(a.release, b.release); | ||
if (result) { | ||
return result; | ||
} | ||
|
||
if (a.prerelease.length === 0) { | ||
return 1; | ||
} | ||
if (b.prerelease.length === 0) { | ||
return -1; | ||
} | ||
|
||
return Version.compareIdentifiers(a.prerelease, b.prerelease); | ||
} | ||
|
||
private static compareIdentifiers(a: Identifier[], b: Identifier[]) { | ||
const l = Math.min(a.length, b.length); | ||
for (let i = 0; i < l; i++) { | ||
const result = Identifier.compare(a[i], b[i]); | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
|
||
if (a.length > b.length) { | ||
return 1; | ||
} else if (b.length > a.length) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private readonly prerelease: Identifier[]; | ||
private readonly release: Identifier[]; | ||
|
||
public constructor(version: string) { | ||
const pattern = | ||
/^([a-zA-Z0-9.]+)(?:-([a-zA-Z0-9.-]+))?(?:\+[a-zA-Z0-9.-]+)?$/; | ||
const match = version.match(pattern); | ||
if (!match) { | ||
throw new Error(`Invalid module version '${version}'`); | ||
} | ||
|
||
this.release = this.convertToIdentifiers(match[1]); | ||
this.prerelease = this.convertToIdentifiers(match[2]); | ||
} | ||
|
||
private convertToIdentifiers(version: string): Identifier[] { | ||
return (version && version.split(".").map((i) => new Identifier(i))) || []; | ||
} | ||
} | ||
|
||
class Identifier { | ||
public static compare(a: Identifier, b: Identifier): number { | ||
if (typeof a.value !== typeof b.value) { | ||
if (typeof a.value === "number") { | ||
return -1; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
if (typeof a.value === "string") { | ||
if (a.value < b.value) { | ||
return -1; | ||
} else if (a.value === b.value) { | ||
return 0; | ||
} | ||
return 1; | ||
} else { | ||
return a.value - (b.value as number); | ||
} | ||
} | ||
|
||
private readonly value: string | number; | ||
|
||
public constructor(value: string) { | ||
const numeric = parseInt(value); | ||
this.value = isNaN(numeric) ? value : numeric; | ||
} | ||
} |
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