Skip to content

Commit

Permalink
Core: deprecate jQuery.type
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Dec 19, 2017
1 parent ecd8dde commit e3e1db1
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 105 deletions.
5 changes: 3 additions & 2 deletions src/callbacks.js
@@ -1,7 +1,8 @@
define( [
"./core",
"./core/toType",
"./var/rnothtmlwhite"
], function( jQuery, rnothtmlwhite ) {
], function( jQuery, toType, rnothtmlwhite ) {

"use strict";

Expand Down Expand Up @@ -129,7 +130,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 );
Expand Down
29 changes: 6 additions & 23 deletions src/core.js
Expand Up @@ -10,17 +10,17 @@ define( [
"./var/concat",
"./var/push",
"./var/indexOf",
"./var/class2type",
"./var/toString",
"./var/hasOwn",
"./var/fnToString",
"./var/ObjectFunctionString",
"./var/support",
"./var/isWindow",
"./core/DOMEval"
"./core/DOMEval",
"./core/toType"
], function( arr, document, getProto, slice, concat, push, indexOf,
class2type, toString, hasOwn, fnToString, ObjectFunctionString,
support, isWindow, DOMEval ) {
toString, hasOwn, fnToString, ObjectFunctionString,
support, isWindow, DOMEval, toType ) {

"use strict";

Expand Down Expand Up @@ -226,7 +226,7 @@ jQuery.extend( {
// 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 );
var type = toType( obj );
return ( type === "number" || type === "string" ) &&

// parseFloat NaNs numeric-cast false positives ("")
Expand Down Expand Up @@ -268,17 +268,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 );
Expand Down Expand Up @@ -451,20 +440,14 @@ if ( typeof Symbol === "function" ) {
jQuery.fn[ Symbol.iterator ] = arr[ Symbol.iterator ];
}

// Populate the class2type map
jQuery.each( "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " ),
function( i, name ) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
} );

function isArrayLike( obj ) {

// Support: real iOS 8.2 only (not reproducible in simulator)
// `in` check used to prevent JIT error (gh-2145)
// 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 ( jQuery.isFunction( obj ) || isWindow( obj ) ) {
return false;
Expand Down
7 changes: 4 additions & 3 deletions src/core/access.js
@@ -1,6 +1,7 @@
define( [
"../core"
], function( jQuery ) {
"../core",
"../core/toType"
], function( jQuery, toType ) {

"use strict";

Expand All @@ -12,7 +13,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 );
Expand Down
28 changes: 28 additions & 0 deletions src/core/toType.js
@@ -0,0 +1,28 @@
define( function() {
"use strict";

var class2type = {
"[object Boolean": "boolean",
"[object Number]": "number",
"[object String]": "string",
"[object Function]": "function",
"[object Array]": "array",
"[object Date]": "date",
"[object RegExp]": "regexp",
"[object Error]": "error",
"[object Symbol]": "symbol"
};

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;
} );
4 changes: 3 additions & 1 deletion src/deprecated.js
@@ -1,8 +1,9 @@
define( [
"./core",
"./core/nodeName",
"./core/toType",
"./var/isWindow"
], function( jQuery, nodeName, isWindow ) {
], function( jQuery, nodeName, toType, isWindow ) {

"use strict";

Expand Down Expand Up @@ -38,5 +39,6 @@ jQuery.isArray = Array.isArray;
jQuery.parseJSON = JSON.parse;
jQuery.nodeName = nodeName;
jQuery.isWindow = isWindow;
jQuery.type = toType;

} );
5 changes: 3 additions & 2 deletions 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";

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/serialize.js
@@ -1,10 +1,11 @@
define( [
"./core",
"./core/toType",
"./manipulation/var/rcheckableType",
"./core/init",
"./traversing", // filter
"./attributes/prop"
], function( jQuery, rcheckableType ) {
], function( jQuery, toType, rcheckableType ) {

"use strict";

Expand Down Expand Up @@ -38,7 +39,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 ) {
Expand Down
6 changes: 0 additions & 6 deletions src/var/class2type.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/var/hasOwn.js
@@ -1,7 +1,7 @@
define( [
"./class2type"
], function( class2type ) {
"./support"
], function( support ) {
"use strict";

return class2type.hasOwnProperty;
return support.hasOwnProperty;
} );
6 changes: 3 additions & 3 deletions src/var/toString.js
@@ -1,7 +1,7 @@
define( [
"./class2type"
], function( class2type ) {
"./support"
], function( support ) {
"use strict";

return class2type.toString;
return support.toString;
} );
2 changes: 1 addition & 1 deletion test/unit/ajax.js
Expand Up @@ -933,7 +933,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)" );
}
}
];
Expand Down
6 changes: 1 addition & 5 deletions test/unit/basic.js
Expand Up @@ -76,17 +76,13 @@ QUnit.test( "show/hide", function( assert ) {
}

QUnit.test( "core", function( assert ) {
assert.expect( 25 );
assert.expect( 22 );

var elem = jQuery( "<div></div><span></span>" );

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)" );

Expand Down
54 changes: 1 addition & 53 deletions test/unit/core.js
Expand Up @@ -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 );
Expand Down Expand Up @@ -1598,7 +1546,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 = "<script>undefined()</script>";
assert.equal( jQuery.parseHTML( html ).length, 0, "Ignore scripts by default" );
Expand Down
2 changes: 1 addition & 1 deletion test/unit/deferred.js
Expand Up @@ -50,7 +50,7 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) {
assert.strictEqual( funcPromise, func, "non objects get extended" );
jQuery.each( promise, function( key ) {
if ( !jQuery.isFunction( promise[ key ] ) ) {
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" );
Expand Down

0 comments on commit e3e1db1

Please sign in to comment.