@@ -4,7 +4,24 @@ const String = globalThis["String"];
44const Number = globalThis [ "Number" ] ;
55const 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- ] ;
7372for ( 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+
7779const isNumberish = is . number ;
7880is . number = ( v ) => ( isNumberish ( v ) && Number . isFinite ( v ) ) ;
7981is . infinite = ( v ) => ( isNumberish ( v ) && ! Number . isFinite ( v ) && ! Number . isNaN ( v ) ) ;
0 commit comments