You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-70Lines changed: 9 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,16 @@
2
2
3
3
A robust alternative to JavaScript's built-in type testing.
4
4
5
+
See the test page for examples.
6
+
5
7
6
8
---
7
9
8
10
9
11
## Type Names
10
12
11
13
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".
13
15
14
16
| Type Name | Values
15
17
| - | -
@@ -33,6 +35,7 @@ For example, `5` and `new String(5)` are both of type "number".
33
35
| weakmap | `WeakMap` objects
34
36
| weakset | `WeakSet` objects
35
37
38
+
36
39
## Determine a Type
37
40
38
41
The **is()** function returns an object describing the type of its argument.
@@ -45,76 +48,11 @@ Returned object:
45
48
| - | -
46
49
| .**type** | The type name used by this module.
47
50
| .**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.
50
53
| .**isObject** | True if the value is an object.
51
54
| .**isPrimitive** | True if the value is a primitive.
52
55
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
-
118
56
119
57
## Type Testing
120
58
@@ -151,7 +89,7 @@ Syntax:
151
89
| is.**date**() | instance of `Date`
152
90
| is.**numberish**() | _number_ primitive, instance of `Number`
153
91
154
-
_Numberish_ values can be more explicitly tested using the following methods:
92
+
_Numberish_ values can be more explicitly tested using the following methods:
155
93
156
94
| Method | Tests for
157
95
| - | -
@@ -160,7 +98,7 @@ _Numberish_ values can be more explicitly tested using the following methods:
160
98
| is.**number**() | real numbers, `Infinity`, `-Infinity`
161
99
| is.**nan**() | `NaN`
162
100
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`.
164
102
165
103
### Text
166
104
@@ -187,6 +125,7 @@ Note that JavaScript doesn't always treat mathematical expressions of undefined
0 commit comments