是否有任何资源可以测试jQuery脚本/扩展/插件/版本兼容性问题?
刚刚写了一个小的jQuery插件来帮助解决版本兼容性问题...随意改进它.
(function($) { /** * Used for version test cases. * * @param {string} left A string containing the version that will become * the left hand operand. * @param {string} oper The comparison operator to test against. By * default, the "==" operator will be used. * @param {string} right A string containing the version that will * become the right hand operand. By default, the current jQuery * version will be used. * * @return {boolean} Returns the evaluation of the expression, either * true or false. */ $.isVersion = function(left, oper, right) { if (left) { var pre = /pre/i, replace = /[^\d]+/g, oper = oper || "==", right = right || $().jquery, l = left.replace(replace, ''), r = right.replace(replace, ''), l_len = l.length, r_len = r.length, l_pre = pre.test(left), r_pre = pre.test(right); l = (r_len > l_len ? parseInt(l) * ((r_len - l_len) * 10) : parseInt(l)); r = (l_len > r_len ? parseInt(r) * ((l_len - r_len) * 10) : parseInt(r)); switch(oper) { case "==": { return (true === (l == r && (l_pre == r_pre))); } case ">=": { return (true === (l >= r && (!l_pre || l_pre == r_pre))); } case "<=": { return (true === (l <= r && (!r_pre || r_pre == l_pre))); } case ">": { return (true === (l > r || (l == r && r_pre))); } case "<": { return (true === (l < r || (l == r && l_pre))); } } } return false; } })(jQuery);
可以像这样使用:
$.isVersion("1.4.2"); // returns true, if $().jquery == "1.4.2" $.isVersion("1.3.2", ">"); // returns true if $().jquery > "1.3.2" $.isVersion("1.3", ">", "1.2.6"); // returns true $.isVersion("1.3.2", "<", "1.3.1"); // returns false $.isVersion("1.4.0", ">=", "1.3.2"); // returns true $.isVersion("1.4.1", "<=", "1.4.1"); // returns true
还支持预发布(版本比预发行版重,因此1.4.0pre <1.4.0):
$.isVersion("1.4.2", "<=", "1.4.2pre"); // returns false
代码有一些错误,特别是
$ .isVersion('1.9.2.17','<','2.0')返回false
$ .isVersion('1.17.2.1','>','1.8')返回false
解决方案
1.由附加的代码修复.
2.如果没有完整的重写,并不是那么棘手,无论如何都是一个罕见的案例.
(function($) { /** * Used for version test cases. * * @param {string} left A string containing the version that will become * the left hand operand. * @param {string} oper The comparison operator to test against. By * default, the "==" operator will be used. * @param {string} right A string containing the version that will * become the right hand operand. By default, the current jQuery * version will be used. * * @return {boolean} Returns the evaluation of the expression, either * true or false. */ $.isVersion = function(left, oper, right) { if (left) { var pre = /pre/i, replace = /[^\d]+/g, oper = oper || "==", right = right || $().jquery, l = left.replace(replace, ''), r = right.replace(replace, ''), l_len = l.length, r_len = r.length, l_pre = pre.test(left), r_pre = pre.test(right); l = (r_len > l_len ? parseInt(l) * Math.pow(10, (r_len - l_len)) : parseInt(l)); r = (l_len > r_len ? parseInt(r) * Math.pow(10, (l_len - r_len)) : parseInt(r)); switch(oper) { case "==": { return (true === (l == r && (l_pre == r_pre))); } case ">=": { return (true === (l >= r && (!l_pre || l_pre == r_pre))); } case "<=": { return (true === (l <= r && (!r_pre || r_pre == l_pre))); } case ">": { return (true === (l > r || (l == r && r_pre))); } case "<": { return (true === (l < r || (l == r && l_pre))); } } } return false; } })(jQuery);