aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Mao <maochenyan@gmail.com>2016-01-14 20:22:15 +1100
committerRichard Gibson <richard.gibson@gmail.com>2016-01-24 19:07:09 -0500
commit7103d8ef47e04a4cf373abee0e8bfa9062fd616f (patch)
treeef32aefb518017419e53678b95c166cafacb16f7
parente04e246552c27e872bbf4ae00b55def02b197189 (diff)
downloadjquery-7103d8ef47e04a4cf373abee0e8bfa9062fd616f.tar.gz
jquery-7103d8ef47e04a4cf373abee0e8bfa9062fd616f.zip
Core: Improve isNumeric logic and test coverage
Also add back accidentally deleted comments about the implementation. Fixes gh-2780 Ref gh-2663 Ref gh-2781 Closes gh-2827
-rw-r--r--src/core.js6
-rw-r--r--test/unit/core.js25
2 files changed, 28 insertions, 3 deletions
diff --git a/src/core.js b/src/core.js
index 3a54ffc0e..b10128c7a 100644
--- a/src/core.js
+++ b/src/core.js
@@ -217,7 +217,11 @@ jQuery.extend( {
// that can be coerced to finite numbers (gh-2662)
var type = jQuery.type( obj );
return ( type === "number" || type === "string" ) &&
- ( obj - parseFloat( obj ) + 1 ) >= 0;
+
+ // parseFloat NaNs numeric-cast false positives ("")
+ // ...but misinterprets leading-number strings, particularly hex literals ("0x...")
+ // subtraction forces infinities to NaN
+ !isNaN( obj - parseFloat( obj ) );
},
isPlainObject: function( obj ) {
diff --git a/test/unit/core.js b/test/unit/core.js
index 5f7d41d69..4099119b8 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -449,7 +449,7 @@ QUnit.test( "isFunction", function( assert ) {
} );
QUnit.test( "isNumeric", function( assert ) {
- assert.expect( 38 );
+ assert.expect( 41 );
var t = jQuery.isNumeric,
ToString = function( value ) {
@@ -464,8 +464,29 @@ QUnit.test( "isNumeric", function( assert ) {
assert.ok( t( -16 ), "Negative integer number" );
assert.ok( t( 0 ), "Zero integer number" );
assert.ok( t( 32 ), "Positive integer number" );
+
+ if ( +"0b1" === 1 ) {
+ assert.ok( t( "0b111110" ), "Binary integer literal string" ); // jshint ignore:line
+ } else {
+ assert.ok( true, "Browser does not support binary integer literal" );
+ }
+
assert.ok( t( "040" ), "Octal integer literal string" );
+
assert.ok( t( "0xFF" ), "Hexadecimal integer literal string" );
+
+ if ( +"0b1" === 1 ) {
+ assert.ok( t( 0b111110 ), "Binary integer literal" ); // jshint ignore:line
+ } else {
+ assert.ok( true, "Browser does not support binary integer literal" );
+ }
+
+ if ( +"0o1" === 1 ) {
+ assert.ok( t( 0o76 ), "Octal integer literal" ); // jshint ignore:line
+ } else {
+ assert.ok( true, "Browser does not support octal integer literal" );
+ }
+
assert.ok( t( 0xFFF ), "Hexadecimal integer literal" );
assert.ok( t( "-1.6" ), "Negative floating point string" );
assert.ok( t( "4.536" ), "Positive floating point string" );
@@ -475,7 +496,7 @@ QUnit.test( "isNumeric", function( assert ) {
assert.ok( t( 8e5 ), "Exponential notation" );
assert.ok( t( "123e-2" ), "Exponential notation string" );
- assert.equal( t( new ToString( "42" ) ), false, "Custom .toString returning number" );
+ assert.equal( t( new ToString( "42" ) ), false, "Only limited to strings and numbers" );
assert.equal( t( "" ), false, "Empty string" );
assert.equal( t( " " ), false, "Whitespace characters string" );
assert.equal( t( "\t\t" ), false, "Tab characters string" );