v1.0.3
This commit is contained in:
parent
ba3f718ea5
commit
da308e71ef
|
@ -12,9 +12,10 @@ This works in both the Browser and SSJS.
|
||||||
Notes
|
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
|
Globals
|
||||||
====
|
====
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
/*jslint onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */
|
/*jslint onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */
|
||||||
"use strict";
|
"use strict";
|
||||||
(function () {
|
(function () {
|
||||||
function typeOf(value) {
|
var classes = "Boolean Number String Function Array Date RegExp Object".split(" "),
|
||||||
var s = typeof value;
|
i,
|
||||||
if (s === 'object') {
|
name,
|
||||||
if (value) {
|
class2type = {};
|
||||||
if ((/array/i).test(({}).toString.call(value))) {
|
|
||||||
s = 'array';
|
for (i in classes) {
|
||||||
|
name = classes[i];
|
||||||
|
class2type["[object " + name + "]"] = name.toLowerCase();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
s = 'null';
|
function typeOf(obj) {
|
||||||
}
|
return (null === obj || undefined === obj) ? String(obj) : class2type[Object.prototype.toString.call(obj)] || "object";
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isEmpty(o) {
|
function isEmpty(o) {
|
||||||
|
|
|
@ -19,5 +19,5 @@
|
||||||
"main": "./lib/remedial",
|
"main": "./lib/remedial",
|
||||||
"name": "remedial",
|
"name": "remedial",
|
||||||
"homepage": "https://git.coolaj86.com/coolaj86/remedial.js",
|
"homepage": "https://git.coolaj86.com/coolaj86/remedial.js",
|
||||||
"version": "1.0.2"
|
"version": "1.0.3"
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
// Expected: 3
|
// Expected: 3
|
||||||
// Node/V8/FF: 0
|
// Node/V8/FF: 0
|
||||||
console.log(typeOf(b));
|
console.log(typeOf(b));
|
||||||
// Expected: Object
|
// Expected: Object (with Flanagan / Miller device or jQuery's type)
|
||||||
// Node/V8/FF: array (with Crockford's original)
|
// Node/V8/FF: array (with Crockford's original)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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')
|
||||||
|
}
|
||||||
|
}());
|
Loading…
Reference in New Issue