Skip to content

Commit 2a3fd1b

Browse files
committed
Change is.number()
- This will be incompatible with previous usage so I'll have to increment the major version number. - still needs testing - is.number() is now only true for real numbers - removed is.___ and added is.infinite() and is.nan() - is().type is still "number" for +/-Infinity and NaN - changed Type .object and .primitive to boolean
1 parent a82de6c commit 2a3fd1b

File tree

3 files changed

+47
-51
lines changed

3 files changed

+47
-51
lines changed

README.md

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,33 @@ Parameters:
2121
Return value:
2222
- A [Type](#the-type-class) object.
2323

24-
### The Type class
24+
### Type objects
2525

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.
26+
In addition to storing a type name, a **Type** object's properties reveal whether the tested value was an object or a primitive.
2727

28-
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. |
28+
| Property | Type | Description
29+
| - | - | -
30+
| .**type** | string | The type name returned by the `typeof` operator.
31+
| .**objectType** | string | The object name used by `Object.prototype.toString()`. Undefined for primitive types.
32+
| .**primitive** | string | True for primitive types.
33+
| .**object** | string | True for object types.
4234

4335
### Example
4436

4537
```
4638
is(2).type; // "number"
47-
is(2)+"" // "number"
39+
is(2).toString() // "number"
4840
is(2) == "number"; //true
49-
is(2).primitive === "number"; // true
50-
is(2).object === "number"; // false
41+
is(2).primitive; // true
42+
is(2).object; // undefined
5143
is(2).objectType; // undefined
5244
5345
let o = new Number(2);
5446
is(o).type; // "number"
55-
is(o)+"" // "number"
47+
is(o).toString() // "number"
5648
is(o) == "number"; //true
57-
is(o).primitive === "number"; // false
58-
is(o).object === "number"; // true
49+
is(o).primitive; // undefined
50+
is(o).object; // true
5951
is(o).objectType; // "Number"
6052
```
6153

@@ -72,10 +64,11 @@ For each of the type-testing methods, the only parameter is the item to be teste
7264
**is.undefined()**
7365
**is.null()**
7466

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`).
67+
**is.number()** - A real number.
68+
**is.infinite()** \*
69+
**is.nan()** \*
70+
71+
\* Note that JavaScript doesn't correctly treat all undefined forms as `NaN`. For example, `1/0` and `0**0` are undefined forms, but JavaScript treats them as `Infinity`.
7972

8073
**is.bigint()**
8174
**is.boolean()**

isType.mjs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
const Object = globalThis["Object"];
3+
const String = globalThis["String"];
4+
const Number = globalThis["Number"];
5+
const Boolean = globalThis["Boolean"];
6+
27
class Type extends String {
38
/**
49
* @constructor
@@ -8,21 +13,24 @@ class Type extends String {
813
constructor(typeName, objectType){
914
if(!(typeof typeName === "string" || typeName instanceof String))
1015
throw new TypeError("'typeName' must be a string");
16+
if(typeName == "")
17+
throw new RangeError("'typeName' cannot be an empty string");
1118
typeName = String(typeName);
1219
if(objectType){
1320
if(!(typeof objectType === "string" || objectType instanceof String))
1421
throw new TypeError("'objectType' must be a string");
1522
objectType = String(objectType);
1623
}
1724

18-
super(typeName);
25+
super();
1926

27+
this.type = typeName;
2028
if(objectType){
21-
this.type = this.object = typeName;
2229
this.objectType = objectType;
30+
this.object = true;
2331
}
2432
else{
25-
this.type = this.primitive = typeName;
33+
this.primitive = true;
2634
}
2735
}
2836
}
@@ -74,9 +82,10 @@ for(const type of typeofTypes){
7482
is[type] = (v)=>(is(v).type === type);
7583
}
7684

77-
is.number.real = (v)=>(is.number(v) && Number.isFinite(1*v));
78-
is.number.infinite = (v)=>(is.number(v) && !Number.isFinite(1*v) && !Number.isNaN(1*v));
79-
is.number.NaN = (v)=>(is.number(v) && Number.isNaN(1*v)); //Note that JavaScript doesn't correctly treat all undefined forms as NaN (e.g., 1/0 and 0**0).
85+
const isNumberish = is.number;
86+
is.number = (v)=>(isNumberish(v) && Number.isFinite(v));
87+
is.infinite = (v)=>(isNumberish(v) && !Number.isFinite(v) && !Number.isNaN(v));
88+
is.nan = (v)=>(isNumberish(v) && Number.isNaN(v)); //Note that JavaScript doesn't correctly treat all undefined forms as NaN (e.g., 1/0 and 0**0 are undefined forms, but JavaScript treats them as Infinity).
8089

8190
const otherCommonTypes = [
8291
"Error",

test/test.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,19 @@ console.group("is.___()");
7575

7676
testIsType(is.number(5), true);
7777
testIsType(is.number(new Number()), true);
78-
testIsType(is.number.real(5), true);
79-
testIsType(is.number.real(new Number()), true);
80-
testIsType(is.number.real(1/0), false);
81-
testIsType(is.number.real(NaN), false);
82-
testIsType(is.number.real("5"), false);
83-
testIsType(is.number(1/0), true);
84-
testIsType(is.number(new Number(1/0)), true);
85-
testIsType(is.number.infinite(1/0), true);
86-
testIsType(is.number.infinite(new Number(1/0)), true);
87-
testIsType(is.number.infinite(5), false);
88-
testIsType(is.number.infinite(NaN), false);
89-
testIsType(is.number.infinite("Infinity"), false);
90-
testIsType(is.number(NaN), true);
91-
testIsType(is.number(new Number(NaN)), true);
92-
testIsType(is.number.NaN(NaN), true);
93-
testIsType(is.number.NaN(new Number(NaN)), true);
94-
testIsType(is.number.NaN(5), false);
95-
testIsType(is.number.NaN(1/0), false);
96-
testIsType(is.number.NaN("NaN"), false);
78+
testIsType(is.number(1/0), false);
79+
testIsType(is.number(NaN), false);
80+
testIsType(is.number("5"), false);
81+
testIsType(is.infinite(1/0), true);
82+
testIsType(is.infinite(new Number(1/0)), true);
83+
testIsType(is.infinite(5), false);
84+
testIsType(is.infinite(NaN), false);
85+
testIsType(is.infinite("Infinity"), false);
86+
testIsType(is.nan(NaN), true);
87+
testIsType(is.nan(new Number(NaN)), true);
88+
testIsType(is.nan(5), false);
89+
testIsType(is.nan(1/0), false);
90+
testIsType(is.nan("NaN"), false);
9791
testIsType(is.number(""), false);
9892
testIsType(is.number(new Date()), false);
9993
testIsType(is.number(1*(new Date())), true);

0 commit comments

Comments
 (0)