Core: deprecate jQuery.isNumeric

Fixes gh-2960
Closes gh-3888
This commit is contained in:
Jason Bedard 2017-12-12 21:51:49 -08:00
parent d7237896c7
commit c4494d4abc
6 changed files with 92 additions and 95 deletions

View File

@ -204,20 +204,6 @@ jQuery.extend( {
noop: function() {}, noop: function() {},
isNumeric: function( obj ) {
// As of jQuery 3.0, isNumeric is limited to
// strings and numbers (primitives or objects)
// that can be coerced to finite numbers (gh-2662)
var type = jQuery.type( obj );
return ( type === "number" || type === "string" ) &&
// 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 ) { isPlainObject: function( obj ) {
var proto, Ctor; var proto, Ctor;

View File

@ -79,4 +79,18 @@ jQuery.camelCase = camelCase;
jQuery.now = Date.now; jQuery.now = Date.now;
jQuery.isNumeric = function( obj ) {
// As of jQuery 3.0, isNumeric is limited to
// strings and numbers (primitives or objects)
// that can be coerced to finite numbers (gh-2662)
var type = jQuery.type( obj );
return ( type === "number" || type === "string" ) &&
// parseFloat NaNs numeric-cast false positives ("")
// ...but misinterprets leading-number strings, particularly hex literals ("0x...")
// subtraction forces infinities to NaN
!isNaN( obj - parseFloat( obj ) );
};
} ); } );

View File

