Skip to content

Instantly share code, notes, and snippets.

@josher19
Created January 11, 2010 09:38
Show Gist options
  • Save josher19/274112 to your computer and use it in GitHub Desktop.
Save josher19/274112 to your computer and use it in GitHub Desktop.
/** testClass return true if obj is of class klass or a superclass of klass (such as Object). */
function testClass(obj, klass) {
// try {
if (obj === null) return "null" === klass || null === klass;
if (typeof(obj) === "undefined") return "undefined" === klass || obj === klass;
if (typeof(obj) === klass) return true; // if klass is a String
//// uncomment next line to make testClass(5, "NUMBER") return true
if (typeof(obj) === klass.toString().toLowerCase()) return true;
if (obj.constructor === klass) return true;
//// uncomment next line to make testClass([], Object) return false:
//if (obj.constructor === Object) return klass === Object;
//// Should testClass(tom, "Horse") return true? No easy way to make testClass(tom, "Animal") true.
// if (obj.constructor && typeof(klass) === "string") return obj.constructor.name === klass
if (typeof(klass) !== "function") return false; // instanceof throws errors unless it is a function.
if (obj instanceof klass) return true; // for inheritance
return false;
// } catch(error) {
// throw new ArgTypeError(obj,type,error);
// return false;
// }
}
// This should actually be a Compile Time error while CoffeeScript is compiled, if possible.
function ArgTypeError(obj,type,error) {
return SyntaxError(
"Barf on args. Expected: " + (type.name || type) +
". Got: " + obj +
" of type " +
(obj === null ? "null" :
(obj.constructor ? (obj.constructor.name||obj.constructor) : obj)) +
(error ? "\n\n" + error.stack : "")
);
}
// Unit Testing w/o JSUnit
function test_testClass(undef) {
var unset;
return [
"\nShould be false:\n",
// false
testClass(unset, null),
testClass(unset, false),
testClass(unset, 0),
testClass("123", Number),
testClass(123, String),
testClass([1,2,3], String),
testClass(null, "undefined"),
testClass({}, Array),
testClass(arguments, Array),
"\nShould be true:\n",
// javascript classes
testClass(true, Boolean),
testClass(123, Number),
testClass("123", String),
testClass([1,2,3], Array),
testClass({}, Object),
// typeof for primitives
testClass(true, "boolean"),
testClass(123, "number"),
testClass("123", "string"),
testClass({}, "object"),
testClass({}, "OBJECT"),
// Null and undefined
testClass(null, "null"),
testClass(null, null),
testClass(undef, "undefined"),
testClass(unset, "undefined"),
testClass(unset, undefined),
// Object almost always true (because of inheritance / instanceof)
testClass(new Boolean(false), Object),
testClass(new Number(123.4), Object),
testClass(new String("123"), Object),
testClass([1,2,3], Object),
testClass([1,2,3], "object"),
[].constructor !== Object,
"\nShould be false:\n",
// Except for primitives:
testClass(true, Object),
testClass(false, Object),
testClass(123.4, Object),
testClass("123", Object),
testClass(new Number(123.4).toString(), Object),
testClass(new String("123").toString(), Object),
"\nCheck if CoffeeScript Examples loaded:\n",
// True if CoffeeScript Examples are loaded
typeof sam != "undefined" && testClass(sam, Snake) ,
typeof sam != "undefined" && testClass(sam, Animal),
typeof tom != "undefined" && testClass(tom, Horse),
typeof tom != "undefined" && testClass(tom, Animal),
"\nDONE!!"
];
}
test_testClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment