Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@ $(document).ready(function() {
ok(_.isUndefined(iUndefined), 'even from another frame');
});

test("objects: toType", function() {
ok(_.toType(1) === "number", '1 is a "number"');
ok(_.toType(-Infinity) === "number", '-Infinity is a "number"');
ok(_.toType(new Number) === "Number", '`new Number` is a "Number"');
ok(_.toType(NaN) === "Number", 'NaN is a "Number" (for realz)');
ok(_.toType(function(){}) === "Function", '`function(){}` is a "Function"');
ok(_.toType(window.alert) === "Function", '`window.alert` is a "Function"');
ok(_.toType(null) === "Null", 'null is type "Null"');
ok(_.toType(undefined) === "Undefined", 'while we\'re at it, undefined is "Undefined"');
ok(_.toType("o hai") === "string", '"o hai" is a "string"');
ok(_.toType(new String("o hai")) === "String", 'but `new String("o hai") is a "String"`');
ok(_.toType(false) === "Boolean", 'false is "Boolean"');
ok(_.toType(new Boolean(true)) === "Boolean", '`new Boolean(true)` is also "Boolean"');
ok(_.toType(/a-z/) === "RegExp", '/a-z/ is a "RegExp"');
ok(_.toType({a: 4}) === "Object", '`{a:4}` is type "Object"');
ok(_.toType([1, 2, 3]) === "Array", '`[1,2,3]` is type "Array"');
ok((function() {return _.toType(arguments)})() == "Arguments", 'even the arguments object gets a type of its own, "Arguments"');
ok(_.toType(new ReferenceError) === "Error", '`new ReferenceError` is type "Error"');
ok(_.toType(new Date) === "Date", 'new Date is a "Date"');
ok(_.toType(Math) === "Math", 'Math is type "Math"');
ok(_.toType(JSON) === "JSON", 'JSON is, of course, "JSON" type');
});

if (window.ActiveXObject) {
test("objects: IE host objects", function() {
var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
Expand Down
11 changes: 11 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,17 @@
return obj === void 0;
};

// Robust type detection, uses a regex to match the return from native object.toString()
// Falls back to native 'typeof' for truthy primitive values (credit: Angus Croll)
// http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
_.toType = function(obj) {
if((function() {return obj && (obj !== this)}).call(obj)) {
return typeof obj;
}
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1];
};


// Utility Functions
// -----------------

Expand Down