Skip to content

Commit 5efe6ec

Browse files
committed
WIP
1 parent 7417964 commit 5efe6ec

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

isType.mjs.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const TYPES = {
44
array: { class: Array },
55
bigint: { primitive: "bigint" },
6-
boolean: { primitive: "boolean", class: Boolean },
6+
boolean: { primitive: "boolean", },
77
date: { class: Date },
88
error: { class: Error },
99
function: { class: Function },
@@ -26,9 +26,10 @@ const TYPES = {
2626
/**
2727
* A collection of boolean properties indicating the type of the given value.
2828
* @class TypeTest
29-
* @extends {String}
3029
*/
31-
class TypeTest extends String {
30+
class TypeTest {
31+
32+
#typeName;
3233

3334
/**
3435
* @constructor
@@ -62,7 +63,7 @@ class TypeTest extends String {
6263
}
6364
}
6465

65-
super(typeName);
66+
this.#typeName = typeName;
6667

6768
for(const name in TYPES){
6869
this[name] = typeName === name;
@@ -74,6 +75,7 @@ class TypeTest extends String {
7475
this.infinite = !this.real;
7576
}
7677

78+
this.object = value instanceof Object;
7779
this.primitive = !this.object;
7880
this.objectish = this.object || this.null;
7981

@@ -85,13 +87,24 @@ class TypeTest extends String {
8587

8688
if(this.string || this.array){
8789
this.empty = value.length === 0;
90+
this.nonempty = !this.empty;
8891
}
89-
else if(this.map || this.set || this.weakmap || this.weakset){
92+
else if(this.map || this.set){
9093
this.empty = value.size === 0;
94+
this.nonempty = !this.empty;
9195
}
9296
}
97+
98+
toString(){
99+
return this.#typeName;
100+
}
93101
}
94102

103+
/**
104+
* Determine the type of a value. The returned object includes boolean properties to quickly test against specific types or for specific states (e.g., 'empty').
105+
* @param {*} value - The value to be tested.
106+
* @returns {TypeTest}
107+
*/
95108
function is(value){
96109
return new TypeTest(value);
97110
}

test/test.htm

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@
2626
</head>
2727
<body>
2828

29-
<details><summary>Types</summary>
29+
<details open><summary>Types</summary>
3030
<table>
3131
<thead>
3232
<tr>
33-
<th rowspan="2">Value</th>
34-
<th colspan="4">Type</th>
35-
</tr>
36-
<tr>
37-
<th>.type</th>
38-
<th>.typeof</th>
39-
<th>.toStringTag</th>
40-
<th>.constructorName</th>
33+
<th>Value</th>
34+
<th>Type</th>
35+
<th>Object</th>
36+
<th>Properties</th>
4137
</tr>
4238
</thead>
4339
<tbody id="type_results"></tbody>

test/test.js

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,84 @@
11
import is from "../isType.mjs.js";
22

33

4-
function _is(value, pseudocode, _type, _typeof, _toStringTag, _constructorName){
4+
function _is(value, pseudocodeHTML, expectedType, expectedIsObject){
55

6-
const type = is(value);
6+
const actualType = is(value);
7+
//console.log(actualType);
78

89
const tbody = document.getElementById("type_results");
910
const tr = document.createElement("tr");
10-
tr.innerHTML = `<td>${pseudocode}</td>`;
11-
createCell(type.type, _type);
12-
createCell(type.typeof, _typeof);
13-
createCell(type.toStringTag, _toStringTag);
14-
createCell(type.constructorName, _constructorName);
11+
tr.innerHTML = `<td>${pseudocodeHTML}</td>`;
12+
createCell(actualType, expectedType);
13+
tbody.appendChild(tr);
14+
createCell(actualType.object, expectedIsObject);
1515
tbody.appendChild(tr);
1616

17+
let props = "";
18+
for(const name in actualType){
19+
if(actualType[name]) props += `, ${name}`;
20+
}
21+
tr.innerHTML += `<td>${props.slice(2)}</td>`;
22+
1723
function createCell(actual, expected){
1824
const td = document.createElement("td");
19-
td.dataset.expected = expected === void 0 ? "undefined" : `"${expected}"`;
20-
td.innerHTML = actual === void 0 ? `<i>undefined</i>` : `"${actual}"`;
21-
if(actual !== expected) td.classList.add("fail");
25+
td.dataset.expected = expected === void 0 ? "undefined" : expected;
26+
td.innerHTML = actual === void 0 ? `<i>undefined</i>` : actual.toString() === "" ? `<i>empty string</i>` : actual;
27+
if(actual != expected) td.classList.add("fail");
2228
tr.appendChild(td);
2329
}
2430
}
2531

2632
{
27-
_is([], "[]", "array", "object", "Array", "Array");
28-
_is(new Array(), "new Array()", "array", "object", "Array", "Array");
29-
_is(5n, "5n", "bigint", "bigint");
30-
_is(true, "true", "boolean", "boolean");
31-
_is(false, "false", "boolean", "boolean");
32-
_is(new Boolean(), "new Boolean()", "boolean", "object", "Boolean", "Boolean");
33-
_is(new Date(), "new Date()", "date", "object", "Date", "Date");
34-
_is(new Error(), "new Error()", "error", "object", "Error", "Error");
35-
_is(new TypeError(), "new TypeError()", "error", "object", "Error", "TypeError");
36-
_is(()=>{}, "()=>{}", "function", "function", "Function", "Function");
37-
_is(Object, "Object", "function", "function", "Function", "Function");
38-
_is(new Map(), "new Map()", "map", "object", "Map", "Map");
39-
_is(NaN, "NaN", "nan", "number");
40-
_is(new Number(NaN), "new Number(NaN)", "nan", "object", "Number", "Number");
41-
_is(new Number('a'), "new Number('a')", "nan", "object", "Number", "Number");
42-
_is(null, "null", "null", "object");
43-
_is(5, "5", "number", "number");
44-
_is(Infinity, "Infinity", "number", "number");
45-
_is(new Number(), "new Number()", "number", "object", "Number", "Number");
46-
_is({}, "{}", "object", "object", "Object", "Object");
47-
_is(new Object(), "new Object()", "object", "object", "Object", "Object");
48-
_is(new Promise(()=>{}), "new Promise(()=>{})", "promise", "object", "Promise", "Promise");
49-
_is(/a/, "/a/", "regex", "object", "RegExp", "RegExp");
50-
_is(new RegExp(), "new RegExp()", "regex", "object", "RegExp", "RegExp");
51-
_is(new Set(), "new Set()", "set", "object", "Set", "Set");
52-
_is("", '""', "string", "string");
53-
_is("a", '"a"', "string", "string");
54-
_is(new String(), "new String()", "string", "object", "String", "String");
55-
_is(Symbol(), "Symbol()", "symbol", "symbol");
56-
_is(void 0, "void 0", "undefined", "undefined");
57-
_is(new WeakMap(), "new WeakMap()", "weakmap", "object", "WeakMap", "WeakMap");
58-
_is(new WeakSet(), "new WeakSet()", "weakset", "object", "WeakSet", "WeakSet");
33+
_is([], "[]", "array", true);
34+
_is(new Array(), "new Array()", "array", true);
35+
_is([1,2], "[1,2]", "array", true);
36+
_is(new Array(1,2), "new Array(1,2)", "array", true);
37+
_is(5n, "5n", "bigint", false);
38+
_is(true, "true", "boolean", false);
39+
_is(false, "false", "boolean", false);
40+
_is(new Boolean(), "new Boolean()", "object", true);
41+
_is(new Date(), "new Date()", "date", true);
42+
_is(new Error(), "new Error()", "error", true);
43+
_is(new TypeError(), "new TypeError()", "error", true);
44+
_is(()=>{}, "()=>{}", "function", true);
45+
_is(Object, "Object", "function", true);
46+
_is(new Map(), "new Map()", "map", true);
47+
_is(NaN, "NaN", "nan", false);
48+
_is(new Number(NaN), "new Number(NaN)", "nan", true);
49+
_is(new Number('a'), "new Number('a')", "nan", true);
50+
_is(null, "null", "null", false);
51+
_is(5, "5", "number", false);
52+
_is(Infinity, "Infinity", "number", false);
53+
_is(new Number(), "new Number()", "number", true);
54+
_is({}, "{}", "object", true);
55+
_is(new Object(), "new Object()", "object", true);
56+
_is(new Promise(()=>{}), "new Promise(()=>{})", "promise", true);
57+
_is(/a/, "/a/", "regex", true);
58+
_is(new RegExp(), "new RegExp()", "regex", true);
59+
_is(new Set(), "new Set()", "set", true);
60+
_is("", '""', "string", false);
61+
_is("a", '"a"', "string", false);
62+
_is(new String(), "new String()", "string", true);
63+
_is(Symbol(), "Symbol()", "symbol", false);
64+
_is(void 0, "void 0", "undefined", false);
65+
_is(new WeakMap(), "new WeakMap()", "weakmap", true);
66+
_is(new WeakSet(), "new WeakSet()", "weakset", true);
5967
{
6068
class Foo {}
61-
_is(new Foo(), "<i>class Foo {}</i><br>new Foo()", "object", "object", "Object", "Foo");
69+
_is(new Foo(), "<i>class Foo {}</i><br>new Foo()", "object", true);
6270
}
6371
{
6472
class Foo extends String {}
65-
_is(new Foo(), "<i>class Foo extends String {}</i><br>new Foo()", "string", "object", "String", "Foo");
73+
_is(new Foo(), "<i>class Foo extends String {}</i><br>new Foo()", "string", true);
6674
}
6775
{
6876
class Foo { get [Symbol.toStringTag](){ return "Bar"; } }
69-
_is(new Foo(), "<i>class Foo { get [Symbol.toStringTag](){ return \"Bar\"; } }</i><br>new Foo()", "object", "object", "Bar", "Foo");
77+
_is(new Foo(), "<i>class Foo { get [Symbol.toStringTag](){ return \"Bar\"; } }</i><br>new Foo()", "object", true);
7078
}
7179
}
7280

73-
81+
/*
7482
function _tester(value, pseudocode, _testerNames){
7583
7684
const testers = [
@@ -191,3 +199,4 @@ function _more(test, pseudocode, _result){
191199
_more(is.of(5, Number), "is.of(5, Number)", false);
192200
_more(is.of(new Number(5), Number), "is.of(new Number(5), Number)", true);
193201
}
202+
*/

0 commit comments

Comments
 (0)