Skip to content

Commit efa4e46

Browse files
committed
Merge branch 'dev' into main
2 parents 0d1fc03 + f78645c commit efa4e46

File tree

5 files changed

+524
-303
lines changed

5 files changed

+524
-303
lines changed

README.md

Lines changed: 109 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,134 @@
11
# Improved JavaScript Type Testing
2-
A robust alternative to JavaScript's `typeof` keyword.
32

4-
This is a JavaScript module that exports: [`is`](#is) (default)
3+
A robust alternative to JavaScript's built-in type testing.
4+
5+
See the [test page](https://wizard04wsu.github.io/javascript-type-testing/test/test.htm) for examples.
6+
57

68
---
79

810

9-
## Get the type
11+
## Type Names
12+
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".
15+
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`
26+
| null | `null`
27+
| number | _number_ primitives, `Number` objects; excludes `NaN`
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
1037

11-
### is()
1238

13-
The **is()** function determines the type of its argument and returns a [Type](#the-type-class) object.
39+
## Determine a Type
40+
41+
The **is()** function returns an object describing the type of its argument.
1442

1543
Syntax:
16-
> `is(value)`
44+
> **is**(_value_)
1745
18-
Parameters:
19-
- ***value*** - The value to determine the type of.
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.
2055

21-
Return value:
22-
- A [Type](#the-type-class) object.
2356

24-
### The Type class
57+
## Type Testing
2558

26-
The **Type** class extends the **String** class. In addition to storing a type name, its properties reveal whether the tested value was an object or a primitive.
59+
Each of the type-testing methods return a boolean indicating if the argument is of that type.
2760

2861
Syntax:
29-
> `new Type(typeName)`
30-
> `new Type(typeName, objectType)`
31-
32-
Parameters:
33-
- ***typeName*** - (string)
34-
- ***objectType*** - (string) The object name used by `Object.prototype.toString()`. Include this for object types. Do not include this for primitive types.
35-
36-
| Property | Type | Description |
37-
| --- | --- | --- |
38-
| .**type** | string | The type name (`typeName`). This is also the instance's primitive value. |
39-
| .**objectType** | string | The object name used by `Object.prototype.toString()`. |
40-
| .**primitive** | string | For primitive types, this property is set to `typeName`. Otherwise, it's undefined. |
41-
| .**object** | string | For object types, this property is set to `typeName`. Otherwise, it's undefined. |
42-
43-
### Example
44-
45-
```
46-
is(2).type; // "number"
47-
is(2)+"" // "number"
48-
is(2) == "number"; //true
49-
is(2).primitive === "number"; // true
50-
is(2).object === "number"; // false
51-
is(2).objectType; // undefined
52-
53-
let o = new Number(2);
54-
is(o).type; // "number"
55-
is(o)+"" // "number"
56-
is(o) == "number"; //true
57-
is(o).primitive === "number"; // false
58-
is(o).object === "number"; // true
59-
is(o).objectType; // "Number"
60-
```
62+
> is._typeTester_(_value_)
6163
62-
---
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`
75+
76+
### Booleans
77+
78+
| Method | Tests for
79+
| - | -
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`
100+
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`.
102+
103+
### Text
63104

105+
| Method | Tests for
106+
| - | -
107+
| is.**regex**() | instance of `RegExp`
108+
| is.**string**() | _string_ primitive, instance of `String`
64109

65-
## Test for a type
110+
### Collections
66111

67-
For each of the type-testing methods, the only parameter is the item to be tested. The return value is a boolean.
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`
68119

69-
**is.object()**
70-
**is.primitive()**
120+
### Other Common Types
71121

72-
**is.undefined()**
73-
**is.null()**
122+
| Method | Tests for
123+
| - | -
124+
| is.**error**() | instance of `Error`
125+
| is.**promise**() | instance of `Promise`
126+
| is.**symbol**() | _symbol_ primitive
74127

75-
**is.number()**
76-
**is.number.real()** - This is most likely what you actually want to use when testing for a number.
77-
**is.number.infinite()**
78-
**is.number.NaN()** - Note that JavaScript doesn't correctly treat all undefined forms as `NaN` (e.g., `1/0` and `0**0`).
79128

80-
**is.bigint()**
81-
**is.boolean()**
82-
**is.string()**
83-
**is.symbol()**
84-
**is.function()**
129+
## Additional Methods
85130

86-
**is.array()**
87-
**is.date()**
88-
**is.error()**
89-
**is.regexp()**
90-
**is.map()**
91-
**is.set()**
92-
**is.weakmap()**
93-
**is.weakset()**
94-
**is.promise()**
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.)

isType.mjs

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

0 commit comments

Comments
 (0)