Last active
August 29, 2015 14:05
-
-
Save lvidarte/1d30aa9e93ab18ca1a5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var files = [ | |
'coquelux.01.08.01.tar.gz', | |
'coquelux.01.08.09.tar.gz', | |
'coquelux.01.08.100.tar.gz', | |
'coquelux.01.08.10.tar.gz', | |
'coquelux.01.08.99.tar.gz', | |
]; | |
var getLatestVersion = function(files) { | |
var latestVersion = [0, 0, 0]; | |
var latestVersionIndex = 0; | |
var tarGzLen = '.tar.gz'.length; | |
for (var i = 0; i < files.length; i++) { | |
var version = files[i].slice(0, -tarGzLen).split('.').slice(-3).map(Number); | |
for (j = 0; j < version.length; j++) { | |
if (version[j] > latestVersion[j]) { | |
latestVersion = version; | |
latestVersionIndex = i; | |
break; | |
} | |
if (version[j] < latestVersion[j]) { | |
break; | |
} | |
} | |
} | |
return files[latestVersionIndex]; | |
}; | |
console.log(getLatestVersion(files)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment