From a2e251141fd2be728016fffba4c05c3c511d4a35 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 30 Nov 2010 01:59:29 -0700 Subject: [PATCH] using jQuery.type for typeOf --- README.md | 5 +++-- lib/remedial.js | 24 ++++++++++++------------ tests/array-wtf.js | 2 +- tests/types.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 tests/types.js diff --git a/README.md b/README.md index 25e578b..d11f919 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,10 @@ This works in both the Browser and SSJS. Notes ---- -This uses the ["Flanagan / Miller device"](http://groups.google.com/group/nodejs/msg/0670a986a2906aeb) rather than the Crockford's original. +`typeOf` is taken from `jQuery.type`, which is more accurate than Crockford's original and even simpler +than the ["Flanagan / Miller device"](http://groups.google.com/group/nodejs/msg/0670a986a2906aeb). -There is [a more specific typeof()](http://rolandog.com/archives/2007/01/18/typeof-a-more-specific-typeof/) implementation worthy of consideration. +There is [a more specific typeof()](http://rolandog.com/archives/2007/01/18/typeof-a-more-specific-typeof/) implementation also worthy of consideration. Globals ==== diff --git a/lib/remedial.js b/lib/remedial.js index 21bece8..74ec508 100644 --- a/lib/remedial.js +++ b/lib/remedial.js @@ -1,18 +1,18 @@ /*jslint onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */ "use strict"; (function () { - function typeOf(value) { - var s = typeof value; - if (s === 'object') { - if (value) { - if ((/array/i).test(({}).toString.call(value))) { - s = 'array'; - } - } else { - s = 'null'; - } - } - return s; + var classes = "Boolean Number String Function Array Date RegExp Object".split(" "), + i, + name, + class2type = {}; + + for (i in classes) { + name = classes[i]; + class2type["[object " + name + "]"] = name.toLowerCase(); + } + + function typeOf(obj) { + return (null === obj || undefined === obj) ? String(obj) : class2type[Object.prototype.toString.call(obj)] || "object"; } function isEmpty(o) { diff --git a/tests/array-wtf.js b/tests/array-wtf.js index cf860f7..127196f 100644 --- a/tests/array-wtf.js +++ b/tests/array-wtf.js @@ -24,7 +24,7 @@ // Expected: 3 // Node/V8/FF: 0 console.log(typeOf(b)); - // Expected: Object + // Expected: Object (with Flanagan / Miller device or jQuery's type) // Node/V8/FF: array (with Crockford's original) diff --git a/tests/types.js b/tests/types.js new file mode 100644 index 0000000..a79b6e1 --- /dev/null +++ b/tests/types.js @@ -0,0 +1,31 @@ +(function () { + require('../lib/remedial'); + var n = null, + u; + + if ( + 'object' === typeOf(Object.create([])) && + 'object' === typeOf(Object.create(function () {})) && + 'array' === typeOf([]) && + 'string' === typeOf('') && + 'regexp' === typeOf(/ /) && + 'number' === typeOf(0) && + 'function' === typeOf(function () {}) && + 'function' === typeOf((function () { + var a = function () {}; + a.foo = 'bar'; + return a; + }())) && + 'boolean' === typeOf(true) && + 'boolean' === typeOf(false) && + 'date' === typeOf(new Date()) && + 'undefined' === typeOf(u) && + 'undefined' === typeOf(undefined) && + 'null' === typeOf(n) && + 'object' === typeOf({}) + ) { + console.log('passed type detections') + } else { + console.log('failed type detections') + } +}());