Skip to content

Commit 11c989f

Browse files
authored
Merge pull request #4 from wizard04wsu/fewer-keystrokes
Version 4 is not backwards compatible. It has been rewritten to be quicker/easier to type code, avoiding the need for string literals and comparison operators when possible.
2 parents 769f5a4 + de018f8 commit 11c989f

File tree

4 files changed

+393
-467
lines changed

4 files changed

+393
-467
lines changed

README.md

Lines changed: 58 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -4,131 +4,75 @@ A robust alternative to JavaScript's built-in type testing.
44

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

7+
***Version 4 is not backwards compatible.***
78

8-
---
99

10+
---
1011

11-
## Type Names
1212

13-
This module uses an expanded set of type names that make no distinction between primitive values and objects.
14-
For example, `5` and `new Number(5)` are both of type "number".
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 _number_, but `5` is _primitive_ and `new Number(5)` is _object_.
1517

16-
| Type Name | Values
17-
| - | -
18-
| array | `Array` objects
19-
| bigint | _bigint_ primitives
20-
| boolean | `false`, `true`, `Boolean` objects
21-
| date | `Date` objects
22-
| error | `Error` objects
23-
| function | `Function` objects
24-
| map | `Map` objects
25-
| nan | `NaN`, `Number` objects with value `NaN`
26-
| null | `null`
27-
| number | _number_ primitives, `Number` objects; excludes `NaN` values
28-
| object | instances of `Object` that don't match another type in this list
29-
| promise | `Promise` objects
30-
| regex | `RegExp` objects
31-
| set | `Set` objects
32-
| string | _string_ primitives, `String` objects
33-
| symbol | _symbol_ primitives
34-
| undefined | `undefined`
35-
| weakmap | `WeakMap` objects
36-
| weakset | `WeakSet` objects
37-
38-
39-
## Determine a Type
40-
41-
The **is()** function returns an object describing the type of its argument.
18+
The **is()** function returns an **IsType** object describing its argument.
4219

4320
Syntax:
4421
> **is**(_value_)
4522
46-
Returned object:
47-
| Property | Description
48-
| - | -
49-
| .**type** | The type name used by this module.
50-
| .**typeof** | The value returned by the `typeof` operator.
51-
| .**toStringTag** | The name used by `Object.prototype.toString()`. `undefined` for primitives.
52-
| .**constructorName** | The name of the argument's constructor. `undefined` for primitives.
53-
| .**isObject** | True if the value is an object.
54-
| .**isPrimitive** | True if the value is a primitive.
55-
56-
57-
## Type Testing
58-
59-
Each of the type-testing methods return a boolean indicating if the argument is of that type.
60-
61-
Syntax:
62-
> is._typeTester_(_value_)
63-
64-
### Basics
65-
66-
| Method | Tests for
67-
| - | -
68-
| is.**function**() | instance of `Function`
69-
| is.**object**() | instance of `Object`
70-
| is.**primitive**() | primitives
71-
| is.**null**() | `null`
72-
| is.**nullish**() | `undefined`, `null`
73-
| is.**undefined**() | `undefined`
74-
| is.**defined**() | not `undefined`
7523

76-
### Booleans
24+
## IsType Object
7725

