Created
July 1, 2010 17:36
-
-
Save cowboy/460275 to your computer and use it in GitHub Desktop.
Test jQuery version WIP UNTESTED
This file contains hidden or 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
/*! | |
* jQuery isVersion - v0.1 - 07/02/2010 | |
* http://benalman.com/ | |
* | |
* Copyright (c) 2010 "Cowboy" Ben Alman | |
* Dual licensed under the MIT and GPL licenses. | |
* http://benalman.com/about/license/ | |
*/ | |
(function($){ | |
'$:nomunge'; // Used by YUI compressor. | |
var re = /^([!<>=]+)?\s*((\d+(?:\.\d+)*)(pre)?)$/i, | |
cur = get_num( $.fn.jquery || '' ); | |
// Return true if all version criteria are met. Comparison operator defaults | |
// to == if omitted. Valid comparisons are == != < <= > >=. | |
// | |
// ie. $.isVersion( '1.3.2' ) or $.isVersion( '>= 1.4', '!= 1.4.1' ) | |
// | |
$.isVersion = function() { | |
var result; | |
// Iterate over all function arguments, allowing any number of tests. | |
$.each( arguments, function(i,v){ | |
var matches = v.match( re ); | |
// If the regexp matched, then evaluate the comparison, setting `result` | |
// to that value, and returing it (so that a `false` value will break | |
// out of the each loop). | |
return matches && ( result = (new Function( | |
'return ' + cur + ( matches[1] || '==' ) + get_num( matches[2] ) | |
))() ); | |
}); | |
return result; | |
}; | |
// Get number that can be used for comparison. Converts a string like | |
// "1.2.3.4" into the number 1020304 (and "1.2.3.4pre" into the number | |
// 1020303.9) for easy numeric comparisons. | |
function get_num( str ) { | |
var matches = str.match( re ), | |
num = 0; | |
if ( matches ) { | |
$.each( matches[3].split('.'), function(i,v){ | |
num += Math.pow( 0.01, i ) * v * 1000000; | |
}); | |
num -= matches[4] ? 0.1 : 0; | |
} | |
return num; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure but at least there are more restrictions coming soon (Firefox 4).
The same can't be said for script injection.
True, but it comes down to how you use it. The same could be said for the
Function
constructor if your code accesses pseudo private properties/methods on a global object (those prefixed with an underscore_
) because some minifiers will shrink them.