Skip to content

Commit 7a70e15

Browse files
committed
more changes
1 parent a420744 commit 7a70e15

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

README.md

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

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

26-
In addition to storing a type name, a **Type** object's properties reveal whether the tested value was an object or a primitive.
26+
A String object storing a primitive type, with the following additional properties.
2727

2828
| Property | Type | Description
2929
| - | - | -

isType.mjs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,24 @@ const String = globalThis["String"];
44
const Number = globalThis["Number"];
55
const Boolean = globalThis["Boolean"];
66

7-
class Type {
7+
const typeofTypes = [
8+
/*** primitives ***/
9+
"undefined",
10+
"null",
11+
"bigint",
12+
"symbol",
13+
14+
/*** primitives & objects ***/
15+
"boolean",
16+
"number",
17+
"string",
18+
19+
/*** objects ***/
20+
"object",
21+
"function",
22+
];
23+
24+
class Type extends String {
825
/**
926
* @constructor
1027
* @param {string} typeName - The result of the `typeof` operator.
@@ -13,11 +30,13 @@ class Type {
1330
constructor(typeName, objectType){
1431
if(!(typeof typeName === "string" || typeName instanceof String))
1532
throw new TypeError("'typeName' must be a string");
16-
if(typeName == "")
17-
throw new RangeError("'typeName' cannot be an empty string");
33+
if(!typeofTypes.includes(typeName))
34+
throw new RangeError("'typeName' must be a primitive type, 'object', or 'function'");
1835
if(objectType && !(typeof objectType === "string" || objectType instanceof String))
1936
throw new TypeError("'objectType' must be a string");
2037

38+
super(typeName);
39+
2140
this.type = typeName;
2241
this.objectType = objectType || "";
2342

@@ -50,30 +69,13 @@ function is(value){
5069
return new Type(type);
5170
}
5271

53-
54-
is.object = (v)=>(v instanceof Object);
55-
is.primitive = (v)=>!is.object(v);
56-
57-
const typeofTypes = [
58-
/*** primitives ***/
59-
"undefined",
60-
"null",
61-
"bigint",
62-
"symbol",
63-
64-
/*** primitives & objects ***/
65-
"boolean",
66-
"number",
67-
"string",
68-
69-
/*** objects ***/
70-
//"object",
71-
"function",
72-
];
7372
for(const type of typeofTypes){
7473
is[type] = (v)=>(is(v).type === type);
7574
}
7675

76+
is.object = (v)=>(v instanceof Object);
77+
is.primitive = (v)=>!is.object(v);
78+
7779
const isNumberish = is.number;
7880
is.number = (v)=>(isNumberish(v) && Number.isFinite(v));
7981
is.infinite = (v)=>(isNumberish(v) && !Number.isFinite(v) && !Number.isNaN(v));

0 commit comments

Comments
 (0)