Skip to content

Commit 5f51b0d

Browse files
committed
- convert to a module
- separate the tests from the code
1 parent 356d7d0 commit 5f51b0d

File tree

4 files changed

+178
-189
lines changed

4 files changed

+178
-189
lines changed

isType.js

Lines changed: 0 additions & 189 deletions
This file was deleted.

isType.mjs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* A more robust 'typeof'.
3+
* https://github.com/wizard04wsu/javascript-type-testing
4+
*
5+
*
6+
* For each of the type testing methods, the only parameter is the item to be tested. These methods do not perform coercion.
7+
*
8+
* is(o) - Returns the item's type.
9+
* - NaN is considered its own type (instead of a number), since it essentially represents something that cannot be converted to a number.
10+
* - For objects, the type of the object is given instead of just 'object' (e.g., 'Array').
11+
*
12+
* is.defined(o)
13+
* is.undefined(o)
14+
* is.null(o)
15+
* is.nan(o) - NaN or a Number object with value NaN
16+
*
17+
* is.array(o)
18+
* is.bigint(o)
19+
* is.boolean(o)
20+
* is.date(o)
21+
* is.function(o)
22+
* is.number(o) - excludes NaN
23+
* is.number.infinite(o)
24+
* is.number.nan(o) - same as is.nan(o)
25+
* is.number.real(o)
26+
* is.regexp(o)
27+
* is.string(o)
28+
* is.symbol(o)
29+
*
30+
* is.object(o)
31+
* is.primitive(o)
32+
*
33+
*
34+
* is.noConflict() - Restores 'is' to what it was before this script replaced it.
35+
*/
36+
37+
function is(o){
38+
if(o === void 0) return "undefined";
39+
const t = typeof o;
40+
if(t === "object"){
41+
if(o === null) return "null";
42+
let name = Object.prototype.toString.call(o).slice(8, -1);
43+
if(name === "Number" && 1*o !== 1*o) return "NaN";
44+
return name; //object type
45+
}
46+
if(o !== o) return "NaN";
47+
return t; //primitive type or "function"
48+
}
49+
50+
function fn(type, eitherCase){
51+
if(eitherCase) return function (o){ return is(o).toLowerCase() === type; };
52+
return function (o){ return is(o) === type; };
53+
}
54+
55+
is.undefined = function (o){ return o === void 0; };
56+
is.defined = function (o){ return o !== void 0; };
57+
is.null = function (o){ return o === null; };
58+
59+
is.nan = fn("NaN"); //NaN or a Number object with value NaN
60+
//Note that JavaScript doesn't correctly treat all undefined forms as NaN (e.g., 1/0 and 0**0).
61+
62+
is.array = fn("Array");
63+
is.bigint = fn("bigint");
64+
is.boolean = fn("boolean", true);
65+
is.date = fn("Date");
66+
is.function = fn("function");
67+
is.number = fn("number", true); //excludes NaN
68+
is.number.nan = is.nan;
69+
is.regexp = fn("RegExp");
70+
is.string = fn("string", true);
71+
is.symbol = fn("symbol");
72+
73+
const infinity = 1/0; //Technically, 1/0 is an undefined form, but JavaScript treats it as Infinity.
74+
is.number.infinite = function (o){ return is.number(o) && Math.abs(o) === infinity; };
75+
is.number.real = function (o){ return is.number(o) && Math.abs(o) !== infinity; };
76+
77+
is.object = function (o){ let t = typeof o; return (t === "object" && o !== null) || t === "function"; };
78+
is.primitive = function (o){ return !is.object(o); };
79+
80+
81+
82+
export { is as default };

test/test.htm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html><head><title>isType test</title><script src="test.js" type="module"></script></head><body>
2+
3+
View the console.
4+
5+
</body></html>

test/test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import is from "../isType.mjs";
2+
3+
let i = 1,
4+
a = function (a,b){ console.assert(a === b, (i++)+". "+a+" !== "+b); };
5+
6+
a(is(), "undefined");
7+
a(is(null), "null");
8+
a(is({}), "Object");
9+
a(is([]), "Array");
10+
a(is(arguments), "Arguments");
11+
if(window.BigInt) a(is(BigInt(5)), "bigint");
12+
else i++;
13+
a(is(false), "boolean");
14+
a(is(new Boolean()), "Boolean");
15+
a(is(new Date()), "Date");
16+
a(is(function(){}), "function");
17+
a(is(new Function()), "function");
18+
a(is(/a/), "RegExp");
19+
a(is(new RegExp("a")), "RegExp");
20+
a(is(""), "string");
21+
a(is(new String("")), "String");
22+
if(window.Symbol) a(is(Symbol()), "symbol");
23+
else i++;
24+
a(is(5), "number");
25+
a(is(new Number(5)), "Number");
26+
a(is(NaN), "NaN");
27+
a(is(new Number(NaN)), "NaN");
28+
a(is(1/0), "number");
29+
a(is(new Number(1/0)), "Number");
30+
31+
a(is.defined(), false);
32+
a(is.defined(5), true);
33+
a(is.undefined(), true);
34+
a(is.undefined(5), false);
35+
a(is.null(null), true);
36+
a(is.null(5), false);
37+
a(is.array([]), true);
38+
a(is.array(5), false);
39+
a(is.array(arguments), false);
40+
if(window.BigInt) a(is.bigint(BigInt(5)), true);
41+
else i++;
42+
a(is.bigint(5), false);
43+
a(is.boolean(false), true);
44+
a(is.boolean(), false);
45+
a(is.boolean(5), false);
46+
a(is.date(new Date()), true);
47+
a(is.date(5), false);
48+
a(is.date(1*(new Date())), false);
49+
a(is.function(function(){}), true);
50+
a(is.function(new Function()), true);
51+
a(is.function(5), false);
52+
a(is.regexp(/a/), true);
53+
a(is.regexp(new RegExp("a")), true);
54+
a(is.regexp(5), false);
55+
a(is.string(""), true);
56+
a(is.string(new String()), true);
57+
a(is.string(5), false);
58+
if(window.Symbol) a(is.symbol(Symbol()), true);
59+
else i++;
60+
a(is.symbol(5), false);
61+
a(is.nan(NaN), true);
62+
a(is.nan(new Number(NaN)), true);
63+
a(is.nan(5), false);
64+
a(is.nan(new Number(5)), false);
65+
a(is.number(5), true);
66+
a(is.number(new Number()), true);
67+
a(is.number(NaN), false);
68+
a(is.number(new Number(NaN)), false);
69+
a(is.number(1/0), true);
70+
a(is.number(new Number(1/0)), true);
71+
a(is.number(""), false);
72+
a(is.number(new Date()), false);
73+
a(is.number(1*(new Date())), true);
74+
a(is.number.real(5), true);
75+
a(is.number.real(1/0), false);
76+
a(is.number.real(NaN), false);
77+
a(is.number.real("5"), false);
78+
a(is.number.infinite(1/0), true);
79+
a(is.number.infinite(5), false);
80+
a(is.number.infinite(NaN), false);
81+
a(is.number.infinite("Infinity"), false);
82+
83+
a(is.object({}), true);
84+
a(is.object(5), false);
85+
a(is.object(new Number()), true);
86+
a(is.object(function(){}), true);
87+
a(is.primitive(), true);
88+
a(is.primitive(5), true);
89+
a(is.primitive(new Number()), false);
90+
a(is.primitive(NaN), true);
91+
a(is.primitive(new Number(NaN)), false);

0 commit comments

Comments
 (0)