Skip to content

Commit 90a4381

Browse files
committed
Merge branch 'dev'
2 parents 421db1a + a5145b1 commit 90a4381

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

README.md

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
# Improved JavaScript Type Testing
22

3-
A robust alternative to JavaScript's built-in type testing.
3+
A robust alternative to type testing in vanilla JavaScript. Uses an expanded set of type names to simplify common tests.
44

55
See the [test page](https://wizard04wsu.github.io/javascript-type-testing/test/test.htm) for examples.
66

77
***Version 4 is not backwards compatible.***
88

99

10-
---
10+
## Syntax
1111

12-
13-
This module uses an expanded set of type names and related descriptors to simplify common tests of values.
14-
Basic types do not distinguish between primitives and objects, but descriptors _primitive_ or _object_
15-
can be used to determine that aspect.
16-
For example, `5` and `new Number(5)` are both type _number_, but `5` has descriptor _primitive_ and `new Number(5)` has descriptor _object_.
17-
18-
The **is()** function returns an object that describes its argument.
19-
20-
Syntax:
2112
> **is**(_value_)
2213
23-
24-
## Return Value
14+
The return value is an object that describes the argument.
2515

2616
| Member | Description
2717
| - | -
28-
| .type | The type of _value_ (using this module's type names).
18+
| .type | The type name of _value_.
2919
| .of(_class_) | Tests if _value_ is an instance of _class_.
30-
| .all(_...descriptors)_ | Takes a list of descriptor names as arguments. Returns `true` if **all** of them applied to _value_.
31-
| .any(_...descriptors_) | Takes a list of descriptor names as arguments. Returns `true` if **any** of them applied to _value_.
32-
| \[_descriptor_\] | Each [descriptor](#descriptors-and-type-names) property is a boolean that is `true` if it applied to _value_.
20+
| .all(_...descriptors_) | Takes descriptor names as arguments. Returns `true` if **all** of them apply to _value_.
21+
| .any(_...descriptors_) | Takes descriptor names as arguments. Returns `true` if **any** of them apply to _value_.
22+
| \[_descriptor_\] | Each [descriptor](#types-and-descriptors) property is a boolean that is `true` if it applies to _value_.
3323

3424
Enumerable properties of `is` are string constants of all the descriptor names. These can be used
3525
in the `.all()` and `.any()` methods instead of string literals.
@@ -40,14 +30,18 @@ For example, these are equivalent:
4030
> <code>is(<i>value</i>).all(is.number, is.object)</code>
4131
4232

43-
## Descriptors and Type Names
33+
## Types and Descriptors
34+
35+
Types in this module do not distinguish between primitives and objects. For example, `5` and `new Number(5)` are both of type "number".
36+
37+
A descriptor is a boolean value that is `true` if it applies to the value being tested. Type names are included as descriptors. For example, `5` is associated with the _primitive_, _number_, and _finite_ descriptors, among others.
4438

4539
| Descriptor | Type Name | Primitive Values | Instances Of Classes
4640
| - | - | - | -
4741
| defined | | not undefined | `Object`
4842
| **undefined** | yes | undefined |
49-
| primitive | | not an instance of `Object` |
50-
| **object** | yes | | `Object`
43+
| primitive | | any primitive value |
44+
| **object** | yes[^1] | | `Object`
5145
| objectish | | `null` | `Object`
5246
| **null** | yes | `null` |
5347
| nullish | | undefined, `null` |
@@ -61,7 +55,8 @@ For example, these are equivalent:
6155
| numberish | | `0`, `5`, `Infinity`, `NaN` | `Number`
6256
| **nan** | yes | `NaN` | `Number` with value `NaN`
6357
| **number** | yes | `0`, `5`, `Infinity` | `Number` excluding those with value `NaN`
64-
| real | | `0`, `5` | `Number` with a real number value
58+
| real[^2] | | `0`, `5` | `Number` with a finite number value
59+
| <a name="finite"></a>finite | | `0`, `5` | `Number` with a finite number value
6560
| infinite | | `Infinity` | `Number` with an infinite value
6661
| **string** | yes | `""`, `"foo"` | `String`
6762
| **array** | yes | `[]`, `[1,2]` | `Array`
@@ -77,6 +72,6 @@ For example, these are equivalent:
7772
| **promise** | yes | | `Promise`
7873
| **regex** | yes | | `Regex`
7974

80-
## Math Note
8175

82-
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`.
76+
[^1]: The "object" type is only used for a value if no other type is appropriate.
77+
[^2]: Deprecated. Use [_finite_](#finite) instead.

isType.mjs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ class TypeTest {
7575

7676
this.numberish = this.number || this.nan;
7777
if(this.number){
78-
this.real = Number.isFinite(this.object ? value.valueOf() : value);
79-
this.infinite = !this.real;
78+
this.real = this.finite = Number.isFinite(this.object ? value.valueOf() : value);
79+
this.infinite = !this.finite;
8080
}
8181
else{
82-
this.real = this.infinite = false;
82+
this.real = this.finite = this.infinite = false;
8383
}
8484

8585
this.defined = !this.undefined;

test/test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const COLUMNS = [
2020
"numberish",
2121
"nan",
2222
"number",
23-
"real",
23+
"finite",
2424
"infinite",
2525
"string",
2626
"array",
@@ -53,10 +53,10 @@ let testResults = [];
5353
_is(()=>(+"a"), is.nan, is.defined, is.primitive, is.falsy, is.numberish);
5454
_is(()=>(new Number(NaN)), is.nan, is.defined, is.object, is.objectish, is.truthy, is.numberish);
5555
_is(()=>(new Number("a")), is.nan, is.defined, is.object, is.objectish, is.truthy, is.numberish);
56-
_is(()=>(0), is.number, is.defined, is.primitive, is.falsy, is.real, is.numberish);
57-
_is(()=>(5), is.number, is.defined, is.primitive, is.truthy, is.real, is.numberish);
58-
_is(()=>(new Number(0)), is.number, is.defined, is.object, is.objectish, is.truthy, is.real, is.numberish);
59-
_is(()=>(new Number(5)), is.number, is.defined, is.object, is.objectish, is.truthy, is.real, is.numberish);
56+
_is(()=>(0), is.number, is.defined, is.primitive, is.falsy, is.finite, is.numberish);
57+
_is(()=>(5), is.number, is.defined, is.primitive, is.truthy, is.finite, is.numberish);
58+
_is(()=>(new Number(0)), is.number, is.defined, is.object, is.objectish, is.truthy, is.finite, is.numberish);
59+
_is(()=>(new Number(5)), is.number, is.defined, is.object, is.objectish, is.truthy, is.finite, is.numberish);
6060
_is(()=>(Infinity), is.number, is.defined, is.primitive, is.truthy, is.infinite, is.numberish);
6161
_is(()=>(-1/0), is.number, is.defined, is.primitive, is.truthy, is.infinite, is.numberish);
6262
_is(()=>(new Number(Infinity)), is.number, is.defined, is.object, is.objectish, is.truthy, is.infinite, is.numberish);

0 commit comments

Comments
 (0)