@ -76,7 +76,7 @@ QUnit.test( "show/hide", function( assert ) {
} }
QUnit.test( "core", function( assert ) { QUnit.test( "core", function( assert ) {
assert.expect( 23 ); assert.expect( 21 );
var elem = jQuery( "<div></div><span></span>" ); var elem = jQuery( "<div></div><span></span>" );
@ -90,9 +90,6 @@ QUnit.test( "core", function( assert ) {
assert.ok( jQuery.isPlainObject( { "a": 2 } ), "jQuery.isPlainObject(object)" ); assert.ok( jQuery.isPlainObject( { "a": 2 } ), "jQuery.isPlainObject(object)" );
assert.ok( !jQuery.isPlainObject( "foo" ), "jQuery.isPlainObject(String)" ); assert.ok( !jQuery.isPlainObject( "foo" ), "jQuery.isPlainObject(String)" );
assert.ok( jQuery.isNumeric( "-2" ), "jQuery.isNumeric(String representing a number)" );
assert.ok( !jQuery.isNumeric( "" ), "jQuery.isNumeric(\"\")" );
assert.ok( jQuery.isXMLDoc( jQuery.parseXML( assert.ok( jQuery.isXMLDoc( jQuery.parseXML(
"<?xml version='1.0' encoding='UTF-8'?><foo bar='baz'></foo>" "<?xml version='1.0' encoding='UTF-8'?><foo bar='baz'></foo>"
) ), "jQuery.isXMLDoc" ); ) ), "jQuery.isXMLDoc" );

View File

@ -404,81 +404,6 @@ QUnit[ "assign" in Object ? "test" : "skip" ]( "isPlainObject(Object.assign(...)
} }
); );
QUnit.test( "isNumeric", function( assert ) {
assert.expect( 43 );
var t = jQuery.isNumeric,
ToString = function( value ) {
this.toString = function() {
return String( value );
};
};
assert.ok( t( "-10" ), "Negative integer string" );
assert.ok( t( "0" ), "Zero string" );
assert.ok( t( "5" ), "Positive integer string" );
assert.ok( t( -16 ), "Negative integer number" );
assert.ok( t( 0 ), "Zero integer number" );
assert.ok( t( 32 ), "Positive integer number" );
assert.ok( t( "-1.6" ), "Negative floating point string" );
assert.ok( t( "4.536" ), "Positive floating point string" );
assert.ok( t( -2.6 ), "Negative floating point number" );
assert.ok( t( 3.1415 ), "Positive floating point number" );
assert.ok( t( 1.5999999999999999 ), "Very precise floating point number" );
assert.ok( t( 8e5 ), "Exponential notation" );
assert.ok( t( "123e-2" ), "Exponential notation string" );
assert.ok( t( "040" ), "Legacy octal integer literal string" );
assert.ok( t( "0xFF" ), "Hexadecimal integer literal string (0x...)" );
assert.ok( t( "0Xba" ), "Hexadecimal integer literal string (0X...)" );
assert.ok( t( 0xFFF ), "Hexadecimal integer literal" );
if ( +"0b1" === 1 ) {
assert.ok( t( "0b111110" ), "Binary integer literal string (0b...)" );
assert.ok( t( "0B111110" ), "Binary integer literal string (0B...)" );
} else {
assert.ok( true, "Browser does not support binary integer literal (0b...)" );
assert.ok( true, "Browser does not support binary integer literal (0B...)" );
}
if ( +"0o1" === 1 ) {
assert.ok( t( "0o76" ), "Octal integer literal string (0o...)" );
assert.ok( t( "0O76" ), "Octal integer literal string (0O...)" );
} else {
assert.ok( true, "Browser does not support octal integer literal (0o...)" );
assert.ok( true, "Browser does not support octal integer literal (0O...)" );
}
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" );
assert.equal( t( "abcdefghijklm1234567890" ), false, "Alphanumeric character string" );
assert.equal( t( "xabcdefx" ), false, "Non-numeric character string" );
assert.equal( t( true ), false, "Boolean true literal" );
assert.equal( t( false ), false, "Boolean false literal" );
assert.equal( t( "bcfed5.2" ), false, "Number with preceding non-numeric characters" );
assert.equal( t( "7.2acdgs" ), false, "Number with trailing non-numeric characters" );
assert.equal( t( undefined ), false, "Undefined value" );
assert.equal( t( null ), false, "Null value" );
assert.equal( t( NaN ), false, "NaN value" );
assert.equal( t( Infinity ), false, "Infinity primitive" );
assert.equal( t( Number.POSITIVE_INFINITY ), false, "Positive Infinity" );
assert.equal( t( Number.NEGATIVE_INFINITY ), false, "Negative Infinity" );
assert.equal( t( new ToString( "Devo" ) ), false, "Custom .toString returning non-number" );
assert.equal( t( {} ), false, "Empty object" );
assert.equal( t( [] ), false, "Empty array" );
assert.equal( t( [ 42 ] ), false, "Array with one number" );
assert.equal( t( function() {} ), false, "Instance of a function" );
assert.equal( t( new Date() ), false, "Instance of a Date" );
} );
QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "isNumeric(Symbol)", function( assert ) {
assert.expect( 2 );
assert.equal( jQuery.isNumeric( Symbol() ), false, "Symbol" );
assert.equal( jQuery.isNumeric( Object( Symbol() ) ), false, "Symbol inside an object" );
} );
QUnit.test( "isXMLDoc - HTML", function( assert ) { QUnit.test( "isXMLDoc - HTML", function( assert ) {
assert.expect( 4 ); assert.expect( 4 );

View File

@ -1223,8 +1223,8 @@ QUnit.test( "certain css values of 'normal' should be convertable to a number, s
var el = jQuery( "<div style='letter-spacing:normal;font-weight:normal;'>test</div>" ).appendTo( "#qunit-fixture" ); var el = jQuery( "<div style='letter-spacing:normal;font-weight:normal;'>test</div>" ).appendTo( "#qunit-fixture" );
assert.ok( jQuery.isNumeric( parseFloat( el.css( "letterSpacing" ) ) ), "css('letterSpacing') not convertable to number, see #8627" ); assert.ok( !isNaN( parseFloat( el.css( "letterSpacing" ) ) ), "css('letterSpacing') not convertable to number, see #8627" );
assert.ok( jQuery.isNumeric( parseFloat( el.css( "fontWeight" ) ) ), "css('fontWeight') not convertable to number, see #8627" ); assert.ok( !isNaN( parseFloat( el.css( "fontWeight" ) ) ), "css('fontWeight') not convertable to number, see #8627" );
assert.equal( typeof el.css( "fontWeight" ), "string", ".css() returns a string" ); assert.equal( typeof el.css( "fontWeight" ), "string", ".css() returns a string" );
} ); } );

View File

@ -487,3 +487,78 @@ QUnit.test( "jQuery.proxy", function( assert ) {
cb = jQuery.proxy( fn, null, "arg1", "arg2" ); cb = jQuery.proxy( fn, null, "arg1", "arg2" );
cb.call( thisObject, "arg3" ); cb.call( thisObject, "arg3" );
} ); } );
QUnit.test( "isNumeric", function( assert ) {
assert.expect( 43 );
var t = jQuery.isNumeric,
ToString = function( value ) {
this.toString = function() {
return String( value );
};
};
assert.ok( t( "-10" ), "Negative integer string" );
assert.ok( t( "0" ), "Zero string" );
assert.ok( t( "5" ), "Positive integer string" );
assert.ok( t( -16 ), "Negative integer number" );
assert.ok( t( 0 ), "Zero integer number" );
assert.ok( t( 32 ), "Positive integer number" );
assert.ok( t( "-1.6" ), "Negative floating point string" );
assert.ok( t( "4.536" ), "Positive floating point string" );
assert.ok( t( -2.6 ), "Negative floating point number" );
assert.ok( t( 3.1415 ), "Positive floating point number" );
assert.ok( t( 1.5999999999999999 ), "Very precise floating point number" );
assert.ok( t( 8e5 ), "Exponential notation" );
assert.ok( t( "123e-2" ), "Exponential notation string" );
assert.ok( t( "040" ), "Legacy octal integer literal string" );
assert.ok( t( "0xFF" ), "Hexadecimal integer literal string (0x...)" );
assert.ok( t( "0Xba" ), "Hexadecimal integer literal string (0X...)" );
assert.ok( t( 0xFFF ), "Hexadecimal integer literal" );
if ( +"0b1" === 1 ) {
assert.ok( t( "0b111110" ), "Binary integer literal string (0b...)" );
assert.ok( t( "0B111110" ), "Binary integer literal string (0B...)" );
} else {
assert.ok( true, "Browser does not support binary integer literal (0b...)" );
assert.ok( true, "Browser does not support binary integer literal (0B...)" );
}
if ( +"0o1" === 1 ) {
assert.ok( t( "0o76" ), "Octal integer literal string (0o...)" );
assert.ok( t( "0O76" ), "Octal integer literal string (0O...)" );
} else {
assert.ok( true, "Browser does not support octal integer literal (0o...)" );
assert.ok( true, "Browser does not support octal integer literal (0O...)" );
}
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" );
assert.equal( t( "abcdefghijklm1234567890" ), false, "Alphanumeric character string" );
assert.equal( t( "xabcdefx" ), false, "Non-numeric character string" );
assert.equal( t( true ), false, "Boolean true literal" );
assert.equal( t( false ), false, "Boolean false literal" );
assert.equal( t( "bcfed5.2" ), false, "Number with preceding non-numeric characters" );
assert.equal( t( "7.2acdgs" ), false, "Number with trailing non-numeric characters" );
assert.equal( t( undefined ), false, "Undefined value" );
assert.equal( t( null ), false, "Null value" );
assert.equal( t( NaN ), false, "NaN value" );
assert.equal( t( Infinity ), false, "Infinity primitive" );
assert.equal( t( Number.POSITIVE_INFINITY ), false, "Positive Infinity" );
assert.equal( t( Number.NEGATIVE_INFINITY ), false, "Negative Infinity" );
assert.equal( t( new ToString( "Devo" ) ), false, "Custom .toString returning non-number" );
assert.equal( t( {} ), false, "Empty object" );
assert.equal( t( [] ), false, "Empty array" );
assert.equal( t( [ 42 ] ), false, "Array with one number" );
assert.equal( t( function() {} ), false, "Instance of a function" );
assert.equal( t( new Date() ), false, "Instance of a Date" );
} );
QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "isNumeric(Symbol)", function( assert ) {
assert.expect( 2 );
assert.equal( jQuery.isNumeric( Symbol() ), false, "Symbol" );
assert.equal( jQuery.isNumeric( Object( Symbol() ) ), false, "Symbol inside an object" );
} );