Skip to content

Commit 7103d8e

Browse files
stevemaogibson042
authored andcommitted
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
1 parent e04e246 commit 7103d8e

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/core.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ jQuery.extend( {
217217
// that can be coerced to finite numbers (gh-2662)
218218
var type = jQuery.type( obj );
219219
return ( type === "number" || type === "string" ) &&
220-
( obj - parseFloat( obj ) + 1 ) >= 0;
220+
221+
// parseFloat NaNs numeric-cast false positives ("")
222+
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
223+
// subtraction forces infinities to NaN
224+
!isNaN( obj - parseFloat( obj ) );
221225
},
222226

223227
isPlainObject: function( obj ) {

test/unit/core.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ QUnit.test( "isFunction", function( assert ) {
449449
} );
450450

451451
QUnit.test( "isNumeric", function( assert ) {
452-
assert.expect( 38 );
452+
assert.expect( 41 );
453453

454454
var t = jQuery.isNumeric,
455455
ToString = function( value ) {
@@ -464,8 +464,29 @@ QUnit.test( "isNumeric", function( assert ) {
464464
assert.ok( t( -16 ), "Negative integer number" );
465465
assert.ok( t( 0 ), "Zero integer number" );
466466
assert.ok( t( 32 ), "Positive integer number" );
467+
468+
if ( +"0b1" === 1 ) {
469+
assert.ok( t( "0b111110" ), "Binary integer literal string" ); // jshint ignore:line
470+
} else {
471+
assert.ok( true, "Browser does not support binary integer literal" );
472+
}
473+
467474
assert.ok( t( "040" ), "Octal integer literal string" );
475+
468476
assert.ok( t( "0xFF" ), "Hexadecimal integer literal string" );
477+
478+
if ( +"0b1" === 1 ) {
479+
assert.ok( t( 0b111110 ), "Binary integer literal" ); // jshint ignore:line
480+
} else {
481+
assert.ok( true, "Browser does not support binary integer literal" );
482+
}
483+
484+
if ( +"0o1" === 1 ) {
485+
assert.ok( t( 0o76 ), "Octal integer literal" ); // jshint ignore:line
486+
} else {
487+
assert.ok( true, "Browser does not support octal integer literal" );
488+
}
489+
469490
assert.ok( t( 0xFFF ), "Hexadecimal integer literal" );
470491
assert.ok( t( "-1.6" ), "Negative floating point string" );
471492
assert.ok( t( "4.536" ), "Positive floating point string" );
@@ -475,7 +496,7 @@ QUnit.test( "isNumeric", function( assert ) {
475496
assert.ok( t( 8e5 ), "Exponential notation" );
476497
assert.ok( t( "123e-2" ), "Exponential notation string" );
477498

478-
assert.equal( t( new ToString( "42" ) ), false, "Custom .toString returning number" );
499+
assert.equal( t( new ToString( "42" ) ), false, "Only limited to strings and numbers" );
479500
assert.equal( t( "" ), false, "Empty string" );
480501
assert.equal( t( " " ), false, "Whitespace characters string" );
481502
assert.equal( t( "\t\t" ), false, "Tab characters string" );

0 commit comments

Comments
 (0)