78-
| Method | Tests for
26+
| Member | Description
7927
| - | -
80-
| is.**boolean**() | `false`, `true`, instance of `Boolean`
81-
| is.**falsy**() | `false`, `undefined`, `null`, `NaN`, `0`, `-0`, `0n`, `""`, [`document.all`](https://developer.mozilla.org/en-US/docs/Web/API/Document/all#conversion_to_boolean)
82-
| is.**truthy**() | not falsy
83-
84-
### Numbers
85-
86-
| Method | Tests for
87-
| - | -
88-
| is.**bigint**() | _bigint_ primitive
89-
| is.**date**() | instance of `Date`
90-
| is.**numberish**() | _number_ primitive, instance of `Number`
91-
92-
_Numberish_ values can be more explicitly tested using the following methods:
93-
94-
| Method | Tests for
95-
| - | -
96-
| is.**real**() | real numbers
97-
| is.**infinite**() | `Infinity`, `-Infinity`
98-
| is.**number**() | real numbers, `Infinity`, `-Infinity`
99-
| is.**nan**() | `NaN`
28+
| .type | The type of _value_ (using this module's type names).
29+
| .of(_class_) | Tests if _value_ was 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_](#type-names-and-related-descriptors)\] | Descriptors are listed in the table below. Each descriptor property is a boolean that is `true` if it applied to _value_.
33+
34+
Enumerable properties of the **is** function are string values of the name of each descriptor. These can be used
35+
in the `.all()` and `.any()` methods instead of string literals.
36+
For example, <code>is(<i>value</i>).all("number", "object")</code> is equivalent to <code>is(<i>value</i>).all(is.number, is.object)</code>.
37+
38+
39+
## Type Names and Related Descriptors
40+
41+
| Descriptor | Type Name | Primitive Values | Instances Of Classes
42+
| - | - | - | -
43+
| defined | | not undefined | `Object`
44+
| **undefined** | yes | undefined |
45+
| primitive | | not an instance of `Object` |
46+
| **object** | yes | | `Object`
47+
| objectish | | `null` | `Object`
48+
| **null** | yes | `null` |
49+
| nullish | | undefined, `null` |
50+
| **boolean** | yes | `false`, `true` |
51+
| false | | `false` |
52+
| true | | `true` |
53+
| falsy | | undefined, `null`, `false`, `0n`, `NaN`, `0`, `""` |
54+
| truthy | | not _falsy_ | `Object`
55+
| **symbol** | yes | a `Symbol` |
56+
| **bigint** | yes | `0n`, `5n` |
57+
| numberish | | `0`, `5`, `Infinity`, `NaN` | `Number`
58+
| **nan** | yes | `NaN` | `Number` with value `NaN`
59+
| **number** | yes | `0`, `5`, `Infinity` | `Number` excluding those with value `NaN`
60+
| real | | `0`, `5` | `Number` with a real number value
61+
| infinite | | `Infinity` | `Number` with an infinite value
62+
| **string** | yes | `""`, `"foo"` | `String`
63+
| **array** | yes | `[]`, `[1,2]` | `Array`
64+
| **map** | yes | | `Map`
65+
| **set** | yes | | `Set`
66+
| **weakmap** | yes | | `WeakMap`
67+
| **weakset** | yes | | `WeakSet`
68+
| empty | | `""`, `[]` | `String` or `Array` of length == 0, `Map` or `Set` of size == 0
69+
| nonempty | | not _empty_ | `String`, `Array`, `Map`, or `Set` that is not _empty_
70+
| **date** | yes | | `Date`
71+
| **error** | yes | | `Error`
72+
| **function** | yes | | `Function`
73+
| **promise** | yes | | `Promise`
74+
| **regex** | yes | | `Regex`
75+
76+
## Note
10077

10178
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`.
102-
103-
### Text
104-
105-
| Method | Tests for
106-
| - | -
107-
| is.**regex**() | instance of `RegExp`
108-
| is.**string**() | _string_ primitive, instance of `String`
109-
110-
### Collections
111-
112-
| Method | Tests for
113-
| - | -
114-
| is.**array**() | instance of `Array`
115-
| is.**map**() | instance of `Map`
116-
| is.**set**() | instance of `Set`
117-
| is.**weakmap**() | instance of `WeakMap`
118-
| is.**weakset**() | instance of `WeakSet`
119-
120-
### Other Common Types
121-
122-
| Method | Tests for
123-
| - | -
124-
| is.**error**() | instance of `Error`
125-
| is.**promise**() | instance of `Promise`
126-
| is.**symbol**() | _symbol_ primitive
127-
128-
129-
## Additional Methods
130-
131-
| Method | Description
132-
| - | -
133-
| is.**empty**(_value_) | Tests if an object's `.length` or `.size` property equals zero.
134-
| is.**of**(_value_, _class_) | Tests if _value_ is an instance of _class_. (Same as using the `instanceof` operator.)

0 commit comments

Comments
 (0)