Skip to content

Commit

Permalink
Sorts version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
hediet committed Jan 23, 2024
1 parent 9488e7e commit 9281a29
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions website/src/website/pages/playground/getNpmVersionsSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,38 @@ async function _getNpmVersions(): Promise<string[]> {
]);

const validVersions = versions.filter((v) => !brokenVersions.has(v));

validVersions.sort((v1, v2) => -compareSemanticVersions(v1, v2));

return validVersions;
}

function compareSemanticVersions(version1: string, version2: string): number {
const v1 = version1.split(/[.]/);
const v2 = version2.split(/[.]/);
for (let i = 0; i < Math.min(v1.length, v2.length); i++) {
const isNumber = /^\d+$/.test(v1[i]) && /^\d+$/.test(v2[i]);

if (isNumber) {
const n1 = parseInt(v1[i]);
const n2 = parseInt(v2[i]);
if (n1 < n2) {
return -1;
}
if (n1 > n2) {
return 1;
}
} else {
const compare = v1[i].localeCompare(v2[i]);
if (compare !== 0) {
return compare;
}
}
}

return v1.length - v2.length;
}

export async function getVsCodeCommitId(
monacoEditorVersion: string
): Promise<string | undefined> {
Expand Down

0 comments on commit 9281a29

Please sign in to comment.