From 92dd811948d4c245a8dc0a8cfe10449949f5a496 Mon Sep 17 00:00:00 2001 From: Zach Plata Date: Tue, 19 Apr 2022 12:59:41 -0700 Subject: [PATCH] fix version compare function --- scripts/nextVersion.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/scripts/nextVersion.js b/scripts/nextVersion.js index d59b042..3bbdd8b 100644 --- a/scripts/nextVersion.js +++ b/scripts/nextVersion.js @@ -14,22 +14,16 @@ function compareVersion(first, second) { const firstParts = first.split('.').map((value) => parseInt(value)); const secondParts = second.split('.').map((value) => parseInt(value)); - for (let i = 0; i < firstParts.length; i++) { - if (secondParts.length === i) { - return 1; + for (let i = 0; i < Math.max(firstParts.length, secondParts.length); i++) { + const first = i < firstParts.length ? firstParts[i] : 0; + const second = i < secondParts.length ? secondParts[i] : 0; + if (first < second) { + return -1; } - - if (firstParts[i] < secondParts[i]) { - return -1; - } else if (firstParts[i] > secondParts[i]) { - return 1; + if (second < first) { + return 1; } } - - if (firstParts.length !== secondParts.length) { - return -1; - } - return 0; }