From c4494d4abc84d368d6597889ab45fc07466f8f26 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Tue, 12 Dec 2017 21:51:49 -0800 Subject: [PATCH] Core: deprecate jQuery.isNumeric Fixes gh-2960 Closes gh-3888 --- src/core.js | 14 -------- src/deprecated.js | 14 ++++++++ test/unit/basic.js | 5 +-- test/unit/core.js | 75 ----------------------------------------- test/unit/css.js | 4 +-- test/unit/deprecated.js | 75 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 95 deletions(-) diff --git a/src/core.js b/src/core.js index 5916b03aa..8ffc4e516 100644 --- a/src/core.js +++ b/src/core.js @@ -204,20 +204,6 @@ jQuery.extend( { 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 ) { var proto, Ctor; diff --git a/src/deprecated.js b/src/deprecated.js index 0982f3a94..f1b7db7c6 100644 --- a/src/deprecated.js +++ b/src/deprecated.js @@ -79,4 +79,18 @@ jQuery.camelCase = camelCase; 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 ) ); +}; + } ); diff --git a/test/unit/basic.js b/test/unit/basic.js index 29042caa5..028492c2c 100644 --- a/test/unit/basic.js +++ b/test/unit/basic.js @@ -76,7 +76,7 @@ QUnit.test( "show/hide", function( assert ) { } QUnit.test( "core", function( assert ) { - assert.expect( 23 ); + assert.expect( 21 ); var elem = jQuery( "
" ); @@ -90,9 +90,6 @@ QUnit.test( "core", function( assert ) { assert.ok( jQuery.isPlainObject( { "a": 2 } ), "jQuery.isPlainObject(object)" ); 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( "" ) ), "jQuery.isXMLDoc" ); diff --git a/test/unit/core.js b/test/unit/core.js index fcff895b1..85439d643 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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 ) { assert.expect( 4 ); diff --git a/test/unit/css.js b/test/unit/css.js index 20f8195aa..7859e3fa8 100644 --- a/test/unit/css.js +++ b/test/unit/css.js @@ -1223,8 +1223,8 @@ QUnit.test( "certain css values of 'normal' should be convertable to a number, s var el = jQuery( "
test
" ).appendTo( "#qunit-fixture" ); - assert.ok( jQuery.isNumeric( 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( "letterSpacing" ) ) ), "css('letterSpacing') 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" ); } ); diff --git a/test/unit/deprecated.js b/test/unit/deprecated.js index 554ab443e..007b3f13f 100644 --- a/test/unit/deprecated.js +++ b/test/unit/deprecated.js @@ -487,3 +487,78 @@ QUnit.test( "jQuery.proxy", function( assert ) { cb = jQuery.proxy( fn, null, "arg1", "arg2" ); 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" ); +} ); -- 2.39.5