From 9281a294b3d8d52a87f53beaffe387b414907852 Mon Sep 17 00:00:00 2001 From: Henning Dieterichs Date: Tue, 23 Jan 2024 20:17:22 +0100 Subject: [PATCH] Sorts version numbers --- .../pages/playground/getNpmVersionsSync.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/website/src/website/pages/playground/getNpmVersionsSync.ts b/website/src/website/pages/playground/getNpmVersionsSync.ts index 2ec1eb9e63..30a47875b7 100644 --- a/website/src/website/pages/playground/getNpmVersionsSync.ts +++ b/website/src/website/pages/playground/getNpmVersionsSync.ts @@ -59,9 +59,38 @@ async function _getNpmVersions(): Promise { ]); 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 {