Skip to content

Instantly share code, notes, and snippets.

@RedWolves
Created May 12, 2012 05:06
Show Gist options
  • Save RedWolves/2664255 to your computer and use it in GitHub Desktop.
Save RedWolves/2664255 to your computer and use it in GitHub Desktop.
Another implementation to @dieseltravis compare jQuery version strings gist https://gist.github.com/2662417 Demo in comments.
var jQueryVer = {
isEqual: function (test, act) {
this.version = test || this.version;
if (this.version) {
//if actual jquery version is passed in use that if not get it.
act = act || jQuery.fn.jquery;
return this._compare(act);
}
},
//Boolean value on if the actual jQuery Version is greater then the test version.
greater: null,
//Boolean value on if the actual jQuery Version is lower then the test version.
lower: null,
version: null,
_compare: function(actVer) {
var testVer = this.version,
_alpha = /[a-z]+(\d+)/gi,
getArray = function (v) {
// replace rc1, rc2, beta3, etc with .-1.1, .-1.2, .-1.3, etc
return v.replace(_alpha, ".-1.$1").split(".");
},
a,
b,
maxLength,
parseVer = function (v) {
var hasLetters = /[a-z]/gi;
return (v) ? (hasLetters.test(v)) ? -1 : parseInt(v, 10) : 0;
};
// quick test of equality before digging in
if (actVer === testVer) {
this.greater = this.lower = false;
return true;
}
a = getArray(testVer);
b = getArray(actVer);
maxLength = Math.max(a.length, b.length);
// verify both arrays are the same size
a.length = maxLength;
b.length = maxLength;
for (var i = 0; i < maxLength; i++) {
var _a = parseVer(a[i]),
_b = parseVer(b[i]);
this.greater = !(_a > _b);
this.lower = !(_a < _b);
if (this.greater !== this.lower)
return false
}
// all equal at this point
return true;
}
};
jQueryVer.version = "1.6.4";
console.log(jQueryVer.version); //1.6.4
console.log(jQueryVer.isEqual()); //false
console.log(jQueryVer.greater ? "Version is greater" : jQueryVer.lower ? "Version is lower" : ""); //Version is greater
console.log((jQueryVer.isEqual("1.7.1"))); //true
console.log(jQueryVer.version); //1.7.1
console.log(jQueryVer.isEqual("1.8.2")); //false
console.log(jQueryVer.version); //1.8.2
console.log(jQueryVer.greater ? "Version is greater" : jQueryVer.lower ? "Version is lower" : ""); //Version is lower
@RedWolves
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment