Skip to content

Instantly share code, notes, and snippets.

@megawac
Last active August 29, 2015 13:59
Show Gist options
  • Save megawac/10954855 to your computer and use it in GitHub Desktop.
Save megawac/10954855 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<link href="//cdn.jsdelivr.net/qunit/1.14.0/qunit.css" rel="stylesheet" type="text/css" />
<script src="//cdn.jsdelivr.net/qunit/1.14.0/qunit.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://rawgithub.com/megawac/10954855/raw/134b2e49935fa3ce90226b31c41b8ac74858a26e/tests.js"></script>
</body>
</html>
function isType(object, type){
return object != null && type ? object.constructor.name === type.name : object === type;
}
function typeWrap(Class) {
return function(object) {
return isType(object, Class);
};
}
window._ = {
isArray: typeWrap(Array),
isString: typeWrap(String),
isNumber: typeWrap(Number),
isBoolean: typeWrap(Boolean),
isFunction: typeWrap(Function),
isDate: typeWrap(Date),
isNaN: typeWrap(NaN),
isNull: typeWrap(null),
isUndefined: typeWrap(void 0)
}
//tests
test('isArray', function() {
ok(!_.isArray(undefined), 'undefined vars are not arrays');
ok(!_.isArray(arguments), 'the arguments object is not an array');
ok(_.isArray([1, 2, 3]), 'but arrays are');
});
test('isString', function() {
var obj = new String('I am a string object');
ok(!_.isString(document.body), 'the document body is not a string');
ok(_.isString([1, 2, 3].join(', ')), 'but strings are');
ok(_.isString('I am a string literal'), 'string literals are');
ok(_.isString(obj), 'so are String objects');
});
test('isNumber', function() {
ok(!_.isNumber('string'), 'a string is not a number');
ok(!_.isNumber(arguments), 'the arguments object is not a number');
ok(!_.isNumber(undefined), 'undefined is not a number');
ok(_.isNumber(3 * 4 - 7 / 10), 'but numbers are');
ok(_.isNumber(NaN), 'NaN *is* a number');
ok(_.isNumber(Infinity), 'Infinity is a number');
ok(!_.isNumber('1'), 'numeric strings are not numbers');
});
test('isBoolean', function() {
ok(!_.isBoolean(2), 'a number is not a boolean');
ok(!_.isBoolean('string'), 'a string is not a boolean');
ok(!_.isBoolean('false'), 'the string "false" is not a boolean');
ok(!_.isBoolean('true'), 'the string "true" is not a boolean');
ok(!_.isBoolean(arguments), 'the arguments object is not a boolean');
ok(!_.isBoolean(undefined), 'undefined is not a boolean');
ok(!_.isBoolean(NaN), 'NaN is not a boolean');
ok(!_.isBoolean(null), 'null is not a boolean');
ok(_.isBoolean(true), 'but true is');
ok(_.isBoolean(false), 'and so is false');
});
test('isFunction', function() {
ok(!_.isFunction(undefined), 'undefined vars are not functions');
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
ok(!_.isFunction('moe'), 'strings are not functions');
ok(_.isFunction(_.isFunction), 'but functions are');
ok(_.isFunction(function(){}), 'even anonymous ones');
});
test('isDate', function() {
ok(!_.isDate(100), 'numbers are not dates');
ok(!_.isDate({}), 'objects are not dates');
ok(_.isDate(new Date()), 'but dates are');
});
test('isRegExp', function() {
ok(!_.isRegExp(_.identity), 'functions are not RegExps');
ok(_.isRegExp(/identity/), 'but RegExps are');
});
test('isFinite', function() {
ok(!_.isFinite(undefined), 'undefined is not finite');
ok(!_.isFinite(null), 'null is not finite');
ok(!_.isFinite(NaN), 'NaN is not finite');
ok(!_.isFinite(Infinity), 'Infinity is not finite');
ok(!_.isFinite(-Infinity), '-Infinity is not finite');
ok(_.isFinite('12'), 'Numeric strings are numbers');
ok(!_.isFinite('1a'), 'Non numeric strings are not numbers');
ok(!_.isFinite(''), 'Empty strings are not numbers');
var obj = new Number(5);
ok(_.isFinite(obj), 'Number instances can be finite');
ok(_.isFinite(0), '0 is finite');
ok(_.isFinite(123), 'Ints are finite');
ok(_.isFinite(-12.44), 'Floats are finite');
});
test('isNaN', function() {
ok(!_.isNaN(undefined), 'undefined is not NaN');
ok(!_.isNaN(null), 'null is not NaN');
ok(!_.isNaN(0), '0 is not NaN');
ok(_.isNaN(NaN), 'but NaN is');
ok(_.isNaN(new Number(NaN)), 'wrapped NaN is still NaN');
});
test('isNull', function() {
ok(!_.isNull(undefined), 'undefined is not null');
ok(!_.isNull(NaN), 'NaN is not null');
ok(_.isNull(null), 'but null is');
ok(_.isNull(iNull), 'even from another frame');
});
test('isUndefined', function() {
ok(!_.isUndefined(1), 'numbers are defined');
ok(!_.isUndefined(null), 'null is defined');
ok(!_.isUndefined(false), 'false is defined');
ok(!_.isUndefined(NaN), 'NaN is defined');
ok(_.isUndefined(), 'nothing is undefined');
ok(_.isUndefined(undefined), 'undefined is undefined');
ok(_.isUndefined(iUndefined), 'even from another frame');
});
if (window.ActiveXObject) {
test('IE host objects', function() {
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0');
ok(!_.isNumber(xml));
ok(!_.isBoolean(xml));
ok(!_.isNaN(xml));
ok(!_.isFunction(xml));
ok(!_.isNull(xml));
ok(!_.isUndefined(xml));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment