1
+ import {
2
+ InvalidType ,
3
+ NotRequestedFormat ,
4
+ DataLimitExceeded
5
+ } from "../structures/Error.js" ;
6
+
7
+ const upperFirst = ( str = "string!" ) => ( String ( str ) . replace ( / ^ \w / , ( c ) => c . toUpperCase ( ) ) ) ;
8
+
9
+ export class Checker {
10
+ /**
11
+ * Creates new Checker.
12
+ * @param {any } data
13
+ * @constructor
14
+ */
15
+ constructor ( data ) {
16
+ /**
17
+ * @protected
18
+ */
19
+ this . data = data ;
20
+ } ;
21
+
22
+ /**
23
+ * Get provided data type.
24
+ * @param {any } data
25
+ * @param {boolean } upper
26
+ * @returns {("string" | "object" | "array" | "symbol" | "null" | "boolean" | "bigint" | "number" | "function") | ("String" | "Object" | "Array" | "Symbol" | "Null" | "Boolean" | "Bigint" | "Number" | "Function") }
27
+ */
28
+ type ( data = this . data , upper ) {
29
+ if ( upper && typeof upper === "boolean" ) return ;
30
+
31
+ let type = ( typeof data ) ;
32
+
33
+ if ( Array . isArray ( data ) ) type = "array" ;
34
+ else if ( data == null || data == "null" || data == "undefined" ) type = "null" ;
35
+
36
+ return ( upper ? upperFirst ( type ) : type ) ;
37
+ } ;
38
+
39
+ /**
40
+ * Checks if data is Boolean.
41
+ * @returns {boolean }
42
+ */
43
+ get isBoolean ( ) {
44
+ if ( this . isNull || this . isUndefined ) return false ;
45
+
46
+ if ( this . type ( ) === "boolean" || this . type ( ) === "Boolean" ) return true ;
47
+ else return false ;
48
+ } ;
49
+
50
+ /**
51
+ * Checks if data is String.
52
+ * @returns {boolean }
53
+ */
54
+ get isString ( ) {
55
+ if ( this . isNull || this . isUndefined ) return false ;
56
+
57
+ if ( this . type ( ) === "string" || this . type ( ) === "String" ) return true ;
58
+ else return false ;
59
+ } ;
60
+
61
+ /**
62
+ * Checks if data is Object.
63
+ * @returns {boolean }
64
+ */
65
+ get isObject ( ) {
66
+ if ( this . isNull || this . isUndefined ) return false ;
67
+
68
+ if ( this . type ( ) === "object" || this . type ( ) === "Object" ) return true ;
69
+ else return false ;
70
+ } ;
71
+
72
+ /**
73
+ * Checks if data is Symbol.
74
+ * @returns {boolean }
75
+ */
76
+ get isSymbol ( ) {
77
+ if ( this . isNull || this . isUndefined ) return false ;
78
+
79
+ if ( this . type ( ) === "symbol" || this . type ( ) === "Symbol" ) return true ;
80
+ else return false ;
81
+ } ;
82
+
83
+ /**
84
+ * Checks if data is Array.
85
+ * @returns {boolean }
86
+ */
87
+ get isArray ( ) {
88
+ if ( this . isNull || this . isUndefined ) return false ;
89
+
90
+ if ( this . type ( ) === "array" || this . type ( ) === "Array" ) return true ;
91
+ else return false ;
92
+ } ;
93
+
94
+ /**
95
+ * Checks if data is Number.
96
+ * @returns {boolean }
97
+ */
98
+ get isNumber ( ) {
99
+ if ( this . isNull || this . isUndefined ) return false ;
100
+
101
+ if ( this . type ( ) === "number" || this . type ( ) === "Number" ) return true ;
102
+ else return false ;
103
+ } ;
104
+
105
+ /**
106
+ * Checks if data is Function.
107
+ * @returns {boolean }
108
+ */
109
+ get isFunction ( ) {
110
+ if ( this . isNull || this . isUndefined ) return false ;
111
+
112
+ if ( this . type ( ) === "function" || this . type ( ) === "Function" ) return true ;
113
+ else return false ;
114
+ } ;
115
+
116
+ /**
117
+ * Checks if data is Undefined (Null).
118
+ * @returns {boolean }
119
+ */
120
+ get isUndefined ( ) {
121
+ if ( this . type ( ) === "null" || this . type ( ) === "Null" ) return true ;
122
+ else return false ;
123
+ } ;
124
+
125
+ /**
126
+ * Checks if data is Null.
127
+ * @returns {boolean }
128
+ */
129
+ get isNull ( ) {
130
+ if ( this . isUndefined ) return true ;
131
+ else return false ;
132
+ } ;
133
+
134
+ /**
135
+ * Checks if data is BigInt.
136
+ * @returns {boolean }
137
+ */
138
+ get isBigInt ( ) {
139
+ if ( this . isNull || this . isUndefined ) return false ;
140
+
141
+ if ( this . type ( ) === "bigint" || this . type ( ) === "Bigint" ) return true ;
142
+ else return false ;
143
+ } ;
144
+
145
+ /**
146
+ * Checks if data is Available.
147
+ * @returns {boolean }
148
+ */
149
+ get isAvailable ( ) {
150
+ if ( this . isNull || this . isUndefined ) return false ;
151
+ else return true ;
152
+ } ;
153
+
154
+ /**
155
+ * Checks that the data is not Boolean.
156
+ * @returns {boolean }
157
+ */
158
+ get isNotBoolean ( ) {
159
+ if ( this . isBoolean ) return false ;
160
+ else return true ;
161
+ } ;
162
+
163
+ /**
164
+ * Checks that the data is not String.
165
+ * @returns {boolean }
166
+ */
167
+ get isNotString ( ) {
168
+ if ( this . isString ) return false ;
169
+ else return true ;
170
+ } ;
171
+
172
+ /**
173
+ * Checks that the data is not Object.
174
+ * @returns {boolean }
175
+ */
176
+ get isNotObject ( ) {
177
+ if ( this . isObject ) return false ;
178
+ else return true ;
179
+ } ;
180
+
181
+ /**
182
+ * Checks that the data is not Symbol.
183
+ * @returns {boolean }
184
+ */
185
+ get isNotSymbol ( ) {
186
+ if ( this . isSymbol ) return false ;
187
+ else return true ;
188
+ } ;
189
+
190
+ /**
191
+ * Checks that the data is not Array.
192
+ * @returns {boolean }
193
+ */
194
+ get isNotArray ( ) {
195
+ if ( this . isArray ) return false ;
196
+ else return true ;
197
+ } ;
198
+
199
+ /**
200
+ * Checks that the data is not Number.
201
+ * @returns {boolean }
202
+ */
203
+ get isNotNumber ( ) {
204
+ if ( this . isNumber ) return false ;
205
+ else return true ;
206
+ } ;
207
+
208
+ /**
209
+ * Checks that the data is not Function.
210
+ * @returns {boolean }
211
+ */
212
+ get isNotFunction ( ) {
213
+ if ( this . isFunction ) return false ;
214
+ else return true ;
215
+ } ;
216
+
217
+ /**
218
+ * Checks that the data is not Undefined.
219
+ * @returns {boolean }
220
+ */
221
+ get isNotUndefined ( ) {
222
+ if ( this . isUndefined ) return false ;
223
+ else return true ;
224
+ } ;
225
+
226
+ /**
227
+ * Checks that the data is not Null.
228
+ * @returns {boolean }
229
+ */
230
+ get isNotNull ( ) {
231
+ if ( this . isNull ) return false ;
232
+ else return true ;
233
+ } ;
234
+
235
+ /**
236
+ * Checks that the data is not BigInt.
237
+ * @returns {boolean }
238
+ */
239
+ get isNotBigInt ( ) {
240
+ if ( this . isBigInt ) return false ;
241
+ else return true ;
242
+ } ;
243
+
244
+ /**
245
+ * Checks that the data is not Available.
246
+ * @returns {boolean }
247
+ */
248
+ get isNotAvailable ( ) {
249
+ if ( this . isAvailable ) return false ;
250
+ else return true ;
251
+ } ;
252
+
253
+ /**
254
+ * Creates a new Error.
255
+ * @param {{ argument: string, errorType?: string, expected?: string, received: string | null} }
256
+ * @returns {{ throw: () => void } }
257
+ */
258
+ createError ( { argument = null , errorType = "InvalidType" , expected = "String" , received } ) {
259
+ if ( this . type ( argument ) !== "string" ) throw new InvalidType ( "argument" , { expected : "String" , received : upperFirst ( typeof argument ) } ) ;
260
+ if ( this . type ( errorType ) !== "string" ) throw new InvalidType ( "type" , { expected : "String" , received : upperFirst ( typeof errorType ) } ) ;
261
+
262
+ const type = ( errorType . replace ( / / g, "_" ) ) . toUpperCase ( ) ;
263
+
264
+ const fetched = upperFirst ( received ?? this . toString ( ) ) ;
265
+
266
+ let error = new InvalidType ( argument , { expected, received : fetched } ) ;
267
+
268
+ if ( type === "NOT_FOUND" ) error = new NotFound ( argument ) ;
269
+ else if ( type === "NOT_REQUESTED_FORMAT" ) error = new NotRequestedFormat ( argument , { expected, received : fetched } ) ;
270
+ else if ( type === "DATA_LIMIT_EXCEEDED" ) error = new DataLimitExceeded ( argument , expected , fetched ) ;
271
+
272
+ const isNotUndefined = this . isNotUndefined ;
273
+ const isNotNull = this . isNotNull ;
274
+
275
+ const getType = this . type ;
276
+ /**
277
+ * Send the created error to the console.
278
+ * @param {boolean } condition
279
+ * @returns {void }
280
+ */
281
+ function throwError ( condition ) {
282
+ if ( getType ( condition ) === "boolean" ) throw new InvalidType ( "condition" , { expected : "Boolean" , received : upperFirst ( typeof condition ) } ) ;
283
+
284
+ if ( condition && isNotUndefined && isNotNull ) throw error ;
285
+ } ;
286
+
287
+ return { throw : throwError } ;
288
+ } ;
289
+
290
+ /**
291
+ * Default type of the "this.data" value.
292
+ * @param {boolean } upperFirstChar
293
+ * @returns {string }
294
+ */
295
+ toString ( upperFirstChar = true ) {
296
+ return ( this . type ( this . data , upperFirstChar ) ) ;
297
+ } ;
298
+ } ;
0 commit comments