This commit is contained in:
AJ ONeal 2018-05-09 18:17:38 -06:00
parent ba3f718ea5
commit da308e71ef
5 changed files with 48 additions and 16 deletions

View File

@ -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
==== ====

View File

@ -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) {

View File

@ -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"
} }

View File

@ -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)

31
tests/types.js Normal file
View File

@ -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')
}
}());