Skip to content

Instantly share code, notes, and snippets.

@oslego
Created March 5, 2011 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 oslego/856370 to your computer and use it in GitHub Desktop.
Save oslego/856370 to your computer and use it in GitHub Desktop.
Check two string representations of versions and return true if the testVersion is equal or greater than the testVersion.
/**
check two string respresentations of versions.
returns true if the version to test is equal or greater than the required minimum version
returns false if the test version is lower than the minimum version
@param {String} minVersion Minimum required version to test against
@param {String} testVersion Version to test
@return {Boolean}
TODO: add variable version length support (decorate smaller length array with 0 for missing parts)
**/
function isNewerVersion(minVersion, testVersion){
if ( typeof minVersion != "string" || typeof testVersion != "string" ){
throw new TypeError("Arguments need to be of type String");
}
var len,
i=0,
toInteger = function(str){
return parseInt(str, 10)
},
minParts = minVersion.split(".").map(toInteger),
testParts = testVersion.split(".").map(toInteger);
if (!testParts.length){
return false;
}
//check only to the length of the test version
for (i, len = testParts.length; i<len; i++){
if (testParts[i] > minParts[i]){
return true;
}
}
//min version is not satisfied
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment