JSFiddle - React, Tailwind, and code Playground
HTML
<!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>
<body>
<h1 id="qunit-header">Test cases for isNaN2 and isNumberic</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>
JavaScript
/**
* @author Larry Battle, http://bateru.com/news
* @date May 12, 2012
*/
var isNaN2 = (function (global) {
var _isNaN = global.isNaN;
return function (val) {
return _isNaN("" + val) || (
typeof val === "object" && !(val || "").hasOwnProperty('push'));
};
}(this));
var isNumberic = (function (global) {
var _isNaN = global.isNaN;
return function (val) {
return !_isNaN("" + val) && (
typeof val !== "object" || (val || "").hasOwnProperty('push'));
};
}(this));
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
];
var i = x.length;
while (i--) {
equal(isNaN2(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)];
var i = x.length;
while (i--) {
equal(isNumberic(x[i]), true, "Type \'" + type(x[i]) + "\' with value, " + x[i] + ", should not be a number.");
}
});
};
runAllTests();