Comparing version 0.0.4 to 0.0.5
@@ -15,3 +15,3 @@ /** | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
@@ -25,3 +25,3 @@ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* | ||
* isA.js | ||
* isA.js | ||
* | ||
@@ -34,3 +34,3 @@ * Reusable node.js/browser JavaScript library that exposes some isXXX() type | ||
* If this library is included in a browser via a script tag, all of it's | ||
* components will be placed in the window scope. | ||
* components will be placed in the window scope. | ||
* | ||
@@ -50,3 +50,3 @@ * If this library is require()'ed in node.js, it will return a function that | ||
* @author Gabriel Harrison (nyteshade AT gmail.com) | ||
* @version 0.0.4 | ||
* @version 0.0.5 | ||
*/ | ||
@@ -56,7 +56,21 @@ (function() { | ||
var hasExports = typeof(exports) != 'undefined', | ||
namespace = hasExports ? exports : | ||
(typeof (window) != 'undefined') ? window : | ||
(typeof (global) != 'undefined') ? global : null; | ||
function exists(variableName) { | ||
var result; | ||
try { result = eval('(' + variableName + ')'); } | ||
catch (e) { | ||
result = undefined; | ||
} | ||
return result; | ||
} | ||
var myExports = exists('exports'), | ||
myModule = exists('module'), | ||
myWindow = exists('window'), | ||
myGlobal = exists('global'), | ||
hasExports = myExports !== undefined, | ||
namespace = hasExports ? myExports : | ||
(myWindow !== undefined) ? myWindow : | ||
(myGlobal !== undefined) ? myGlobal : null, | ||
library, i, backRef; | ||
if (!namespace) { | ||
@@ -67,3 +81,5 @@ console.error('Cannot locate place to export library to!'); | ||
var library = (function(exports) { | ||
library = (function(exports) { | ||
var _t, _t2, _count = 0, _unique = 0, _questionable = [], unique; | ||
/** | ||
@@ -93,3 +109,3 @@ * Declare an array of Strings reported by the JavaScript VM to be the | ||
* instances of the type for comparison. */ | ||
var backRef = exports.IS_A_BACKREF = [ | ||
backRef = exports.IS_A_BACKREF = [ | ||
undefined, exports.IS_A.UNDEFINED, | ||
@@ -109,3 +125,3 @@ Function, exports.IS_A.FUNCTION, | ||
backRef.indexOf = function(obj) { | ||
for (var i = 0; i < backRef.length; i++) { | ||
for (i = 0; i < backRef.length; i++) { | ||
if (backRef[i] === obj) { | ||
@@ -122,3 +138,5 @@ return i; | ||
var pos = this.indexOf(baseConstructor); | ||
if (pos === -1) { return Object.prototype.toString.call(baseConstructor); } | ||
if (pos === -1) { | ||
return Object.prototype.toString.call(baseConstructor); | ||
} | ||
return this[pos + 1]; | ||
@@ -129,6 +147,5 @@ }; | ||
* measure the accuracy of our comparison "constants" */ | ||
var _t, _t2, _count = 0, _unique = 0, _questionable = []; | ||
for (_t in exports.IS_A) { | ||
if (exports.IS_A.hasOwnProperty(_t)) { | ||
var unique = true; | ||
unique = true; | ||
for (_t2 in exports.IS_A) { | ||
@@ -145,4 +162,4 @@ if (exports.IS_A.hasOwnProperty(_t2)) { | ||
if (unique) { _unique++; } | ||
_count++; | ||
} | ||
_count++; | ||
} | ||
@@ -161,3 +178,3 @@ | ||
console.warn('IS_A dictionary reported non-unique value pairs: ', | ||
exports.IS_A_QUESTIONABLE); | ||
exports.IS_A_QUESTIONABLE); | ||
} | ||
@@ -171,11 +188,13 @@ | ||
exports.isA = function(type, object) { | ||
var result, typeString; | ||
if (arguments.length === 1) { | ||
return Object.prototype.toString.call(type) | ||
result = Object.prototype.toString.call(type); | ||
} | ||
else { | ||
var typeString = Object.prototype.toString.call(object); | ||
return typeString === type | ||
typeString = Object.prototype.toString.call(object); | ||
result = typeString === type | ||
|| typeString === exports.IS_A[type] | ||
|| typeString === backRef.matchingType(type); | ||
} | ||
return result; | ||
}; | ||
@@ -301,6 +320,8 @@ | ||
exports.INJECT_IS_A_LIB = function(destinationScope) { | ||
var property; | ||
if (destinationScope === null || destinationScope === undefined) { | ||
throw new Error("Cannot locate destination scope to install to."); | ||
} | ||
for (var property in exports) { | ||
for (property in exports) { | ||
if (exports.hasOwnProperty(property)) { | ||
@@ -314,16 +335,18 @@ destinationScope[property] = exports[property]; | ||
return exports; | ||
})({}); | ||
}({})); | ||
if (hasExports && typeof module !== 'undefined') { | ||
module.exports = function(destination) { | ||
if (hasExports && myModule !== undefined) { | ||
myModule.exports = function(destination) { | ||
var result; | ||
if (!destination) { | ||
return library; | ||
result = library; | ||
} | ||
else { | ||
return library.INJECT_IS_A_LIB(destination); | ||
result = library.INJECT_IS_A_LIB(destination); | ||
} | ||
return result; | ||
}; | ||
} | ||
else if (typeof window !== 'undefined') { | ||
library.INJECT_IS_A_LIB(window); | ||
else if (myWindow !== undefined) { | ||
library.INJECT_IS_A_LIB(myWindow); | ||
} | ||
@@ -333,4 +356,17 @@ else { | ||
} | ||
})(); | ||
}()); | ||
/*jslint browser:true,indent:2,node:true*/ | ||
/** JSLint Hinting */ | ||
/*properties | ||
ARRAY, BOOLEAN, ERROR, FUNCTION, INJECT_IS_A_LIB, IS_A, IS_A_ACCURACY, | ||
IS_A_BACKREF, IS_A_QUESTIONABLE, NULL, NUMBER, OBJECT, REGEXP, STRING, | ||
UNDEFINED, call, error, exports, hasOwnProperty, indexOf, isA, isArray, | ||
isBoolean, isError, isFunction, isNull, isNumber, isObject, isRegExp, | ||
isString, isUndefined, length, matchingType, prototype, push, toString, | ||
warn | ||
*/ | ||
/*jslint | ||
browser: true, devel: true, node: true, continue: true, evil: true, | ||
nomen: true, plusplus: true, unparam: true, white: true, indent: 2, | ||
maxlen: 80 | ||
*/ |
@@ -5,3 +5,3 @@ { | ||
"description": "Reusable Object.prototype.toString type checking library", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "url": "https://github.com/nyteshade/isA.git" |
16501
330