From: Jason Bedard Date: Sun, 14 Jan 2018 08:46:20 +0000 (-0800) Subject: Core: deprecate jQuery.type X-Git-Tag: 3.3.0~12 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1ea092a54b00aa4d902f4e22ada3854d195d4a18;p=jquery.git Core: deprecate jQuery.type Fixes gh-3605 Close gh-3895 --- diff --git a/src/callbacks.js b/src/callbacks.js index 78e9b4fd0..6cf54031e 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -1,8 +1,9 @@ define( [ "./core", + "./core/toType", "./var/isFunction", "./var/rnothtmlwhite" -], function( jQuery, isFunction, rnothtmlwhite ) { +], function( jQuery, toType, isFunction, rnothtmlwhite ) { "use strict"; @@ -130,7 +131,7 @@ jQuery.Callbacks = function( options ) { if ( !options.unique || !self.has( arg ) ) { list.push( arg ); } - } else if ( arg && arg.length && jQuery.type( arg ) !== "string" ) { + } else if ( arg && arg.length && toType( arg ) !== "string" ) { // Inspect recursively add( arg ); diff --git a/src/core.js b/src/core.js index 8ffc4e516..7a016121c 100644 --- a/src/core.js +++ b/src/core.js @@ -18,10 +18,11 @@ define( [ "./var/support", "./var/isFunction", "./var/isWindow", - "./core/DOMEval" + "./core/DOMEval", + "./core/toType" ], function( arr, document, getProto, slice, concat, push, indexOf, class2type, toString, hasOwn, fnToString, ObjectFunctionString, - support, isFunction, isWindow, DOMEval ) { + support, isFunction, isWindow, DOMEval, toType ) { "use strict"; @@ -237,17 +238,6 @@ jQuery.extend( { return true; }, - type: function( obj ) { - if ( obj == null ) { - return obj + ""; - } - - // Support: Android <=2.3 only (functionish RegExp) - return typeof obj === "object" || typeof obj === "function" ? - class2type[ toString.call( obj ) ] || "object" : - typeof obj; - }, - // Evaluates a script in a global context globalEval: function( code ) { DOMEval( code ); @@ -395,7 +385,7 @@ function isArrayLike( obj ) { // hasOwn isn't used here due to false negatives // regarding Nodelist length in IE var length = !!obj && "length" in obj && obj.length, - type = jQuery.type( obj ); + type = toType( obj ); if ( isFunction( obj ) || isWindow( obj ) ) { return false; diff --git a/src/core/access.js b/src/core/access.js index 2e1eb4121..842c4a42b 100644 --- a/src/core/access.js +++ b/src/core/access.js @@ -1,7 +1,8 @@ define( [ "../core", + "../core/toType", "../var/isFunction" -], function( jQuery, isFunction ) { +], function( jQuery, toType, isFunction ) { "use strict"; @@ -13,7 +14,7 @@ var access = function( elems, fn, key, value, chainable, emptyGet, raw ) { bulk = key == null; // Sets many values - if ( jQuery.type( key ) === "object" ) { + if ( toType( key ) === "object" ) { chainable = true; for ( i in key ) { access( elems, fn, i, key[ i ], true, emptyGet, raw ); diff --git a/src/core/toType.js b/src/core/toType.js new file mode 100644 index 000000000..c77ba95ad --- /dev/null +++ b/src/core/toType.js @@ -0,0 +1,20 @@ +define( [ + "../var/class2type", + "../var/toString" +], function( class2type, toString ) { + +"use strict"; + +function toType( obj ) { + if ( obj == null ) { + return obj + ""; + } + + // Support: Android <=2.3 only (functionish RegExp) + return typeof obj === "object" || typeof obj === "function" ? + class2type[ toString.call( obj ) ] || "object" : + typeof obj; +} + +return toType; +} ); diff --git a/src/deprecated.js b/src/deprecated.js index f1b7db7c6..c11b0d332 100644 --- a/src/deprecated.js +++ b/src/deprecated.js @@ -2,12 +2,13 @@ define( [ "./core", "./core/nodeName", "./core/camelCase", + "./core/toType", "./var/isFunction", "./var/isWindow", "./var/slice", "./event/alias" -], function( jQuery, nodeName, camelCase, isFunction, isWindow, slice ) { +], function( jQuery, nodeName, camelCase, toType, isFunction, isWindow, slice ) { "use strict"; @@ -76,6 +77,7 @@ jQuery.nodeName = nodeName; jQuery.isFunction = isFunction; jQuery.isWindow = isWindow; jQuery.camelCase = camelCase; +jQuery.type = toType; jQuery.now = Date.now; diff --git a/src/manipulation/buildFragment.js b/src/manipulation/buildFragment.js index bcb508531..782de0c61 100644 --- a/src/manipulation/buildFragment.js +++ b/src/manipulation/buildFragment.js @@ -1,11 +1,12 @@ define( [ "../core", + "../core/toType", "./var/rtagName", "./var/rscriptType", "./wrapMap", "./getAll", "./setGlobalEval" -], function( jQuery, rtagName, rscriptType, wrapMap, getAll, setGlobalEval ) { +], function( jQuery, toType, rtagName, rscriptType, wrapMap, getAll, setGlobalEval ) { "use strict"; @@ -24,7 +25,7 @@ function buildFragment( elems, context, scripts, selection, ignored ) { if ( elem || elem === 0 ) { // Add nodes directly - if ( jQuery.type( elem ) === "object" ) { + if ( toType( elem ) === "object" ) { // Support: Android <=4.0 only, PhantomJS 1 only // push.apply(_, arraylike) throws on ancient WebKit diff --git a/src/serialize.js b/src/serialize.js index 635d5b646..30fcf98cc 100644 --- a/src/serialize.js +++ b/src/serialize.js @@ -1,11 +1,12 @@ define( [ "./core", + "./core/toType", "./manipulation/var/rcheckableType", "./var/isFunction", "./core/init", "./traversing", // filter "./attributes/prop" -], function( jQuery, rcheckableType, isFunction ) { +], function( jQuery, toType, rcheckableType, isFunction ) { "use strict"; @@ -39,7 +40,7 @@ function buildParams( prefix, obj, traditional, add ) { } } ); - } else if ( !traditional && jQuery.type( obj ) === "object" ) { + } else if ( !traditional && toType( obj ) === "object" ) { // Serialize object item. for ( name in obj ) { diff --git a/test/unit/ajax.js b/test/unit/ajax.js index ab5ce24a8..e1ecd2036 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -937,7 +937,7 @@ QUnit.module( "ajax", { dataType: "jsonp", crossDomain: crossDomain, success: function( data ) { - assert.strictEqual( jQuery.type( data ), "array", "JSON results returned (GET, REST-like with param)" ); + assert.ok( Array.isArray( data ), "JSON results returned (GET, REST-like with param)" ); } } ]; diff --git a/test/unit/basic.js b/test/unit/basic.js index 028492c2c..d01ee5346 100644 --- a/test/unit/basic.js +++ b/test/unit/basic.js @@ -76,17 +76,13 @@ QUnit.test( "show/hide", function( assert ) { } QUnit.test( "core", function( assert ) { - assert.expect( 21 ); + assert.expect( 17 ); var elem = jQuery( "
" ); assert.strictEqual( elem.length, 2, "Correct number of elements" ); assert.strictEqual( jQuery.trim( " hello " ), "hello", "jQuery.trim" ); - assert.strictEqual( jQuery.type( null ), "null", "jQuery.type(null)" ); - assert.strictEqual( jQuery.type( undefined ), "undefined", "jQuery.type(undefined)" ); - assert.strictEqual( jQuery.type( "a" ), "string", "jQuery.type(String)" ); - assert.ok( jQuery.isPlainObject( { "a": 2 } ), "jQuery.isPlainObject(object)" ); assert.ok( !jQuery.isPlainObject( "foo" ), "jQuery.isPlainObject(String)" ); diff --git a/test/unit/core.js b/test/unit/core.js index 85439d643..fba05b0ec 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -238,58 +238,6 @@ QUnit.test( "trim", function( assert ) { assert.equal( jQuery.trim( "\uFEFF \xA0! | \uFEFF" ), "! |", "leading/trailing should be trimmed" ); } ); -QUnit.test( "type", function( assert ) { - assert.expect( 28 ); - - assert.equal( jQuery.type( null ), "null", "null" ); - assert.equal( jQuery.type( undefined ), "undefined", "undefined" ); - assert.equal( jQuery.type( true ), "boolean", "Boolean" ); - assert.equal( jQuery.type( false ), "boolean", "Boolean" ); - assert.equal( jQuery.type( Boolean( true ) ), "boolean", "Boolean" ); - assert.equal( jQuery.type( 0 ), "number", "Number" ); - assert.equal( jQuery.type( 1 ), "number", "Number" ); - assert.equal( jQuery.type( Number( 1 ) ), "number", "Number" ); - assert.equal( jQuery.type( "" ), "string", "String" ); - assert.equal( jQuery.type( "a" ), "string", "String" ); - assert.equal( jQuery.type( String( "a" ) ), "string", "String" ); - assert.equal( jQuery.type( {} ), "object", "Object" ); - assert.equal( jQuery.type( /foo/ ), "regexp", "RegExp" ); - assert.equal( jQuery.type( new RegExp( "asdf" ) ), "regexp", "RegExp" ); - assert.equal( jQuery.type( [ 1 ] ), "array", "Array" ); - assert.equal( jQuery.type( new Date() ), "date", "Date" ); - assert.equal( jQuery.type( new Function( "return;" ) ), "function", "Function" ); - assert.equal( jQuery.type( function() {} ), "function", "Function" ); - assert.equal( jQuery.type( new Error() ), "error", "Error" ); - assert.equal( jQuery.type( window ), "object", "Window" ); - assert.equal( jQuery.type( document ), "object", "Document" ); - assert.equal( jQuery.type( document.body ), "object", "Element" ); - assert.equal( jQuery.type( document.createTextNode( "foo" ) ), "object", "TextNode" ); - assert.equal( jQuery.type( document.getElementsByTagName( "*" ) ), "object", "NodeList" ); - - // Avoid Lint complaints - var MyString = String, - MyNumber = Number, - MyBoolean = Boolean, - MyObject = Object; - assert.equal( jQuery.type( new MyBoolean( true ) ), "boolean", "Boolean" ); - assert.equal( jQuery.type( new MyNumber( 1 ) ), "number", "Number" ); - assert.equal( jQuery.type( new MyString( "a" ) ), "string", "String" ); - assert.equal( jQuery.type( new MyObject() ), "object", "Object" ); -} ); - -QUnit.test( "type for `Symbol`", function( assert ) { - // Prevent reference errors - if ( typeof Symbol !== "function" ) { - assert.expect( 0 ); - return; - } - - assert.expect( 2 ); - - assert.equal( jQuery.type( Symbol() ), "symbol", "Symbol" ); - assert.equal( jQuery.type( Object( Symbol() ) ), "symbol", "Symbol" ); -} ); - QUnit.asyncTest( "isPlainObject", function( assert ) { assert.expect( 23 ); @@ -1328,7 +1276,7 @@ QUnit.test( "jQuery.parseHTML", function( assert ) { nodes = jQuery.parseHTML( jQuery( "body" )[ 0 ].innerHTML ); assert.ok( nodes.length > 4, "Parse a large html string" ); - assert.equal( jQuery.type( nodes ), "array", "parseHTML returns an array rather than a nodelist" ); + assert.ok( Array.isArray( nodes ), "parseHTML returns an array rather than a nodelist" ); html = ""; assert.equal( jQuery.parseHTML( html ).length, 0, "Ignore scripts by default" ); diff --git a/test/unit/deferred.js b/test/unit/deferred.js index 9ba97181d..2e414e642 100644 --- a/test/unit/deferred.js +++ b/test/unit/deferred.js @@ -50,7 +50,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) { assert.strictEqual( funcPromise, func, "non objects get extended" ); jQuery.each( promise, function( key ) { if ( typeof promise[ key ] !== "function" ) { - assert.ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" ); + assert.ok( false, key + " is a function (" + typeof( promise[ key ] ) + ")" ); } if ( promise[ key ] !== func[ key ] ) { assert.strictEqual( func[ key ], promise[ key ], key + " is the same" ); diff --git a/test/unit/deprecated.js b/test/unit/deprecated.js index 007b3f13f..aaab68beb 100644 --- a/test/unit/deprecated.js +++ b/test/unit/deprecated.js @@ -242,14 +242,52 @@ QUnit.test( "jQuery.nodeName", function( assert ) { } ); -QUnit.test( "core", function( assert ) { +QUnit.test( "type", function( assert ) { + assert.expect( 28 ); + + assert.equal( jQuery.type( null ), "null", "null" ); + assert.equal( jQuery.type( undefined ), "undefined", "undefined" ); + assert.equal( jQuery.type( true ), "boolean", "Boolean" ); + assert.equal( jQuery.type( false ), "boolean", "Boolean" ); + assert.equal( jQuery.type( Boolean( true ) ), "boolean", "Boolean" ); + assert.equal( jQuery.type( 0 ), "number", "Number" ); + assert.equal( jQuery.type( 1 ), "number", "Number" ); + assert.equal( jQuery.type( Number( 1 ) ), "number", "Number" ); + assert.equal( jQuery.type( "" ), "string", "String" ); + assert.equal( jQuery.type( "a" ), "string", "String" ); + assert.equal( jQuery.type( String( "a" ) ), "string", "String" ); + assert.equal( jQuery.type( {} ), "object", "Object" ); + assert.equal( jQuery.type( /foo/ ), "regexp", "RegExp" ); + assert.equal( jQuery.type( new RegExp( "asdf" ) ), "regexp", "RegExp" ); + assert.equal( jQuery.type( [ 1 ] ), "array", "Array" ); + assert.equal( jQuery.type( new Date() ), "date", "Date" ); + assert.equal( jQuery.type( new Function( "return;" ) ), "function", "Function" ); + assert.equal( jQuery.type( function() {} ), "function", "Function" ); + assert.equal( jQuery.type( new Error() ), "error", "Error" ); + assert.equal( jQuery.type( window ), "object", "Window" ); + assert.equal( jQuery.type( document ), "object", "Document" ); + assert.equal( jQuery.type( document.body ), "object", "Element" ); + assert.equal( jQuery.type( document.createTextNode( "foo" ) ), "object", "TextNode" ); + assert.equal( jQuery.type( document.getElementsByTagName( "*" ) ), "object", "NodeList" ); + + // Avoid Lint complaints + var MyString = String, + MyNumber = Number, + MyBoolean = Boolean, + MyObject = Object; + assert.equal( jQuery.type( new MyBoolean( true ) ), "boolean", "Boolean" ); + assert.equal( jQuery.type( new MyNumber( 1 ) ), "number", "Number" ); + assert.equal( jQuery.type( new MyString( "a" ) ), "string", "String" ); + assert.equal( jQuery.type( new MyObject() ), "object", "Object" ); +} ); + +QUnit[ typeof Symbol === "function" ? "test" : "skip" ]( "type for `Symbol`", function( assert ) { assert.expect( 2 ); - assert.ok( jQuery.isFunction( jQuery.noop ), "jQuery.isFunction(jQuery.noop)" ); - assert.ok( !jQuery.isFunction( 2 ), "jQuery.isFunction(Number)" ); + assert.equal( jQuery.type( Symbol() ), "symbol", "Symbol" ); + assert.equal( jQuery.type( Object( Symbol() ) ), "symbol", "Symbol" ); } ); - QUnit.test( "isFunction", function( assert ) { assert.expect( 20 );