-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
524 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,134 @@ | ||
# Improved JavaScript Type Testing | ||
A robust alternative to JavaScript's `typeof` keyword. | ||
|
||
This is a JavaScript module that exports: [`is`](#is) (default) | ||
A robust alternative to JavaScript's built-in type testing. | ||
|
||
See the [test page](https://wizard04wsu.github.io/javascript-type-testing/test/test.htm) for examples. | ||
|
||
|
||
--- | ||
|
||
|
||
## Get the type | ||
## Type Names | ||
|
||
This module uses an expanded set of type names that make no distinction between primitive values and objects. | ||
For example, `5` and `new Number(5)` are both of type "number". | ||
|
||
| Type Name | Values | ||
| - | - | ||
| array | `Array` objects | ||
| bigint | _bigint_ primitives | ||
| boolean | `false`, `true`, `Boolean` objects | ||
| date | `Date` objects | ||
| error | `Error` objects | ||
| function | `Function` objects | ||
| map | `Map` objects | ||
| nan | `NaN` | ||
| null | `null` | ||
| number | _number_ primitives, `Number` objects; excludes `NaN` | ||
| object | instances of `Object` that don't match another type in this list | ||
| promise | `Promise` objects | ||
| regex | `RegExp` objects | ||
| set | `Set` objects | ||
| string | _string_ primitives, `String` objects | ||
| symbol | _symbol_ primitives | ||
| undefined | `undefined` | ||
| weakmap | `WeakMap` objects | ||
| weakset | `WeakSet` objects | ||
|
||
### is() | ||
|
||
The **is()** function determines the type of its argument and returns a [Type](#the-type-class) object. | ||
## Determine a Type | ||
|
||
The **is()** function returns an object describing the type of its argument. | ||
|
||
Syntax: | ||
> `is(value)` | ||
> **is**(_value_) | ||
Parameters: | ||
- ***value*** - The value to determine the type of. | ||
Returned object: | ||
| Property | Description | ||
| - | - | ||
| .**type** | The type name used by this module. | ||
| .**typeof** | The value returned by the `typeof` operator. | ||
| .**toStringTag** | The name used by `Object.prototype.toString()`. `undefined` for primitives. | ||
| .**constructorName** | The name of the argument's constructor. `undefined` for primitives. | ||
| .**isObject** | True if the value is an object. | ||
| .**isPrimitive** | True if the value is a primitive. | ||
|
||
Return value: | ||
- A [Type](#the-type-class) object. | ||
|
||
### The Type class | ||
## Type Testing | ||
|
||
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. | ||
Each of the type-testing methods return a boolean indicating if the argument is of that type. | ||
|
||
Syntax: | ||
> `new Type(typeName)` | ||
> `new Type(typeName, objectType)` | ||
Parameters: | ||
- ***typeName*** - (string) | ||
- ***objectType*** - (string) The object name used by `Object.prototype.toString()`. Include this for object types. Do not include this for primitive types. | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| .**type** | string | The type name (`typeName`). This is also the instance's primitive value. | | ||
| .**objectType** | string | The object name used by `Object.prototype.toString()`. | | ||
| .**primitive** | string | For primitive types, this property is set to `typeName`. Otherwise, it's undefined. | | ||
| .**object** | string | For object types, this property is set to `typeName`. Otherwise, it's undefined. | | ||
|
||
### Example | ||
|
||
``` | ||
is(2).type; // "number" | ||
is(2)+"" // "number" | ||
is(2) == "number"; //true | ||
is(2).primitive === "number"; // true | ||
is(2).object === "number"; // false | ||
is(2).objectType; // undefined | ||
let o = new Number(2); | ||
is(o).type; // "number" | ||
is(o)+"" // "number" | ||
is(o) == "number"; //true | ||
is(o).primitive === "number"; // false | ||
is(o).object === "number"; // true | ||
is(o).objectType; // "Number" | ||
``` | ||
> is._typeTester_(_value_) | ||
--- | ||
### Basics | ||
|
||
| Method | Tests for | ||
| - | - | ||
| is.**function**() | instance of `Function` | ||
| is.**object**() | instance of `Object` | ||
| is.**primitive**() | primitives | ||
| is.**null**() | `null` | ||
| is.**nullish**() | `undefined`, `null` | ||
| is.**undefined**() | `undefined` | ||
| is.**defined**() | not `undefined` | ||
|
||
### Booleans | ||
|
||
| Method | Tests for | ||
| - | - | ||
| is.**boolean**() | `false`, `true`, instance of `Boolean` | ||
| 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) | ||
| is.**truthy**() | not falsy | ||
|
||
### Numbers | ||
|
||
| Method | Tests for | ||
| - | - | ||
| is.**bigint**() | _bigint_ primitive | ||
| is.**date**() | instance of `Date` | ||
| is.**numberish**() | _number_ primitive, instance of `Number` | ||
|
||
_Numberish_ values can be more explicitly tested using the following methods: | ||
|
||
| Method | Tests for | ||
| - | - | ||
| is.**real**() | real numbers | ||
| is.**infinite**() | `Infinity`, `-Infinity` | ||
| is.**number**() | real numbers, `Infinity`, `-Infinity` | ||
| is.**nan**() | `NaN` | ||
|
||
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`. | ||
|
||
### Text | ||
|
||
| Method | Tests for | ||
| - | - | ||
| is.**regex**() | instance of `RegExp` | ||
| is.**string**() | _string_ primitive, instance of `String` | ||
|
||
## Test for a type | ||
### Collections | ||
|
||
For each of the type-testing methods, the only parameter is the item to be tested. The return value is a boolean. | ||
| Method | Tests for | ||
| - | - | ||
| is.**array**() | instance of `Array` | ||
| is.**map**() | instance of `Map` | ||
| is.**set**() | instance of `Set` | ||
| is.**weakmap**() | instance of `WeakMap` | ||
| is.**weakset**() | instance of `WeakSet` | ||
|
||
**is.object()** | ||
**is.primitive()** | ||
### Other Common Types | ||
|
||
**is.undefined()** | ||
**is.null()** | ||
| Method | Tests for | ||
| - | - | ||
| is.**error**() | instance of `Error` | ||
| is.**promise**() | instance of `Promise` | ||
| is.**symbol**() | _symbol_ primitive | ||
|
||
**is.number()** | ||
**is.number.real()** - This is most likely what you actually want to use when testing for a number. | ||
**is.number.infinite()** | ||
**is.number.NaN()** - Note that JavaScript doesn't correctly treat all undefined forms as `NaN` (e.g., `1/0` and `0**0`). | ||
|
||
**is.bigint()** | ||
**is.boolean()** | ||
**is.string()** | ||
**is.symbol()** | ||
**is.function()** | ||
## Additional Methods | ||
|
||
**is.array()** | ||
**is.date()** | ||
**is.error()** | ||
**is.regexp()** | ||
**is.map()** | ||
**is.set()** | ||
**is.weakmap()** | ||
**is.weakset()** | ||
**is.promise()** | ||
| Method | Description | ||
| - | - | ||
| is.**empty**(_value_) | Tests if an object's `.length` or `.size` property equals zero. | ||
| is.**of**(_value_, _class_) | Tests if _value_ is an instance of _class_. (Same as using the `instanceof` operator.) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.