-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisType.mjs.js
154 lines (127 loc) · 3.62 KB
/
isType.mjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/** @module isType */
const TYPES = {
array: { class: Array },
bigint: { primitive: "bigint" },
boolean: { primitive: "boolean", },
date: { class: Date },
error: { class: Error },
function: { class: Function },
map: { class: Map },
nan: {},
null: {},
number: {},
numberish: { primitive: "number", class: Number },
object: {},
promise: { class: Promise },
regex: { class: RegExp },
set: { class: Set },
string: { primitive: "string", class: String },
symbol: { primitive: "symbol", },
undefined: { primitive: "undefined", },
weakmap: { class: WeakMap },
weakset: { class: WeakSet },
};
/**
* A collection of boolean properties set according to the type, and sometimes value, of the argument.
* @class TypeTest
*/
class TypeTest {
#typeName;
/**
* @constructor
* @param {*} value - The value to be tested.
*/
constructor(value){
let typeName;
if(value === null){
typeName = "null";
}
else{
typeName = "object";
for(const name in TYPES){
const type = TYPES[name];
if(typeof value === type.primitive || (type.class && value instanceof type.class)){
typeName = name;
break;
}
}
}
if(typeName === "numberish"){
if(Number.isNaN(value) || (value instanceof Number && Number.isNaN(value.valueOf()))){
typeName = "nan";
}
else{
typeName = "number";
}
}
this.#typeName = typeName;
for(const name in TYPES){
this[name] = typeName === name;
}
this.object = value instanceof Object;
this.primitive = !this.object;
this.objectish = this.object || this.null;
this.numberish = this.number || this.nan;
if(this.number){
this.real = Number.isFinite(this.object ? value.valueOf() : value);
this.infinite = !this.real;
}
else{
this.real = this.infinite = false;
}
this.defined = !this.undefined;
this.nullish = this.undefined || this.null;
this.false = value === false;
this.true = value === true;
this.falsy = !value;
this.truthy = !this.falsy;
if(this.string || this.array){
this.empty = value.length === 0;
this.nonempty = !this.empty;
}
else if(this.map || this.set){
this.empty = value.size === 0;
this.nonempty = !this.empty;
}
else{
this.empty = this.nonempty = false;
}
}
toString(){
return this.#typeName;
}
}
class IsType extends TypeTest {
#valueConstructor;
constructor(value){
super(value);
if(value instanceof Object) this.#valueConstructor = value.constructor;
}
get type(){
return this.toString();
}
of(constructor){
return !!(this.#valueConstructor && (this.#valueConstructor === constructor || this.#valueConstructor.prototype instanceof constructor));
}
all(...propNames){
return propNames.every(propName=>this[propName]);
}
any(...propNames){
return propNames.some(propName=>this[propName]);
}
}
/**
* Determine the type of a value. The returned object includes boolean properties to quickly test against specific types or for specific states (e.g., 'empty').
* @param {*} value - The value to be tested.
* @returns {IsType}
*/
function is(value){
return new IsType(value);
}
for(const propName in new TypeTest()){
Object.defineProperty(is, propName, {
value: propName,
enumerable: true,
});
}
export { is as default };