From d45c283f3b8e7377a585670f4036572f90e39845 Mon Sep 17 00:00:00 2001 From: Kalila L Date: Thu, 27 Aug 2020 03:53:04 -0400 Subject: [PATCH] Add semanticVersionCompare function to utils.js --- scripts/system/libraries/utils.js | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/scripts/system/libraries/utils.js b/scripts/system/libraries/utils.js index d5155ff465..ff467b1f5b 100644 --- a/scripts/system/libraries/utils.js +++ b/scripts/system/libraries/utils.js @@ -502,3 +502,58 @@ getMainTabletIDs = function () { } return tabletIDs; }; + +// returns 1 if A is greater, 0 if equal, -1 if A is lesser +semanticVersionCompare = function(versionA, versionB) { + var versionAParts = versionA.split('.'); + var versionBParts = versionB.split('.'); + + // make sure each version has 3 parts + var partsLength = versionAParts.length; + while (partsLength < 3) { + partsLength = versionAParts.push(0); + } + + partsLength = versionBParts.length; + while (partsLength < 3) { + partsLength = versionBParts.push(0); + } + + for (var i = 0; i < 3; ++i) { + // We're detecting if there's tags, indicating a pre-release OR it's obsolete. + var versionAPartHasTag = (versionAParts[i].indexOf('-') !== -1); + var versionBPartHasTag = (versionBParts[i].indexOf('-') !== -1); + + if (versionAPartHasTag || versionBPartHasTag) { + // In semantic versioning, if version A has a tag and B does not, B is a higher version. + if (versionAPartHasTag < versionBPartHasTag) { + return 1; + } else if (versionAPartHasTag > versionBPartHasTag) { + return -1; + } else { + // They both have tags + var splitA = versionAParts[i].split('-')[0]; + var splitB = versionBParts[i].split('-')[0]; + + if (splitA == splitB) { + continue; + } else if (splitA > splitB) { + return 1; + } else { + return -1; + } + } + } else { // Else, they're both numbers so compare the strings as if they were. + if (versionAParts[i] == versionBParts[i]) { + continue; + } else if (versionAParts[i] > versionBParts[i]) { + return 1; + } else { + return -1; + } + } + } + + // They're both equal! + return 0; +}