Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvidarte/1d30aa9e93ab18ca1a5d to your computer and use it in GitHub Desktop.
Save lvidarte/1d30aa9e93ab18ca1a5d to your computer and use it in GitHub Desktop.
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