web: Compare build ident if all else fails, when finding best versions

This commit is contained in:
Nathan Adams 2023-08-05 00:37:08 +02:00
parent ee1e8ec267
commit 64d951099d
2 changed files with 56 additions and 41 deletions

View File

@ -18,7 +18,6 @@ export class Version {
private readonly minor: number, private readonly minor: number,
private readonly patch: number, private readonly patch: number,
private readonly prIdent: string[] | null, private readonly prIdent: string[] | null,
// @ts-expect-error: Property 'buildIdent' is declared but its value is never read.
private readonly buildIdent: string[] | null, private readonly buildIdent: string[] | null,
) {} ) {}
@ -124,6 +123,8 @@ export class Version {
if (this.prIdent === null && other.prIdent !== null) { if (this.prIdent === null && other.prIdent !== null) {
return true; return true;
} else if (this.prIdent !== null && other.prIdent === null) {
return false;
} else if (this.prIdent !== null && other.prIdent !== null) { } else if (this.prIdent !== null && other.prIdent !== null) {
const isNumeric = /^[0-9]*$/; const isNumeric = /^[0-9]*$/;
for ( for (
@ -131,35 +132,21 @@ export class Version {
i < this.prIdent.length && i < other.prIdent.length; i < this.prIdent.length && i < other.prIdent.length;
i += 1 i += 1
) { ) {
if ( const numericThis = isNumeric.test(other.prIdent[i]!);
!isNumeric.test(this.prIdent[i]!) && const numericOther = isNumeric.test(this.prIdent[i]!);
isNumeric.test(other.prIdent[i]!) if (!numericOther && numericThis) {
) {
return true; return true;
} else if ( } else if (numericOther && numericThis) {
isNumeric.test(this.prIdent[i]!) && const intThis = parseInt(this.prIdent[i]!, 10);
isNumeric.test(other.prIdent[i]!) const intOther = parseInt(other.prIdent[i]!, 10);
) { if (intThis > intOther) {
if (
parseInt(this.prIdent[i]!, 10) >
parseInt(other.prIdent[i]!, 10)
) {
return true; return true;
} else if ( } else if (intThis < intOther) {
parseInt(this.prIdent[i]!, 10) <
parseInt(other.prIdent[i]!, 10)
) {
return false; return false;
} }
} else if ( } else if (numericOther && !numericThis) {
isNumeric.test(this.prIdent[i]!) &&
!isNumeric.test(other.prIdent[i]!)
) {
return false; return false;
} else if ( } else if (!numericOther && !numericThis) {
!isNumeric.test(this.prIdent[i]!) &&
!isNumeric.test(other.prIdent[i]!)
) {
if (this.prIdent[i]! > other.prIdent[i]!) { if (this.prIdent[i]! > other.prIdent[i]!) {
return true; return true;
} else if (this.prIdent[i]! < other.prIdent[i]!) { } else if (this.prIdent[i]! < other.prIdent[i]!) {
@ -168,7 +155,49 @@ export class Version {
} }
} }
return this.prIdent.length > other.prIdent.length; if (this.prIdent.length > other.prIdent.length) {
return true;
} else if (this.prIdent.length < other.prIdent.length) {
return false;
}
}
// Unlike prerelease, we prefer to have a build ident than to not
if (this.buildIdent !== null && other.buildIdent === null) {
return true;
} else if (this.buildIdent === null && other.buildIdent !== null) {
return false;
} else if (this.buildIdent !== null && other.buildIdent !== null) {
const isNumeric = /^[0-9]*$/;
for (
let i = 0;
i < this.buildIdent.length && i < other.buildIdent.length;
i += 1
) {
const numricThis = isNumeric.test(this.buildIdent[i]!);
const numericOther = isNumeric.test(other.buildIdent[i]!);
if (!numricThis && numericOther) {
return true;
} else if (numricThis && numericOther) {
const intThis = parseInt(this.buildIdent[i]!, 10);
const intOther = parseInt(other.buildIdent[i]!, 10);
if (intThis > intOther) {
return true;
} else if (intThis < intOther) {
return false;
}
} else if (numricThis && !numericOther) {
return false;
} else if (!numricThis && !numericOther) {
if (this.buildIdent[i]! > other.buildIdent[i]!) {
return true;
} else if (this.buildIdent[i]! < other.buildIdent[i]!) {
return false;
}
}
}
return this.buildIdent.length > other.buildIdent.length;
} }
return false; return false;

View File

@ -20,7 +20,7 @@ const testMatrix = [
"1-alpha", "1-alpha",
], ],
["0.1.2", "0.1.1-dev", "0.1"], ["0.1.2", "0.1.1-dev", "0.1"],
["0.0.2", "0.0.2-dev", "0.0.2+build"], ["0.0.2+build", "0.0.2+124", "0.0.2+123", "0.0.2", "0.0.2-dev"],
["0.0.1", "0.0.1-dev", "0.0.1-5", "0.0.1-2"], ["0.0.1", "0.0.1-dev", "0.0.1-5", "0.0.1-2"],
]; ];
@ -107,13 +107,6 @@ describe("Version", function () {
const tests = flatten(testMatrix); const tests = flatten(testMatrix);
for (let a = 0; a < tests.length; a++) { for (let a = 0; a < tests.length; a++) {
for (let b = a + 1; b < tests.length; b++) { for (let b = a + 1; b < tests.length; b++) {
if (
tests[a]!.indexOf("+") > -1 ||
tests[b]!.indexOf("+") > -1
) {
// Skip "builds" for purposes of this test.
continue;
}
assert( assert(
Version.fromSemver(tests[a]!).hasPrecedenceOver( Version.fromSemver(tests[a]!).hasPrecedenceOver(
Version.fromSemver(tests[b]!), Version.fromSemver(tests[b]!),
@ -127,13 +120,6 @@ describe("Version", function () {
const tests = flatten(testMatrix).reverse(); const tests = flatten(testMatrix).reverse();
for (let a = 0; a < tests.length; a++) { for (let a = 0; a < tests.length; a++) {
for (let b = a + 1; b < tests.length; b++) { for (let b = a + 1; b < tests.length; b++) {
if (
tests[a]!.indexOf("+") > -1 ||
tests[b]!.indexOf("+") > -1
) {
// Skip "builds" for purposes of this test.
continue;
}
assert( assert(
!Version.fromSemver(tests[a]!).hasPrecedenceOver( !Version.fromSemver(tests[a]!).hasPrecedenceOver(
Version.fromSemver(tests[b]!), Version.fromSemver(tests[b]!),