Skip to content

Commit debed42

Browse files
committed
add tests for additional methods, update readme
1 parent d16864d commit debed42

File tree

3 files changed

+52
-72
lines changed

3 files changed

+52
-72
lines changed

README.md

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
A robust alternative to JavaScript's built-in type testing.
44

5+
See the test page for examples.
6+
57

68
---
79

810

911
## Type Names
1012

1113
This module uses an expanded set of type names that make no distinction between primitive values and objects.
12-
For example, `5` and `new String(5)` are both of type "number".
14+
For example, `5` and `new Number(5)` are both of type "number".
1315

1416
| Type Name | Values
1517
| - | -
@@ -33,6 +35,7 @@ For example, `5` and `new String(5)` are both of type "number".
3335
| weakmap | `WeakMap` objects
3436
| weakset | `WeakSet` objects
3537

38+
3639
## Determine a Type
3740

3841
The **is()** function returns an object describing the type of its argument.
@@ -45,76 +48,11 @@ Returned object:
4548
| - | -
4649
| .**type** | The type name used by this module.
4750
| .**typeof** | The value returned by the `typeof` operator.
48-
| .**toStringTag** | The name used by `Object.prototype.toString()`.
49-
| .**constructorName** | The name of the argument's constructor, or `undefined`.
51+
| .**toStringTag** | The name used by `Object.prototype.toString()`. `undefined` for primitives.
52+
| .**constructorName** | The name of the argument's constructor. `undefined` for primitives.
5053
| .**isObject** | True if the value is an object.
5154
| .**isPrimitive** | True if the value is a primitive.
5255

53-
<details>
54-
<summary>Examples</summary>
55-
56-
```
57-
let v;
58-
is(v).type // "undefined"
59-
is(v).typeof // "undefined"
60-
is(v).toStringTag // "Undefined"
61-
is(v).constructorName // undefined
62-
63-
v = null;
64-
is(v).type // "null"
65-
is(v).typeof // "object"
66-
is(v).toStringTag // "Null"
67-
is(v).constructorName // undefined
68-
69-
v = NaN;
70-
is(v).type // "nan"
71-
is(v).typeof // "number"
72-
is(v).toStringTag // "Number"
73-
is(v).constructorName // "Number"
74-
75-
v = 42;
76-
is(v).type // "number"
77-
is(v).typeof // "number"
78-
is(v).toStringTag // "Number"
79-
is(v).constructorName // "Number"
80-
81-
v = new Number(42);
82-
is(v).type // "number"
83-
is(v).typeof // "object"
84-
is(v).toStringTag // "Number"
85-
is(v).constructorName // "Number"
86-
87-
v = [];
88-
is(v).type // "array"
89-
is(v).typeof // "object"
90-
is(v).toStringTag // "Array"
91-
is(v).constructorName // "Array"
92-
93-
v = ()=>{};
94-
is(v).type // "function"
95-
is(v).typeof // "function"
96-
is(v).toStringTag // "Function"
97-
is(v).constructorName // "Function"
98-
99-
class Foo {}
100-
v = new Foo();
101-
is(v).type // "object"
102-
is(v).typeof // "object"
103-
is(v).toStringTag // "Object"
104-
is(v).constructorName // "Foo"
105-
106-
class Bar {
107-
get [Symbol.toStringTag](){ return "Foobar"; }
108-
}
109-
v = new Bar();
110-
is(v).type // "object"
111-
is(v).typeof // "object"
112-
is(v).toStringTag // "Foobar"
113-
is(v).constructorName // "Bar"
114-
```
115-
116-
</details>
117-
11856

11957
## Type Testing
12058

@@ -151,7 +89,7 @@ Syntax:
15189
| is.**date**() | instance of `Date`
15290
| is.**numberish**() | _number_ primitive, instance of `Number`
15391

154-
_Numberish_ values can be more explicitly tested using the following methods:
92+
_Numberish_ values can be more explicitly tested using the following methods:
15593

15694
| Method | Tests for
15795
| - | -
@@ -160,7 +98,7 @@ _Numberish_ values can be more explicitly tested using the following methods:
16098
| is.**number**() | real numbers, `Infinity`, `-Infinity`
16199
| is.**nan**() | `NaN`
162100

163-
Note that JavaScript doesn't always treat mathematical expressions of undefined or indeterminate form as you might expect. For example, `1/0` is an undefined form, but JavaScript evaluates it as `Infinity`.
101+
Note that JavaScript doesn't always treat mathematical expressions of undefined or indeterminate form as you might expect. For example, `1/0` is an undefined form, but JavaScript evaluates it as `Infinity`.
164102

165103
### Text
166104

@@ -187,6 +125,7 @@ Note that JavaScript doesn't always treat mathematical expressions of undefined
187125
| is.**promise**() | instance of `Promise`
188126
| is.**symbol**() | _symbol_ primitive
189127

128+
190129
## Additional Methods
191130

192131
| Method | Description

test/test.htm

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,29 @@
4444
</table>
4545
</details>
4646

47-
<details><summary>Type Tester Functions</summary>
47+
<details><summary>Type Tester Methods</summary>
4848
<table>
4949
<thead>
5050
<tr>
5151
<th>Value</th>
52-
<th>Successful Type Tester Functions</th>
52+
<th>Successful Type Tester Methods</th>
5353
</tr>
5454
</thead>
5555
<tbody id="tester_results"></tbody>
5656
</table>
5757
</details>
5858

59+
<details><summary>Additional Methods</summary>
60+
<table>
61+
<thead>
62+
<tr>
63+
<th>Test</th>
64+
<th>Result</th>
65+
</tr>
66+
</thead>
67+
<tbody id="additional_results"></tbody>
68+
</table>
69+
</details>
70+
5971
</body>
6072
</html>

test/test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,32 @@ function _tester(value, pseudocode, _testerNames){
162162
_tester(new WeakSet(), "new WeakSet()", ["weakset", "object", "defined", "truthy"]);
163163
_tester(document.all, "document.all", ["object", "nullish", "undefined", "falsy"]);
164164
}
165+
166+
167+
function _more(test, pseudocode, _result){
168+
169+
const tbody = document.getElementById("additional_results");
170+
const tr = document.createElement("tr");
171+
tr.innerHTML = `<td>${pseudocode}</td>`;
172+
createCell(test, _result);
173+
tbody.appendChild(tr);
174+
175+
function createCell(actual, expected){
176+
const td = document.createElement("td");
177+
td.dataset.expected = expected;
178+
td.innerHTML = actual;
179+
if(actual !== expected) td.classList.add("fail");
180+
tr.appendChild(td);
181+
}
182+
}
183+
184+
{
185+
_more(is.empty(""), 'is.empty("")', true);
186+
_more(is.empty("a"), 'is.empty("a")', false);
187+
_more(is.empty([]), "is.empty([])", true);
188+
_more(is.empty([5]), "is.empty([5])", false);
189+
_more(is.empty(new Map()), "is.empty(new Map())", true);
190+
_more(is.empty(new Map([[1, "one"]])), 'is.empty(new Map([[1, "one"]]))', false);
191+
_more(is.of(5, Number), "is.of(5, Number)", false);
192+
_more(is.of(new Number(5), Number), "is.of(new Number(5), Number)", true);
193+
}

0 commit comments

Comments
 (0)