Skip to content
Permalink
Browse files

Fix #10690. Make sure `.isNumeric()` can't get a `Date`.

Also add tests for custom objects with a `.toString()` method.
  • Loading branch information...
dmethvin committed Nov 7, 2011
1 parent 13aa845 commit 795583d2936a48c6f0048e28f74c5fc71e3a60b0
Showing with 16 additions and 4 deletions.
  1. +1 −1 src/core.js
  2. +15 −3 test/unit/core.js
@@ -485,7 +485,7 @@ jQuery.extend({
},

isNumeric: function( obj ) {
return obj != null && rdigit.test( obj ) && !isNaN( obj );
return !isNaN( parseFloat(obj) ) && isFinite( obj );

This comment has been minimized.

Copy link
@pimvdb

pimvdb Nov 18, 2011

What about removing rdigit now? It's nowhere used anymore, since isNumeric was the only use.

This comment has been minimized.

Copy link
@dmethvin

dmethvin Nov 18, 2011

Author Member

Sounds good to me! Thanks.

This comment has been minimized.

Copy link
@timmywil

timmywil Nov 18, 2011

Member

You're right

},

type: function( obj ) {
@@ -455,9 +455,17 @@ test("isFunction", function() {
});

test( "isNumeric", function() {
expect( 33 );

var t = jQuery.isNumeric;
expect( 37 );

var t = jQuery.isNumeric,
Traditionalists = function(n) {
this.value = n;
this.toString = function(){
return String(this.value);
};
},
answer = new Traditionalists( "42" ),
rong = new Traditionalists( "Devo" );

ok( t("-10"), "Negative integer string");
ok( t("0"), "Zero string");
@@ -475,6 +483,7 @@ test( "isNumeric", function() {
ok( t(3.1415), "Positive floating point number");
ok( t(8e5), "Exponential notation");
ok( t("123e-2"), "Exponential notation string");
ok( t(answer), "Custom .toString returning number");
equal( t(""), false, "Empty string");
equal( t(" "), false, "Whitespace characters string");
equal( t("\t\t"), false, "Tab characters string");
@@ -490,8 +499,11 @@ test( "isNumeric", function() {
equal( t(Infinity), false, "Infinity primitive");
equal( t(Number.POSITIVE_INFINITY), false, "Positive Infinity");
equal( t(Number.NEGATIVE_INFINITY), false, "Negative Infinity");
equal( t(rong), false, "Custom .toString returning non-number");
equal( t({}), false, "Empty object");
equal( t(function(){} ), false, "Instance of a function");
equal( t( new Date ), false, "Instance of a Date");
equal( t(function(){} ), false, "Instance of a function");
});

test("isXMLDoc - HTML", function() {

0 comments on commit 795583d

Please sign in to comment.
You can’t perform that action at this time.