Created
March 5, 2011 14:05
-
-
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.
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
/** | |
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