Skip to content

Instantly share code, notes, and snippets.

@mteece
Created May 23, 2014 19:28
Show Gist options
  • Save mteece/5140f80bb8fb1ada38ee to your computer and use it in GitHub Desktop.
Save mteece/5140f80bb8fb1ada38ee to your computer and use it in GitHub Desktop.
Compare the installed to required version. Assumes Semantic Versioning.
/*
* Compare the installed to required version.
*
* @param {String} installed The currently installed version string.
* @param {String} required The required version string.
*
* compareVersions('1.0.1', '1.0.0') returns true
* compareVersions('1.0.0', '1.0.0') returns true
* compareVersions('0.0.9', '1.0.0') returns false
* compareVersions('1.0.0', '1.0.1') returns false
* compareVersions('2.1.2', '1.1.1') returns true
*
* Returns {Boolean}
* */
compareVersions: function (installed, required) {
var a = installed.split('.');
var b = required.split('.');
for (var i = 0; i < a.length; ++i) {
a[i] = Number(a[i]);
}
for (var i = 0; i < b.length; ++i) {
b[i] = Number(b[i]);
}
if (a.length == 2) {
a[2] = 0;
}
if (a[0] > b[0]) return true;
if (a[0] < b[0]) return false;
if (a[1] > b[1]) return true;
if (a[1] < b[1]) return false;
if (a[2] > b[2]) return true;
if (a[2] < b[2]) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment