/** * @author Larry Battle, http://bateru.com/news * @date May 12, 2012 */ (function (global) { var _isNaN = function () {}; var type = function(val){ return Object.prototype.toString.call(val); }; var runAllTests = function () { test("test for non-numbers.", function () { var x = [ undefined, null, [], {}, /regex/, true, false, Math, this, NaN, "string", 0/0 ]; for (var i = 0, len = x.length; i < len; i++) { equal(_isNaN(x[i]), true, "Type \'" + type(x[i]) + "\' with value, "+x[i]+", should not be a number."); } }); test("test for numbers.", function () { var x = ["-10", 16, 0xFF, "0xFF", 8e5, 3.1415, Math.PI, +10, 0144, 0, '0', 42, 1, 1 / 0, -1 / 0, Number(5)]; for (var i = 0, len = x.length; i < len; i++) { equal(_isNaN(x[i]), false, "Type \'" + type(x[i]) + "\' with value, "+x[i]+", should not be a number."); } }); }; var funcs = { "withoutFix" : global.isNaN, "withFix" : (function () { var _isNaN = global.isNaN; return function (val) { return _isNaN("" + val) || (typeof val === "object" && !(val || "").hasOwnProperty('push')); }; }()), "jQueryIsNumeric": function(obj){ return !isNaN( parseFloat(obj) ) && isFinite( obj ); } }; var updateFunction = function () { var funcName = $(this).attr('id') || 'withFix'; _isNaN = funcs[funcName]; QUnit.reset(); QUnit.init(); QUnit.start(); runAllTests(); }; $("input[name='isNaNFunction']").click(updateFunction); updateFunction(); }(this));
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" /> </head> <fieldset> <legend>Settings</legend> Which version of isNaN would you like to test?<br/> <input type="radio" name="isNaNFunction" id="withFix" checked="checked"/><label for="withFix">Fixed Version: </label><br/> <input type="radio" name="isNaNFunction" id="withoutFix"/><label for="withoutFix">Browser Native Version: </label> <br/> <input type="radio" name="isNaNFunction" id="jQueryIsNumeric"/><label for="withoutFix">jQuery isNumeric Version: </label> <br/> More Information at <a href="http://bateru.com/news/">bateru.com/news</a> </fieldset> <body> <h1 id="qunit-header">QUnit example</h1> <h2 id="qunit-banner"></h2> <div id="qunit-testrunner-toolbar"></div> <h2 id="qunit-userAgent"></h2> <ol id="qunit-tests"></ol> <div id="qunit-fixture">test markup, will be hidden</div> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script> </body> </html>?