-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasserter.js
310 lines (255 loc) · 7.79 KB
/
asserter.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const fs = require("fs");
const config = require("./config/config.json");
const { TypeError, RangeError, LengthError, SemanticError, InstanceOfError, InvalidDiscordIdError, InvalidPathError, PermissionsError } = require("./errors/custom_errors.js");
exports.isArray = isArray;
exports.isObject = isObject;
exports.isSerializedBuffer = isSerializedBuffer;
exports.isString = isString;
exports.isNumber = isNumber;
exports.isInteger = isInteger;
exports.isBoolean = isBoolean;
exports.isFunction = isFunction;
exports.isRegexp = isRegexp;
exports.isParsedByRegexp = isParsedByRegexp;
exports.isLessThanNCharacters = isLessThanNCharacters;
exports.isMoreThanNCharacters = isMoreThanNCharacters;
exports.isNumberInRange = isNumberInRange;
exports.isStringInArray = isStringInArray;
exports.isInstanceOfPrototype = isInstanceOfPrototype;
exports.isPermissionsError = isPermissionsError;
exports.isSemanticError = isSemanticError;
exports.doesStringEndIn = doesStringEndIn;
exports.isValidGameType = isValidGameType;
exports.isDom5GameType = isDom5GameType;
exports.isDom6GameType = isDom6GameType;
exports.isValidPath = isValidPath;
exports.isValidDiscordId = isValidDiscordId;
exports.isArrayOrThrow = isArrayOrThrow;
exports.isObjectOrThrow = isObjectOrThrow;
exports.isSerializedBufferOrThrow = isSerializedBufferOrThrow;
exports.isStringOrThrow = isStringOrThrow;
exports.isNumberOrThrow = isNumberOrThrow;
exports.isIntegerOrThrow = isIntegerOrThrow;
exports.isBooleanOrThrow = isBooleanOrThrow;
exports.isFunctionOrThrow = isFunctionOrThrow;
exports.isRegexpOrThrow = isRegexpOrThrow;
exports.isParsedByRegexpOrThrow = isParsedByRegexpOrThrow;
exports.isLessThanNCharactersOrThrow = isLessThanNCharactersOrThrow;
exports.isMoreThanNCharactersOrThrow = isMoreThanNCharactersOrThrow;
exports.isNumberInRangeOrThrow = isNumberInRangeOrThrow;
exports.isStringInArrayOrThrow = isStringInArrayOrThrow;
exports.isInstanceOfPrototypeOrThrow = isInstanceOfPrototypeOrThrow;
exports.isSemanticErrorOrThrow = isSemanticErrorOrThrow;
exports.doesStringEndInOrThrow = doesStringEndInOrThrow;
exports.isValidGameTypeOrThrow = isValidGameTypeOrThrow;
exports.isValidPathOrThrow = isValidPathOrThrow;
exports.isValidDiscordIdOrThrow = isValidDiscordIdOrThrow;
function isArray(arr)
{
return Array.isArray(arr);
}
function isObject(obj)
{
return Array.isArray(obj) === false && typeof obj === "object" && obj != null;
}
function isSerializedBuffer(obj)
{
return isObject(obj) === true && obj.type === "Buffer" && isArray(obj.data) === true;
}
function isString(str)
{
return typeof str === "string";
}
function isBoolean(bool)
{
return typeof bool === "boolean";
}
function isFunction(fn)
{
return typeof fn === "function";
}
function isRegexp(regexp)
{
return RegExp.prototype.isPrototypeOf(regexp) === true;
}
function isParsedByRegexp(str, regexp)
{
return regexp.test(str) === true;
}
function isLessThanNCharacters(str, n)
{
return str.length < n;
}
function isMoreThanNCharacters(str, n)
{
return str.length > n;
}
function isNumber(nbr)
{
return isNaN(nbr) === false;
}
function isInteger(nbr)
{
return Number.isInteger(nbr);
}
function isNumberInRange(nbr, min, max)
{
return nbr > min && nbr < max;
}
function isStringInArray(str, array)
{
return array.includes(str) === true;
}
function isInstanceOfPrototype(instance, prototypeDef)
{
return prototypeDef.prototype.isPrototypeOf(instance);
}
function isPermissionsError(error)
{
return isInstanceOfPrototype(error, PermissionsError);
}
function isSemanticError(error)
{
return isInstanceOfPrototype(error, SemanticError);
}
function doesStringEndIn(str, ending)
{
return isArray(str.match(`^.*${ending}$`));
}
function isValidPath(path)
{
return fs.existsSync(path);
}
function isValidDiscordId(id)
{
return isString(id) === true && /^\d+$/.test(id) === true;
}
function isValidGameType(gameType)
{
if (isString(gameType) === false)
return false;
if (gameType.toLowerCase() === config.dom5GameTypeName.toLowerCase() ||
gameType.toLowerCase() === config.dom6GameTypeName.toLowerCase())
{
return true;
}
else return false;
}
function isDom5GameType(gameType)
{
if (isValidGameType(gameType) === false)
return false;
return gameType.toLowerCase() === config.dom5GameTypeName.toLowerCase();
}
function isDom6GameType(gameType)
{
if (isValidGameType(gameType) === false)
return false;
return gameType.toLowerCase() === config.dom6GameTypeName.toLowerCase();
}
function isArrayOrThrow(arr)
{
if (isArray(arr) === false)
throw new TypeError(`Expected Array, got: <${arr}> (${typeof arr})`);
}
function isObjectOrThrow(obj)
{
if (isObject(obj) === false)
throw new TypeError(`Expected Object, got: <${obj}> (${typeof obj})`);
}
function isSerializedBufferOrThrow(obj)
{
if (isSerializedBuffer(obj) === false)
throw new TypeError(`Expected Serialized Buffer, got: <${obj}> (${typeof obj})`);
}
function isStringOrThrow(str)
{
if (isString(str) === false)
throw new TypeError(`Expected String, got: <${str}> (${typeof str})`);
}
function isNumberOrThrow(nbr)
{
if (isNumber(nbr) === false)
throw new TypeError(`Expected Number, got: <${nbr}> (${typeof nbr})`);
}
function isIntegerOrThrow(nbr)
{
if (isInteger(nbr) === false)
throw new TypeError(`Expected Integer, got: <${nbr}> (${typeof nbr})`);
}
function isBooleanOrThrow(bool)
{
if (isBoolean(bool) === false)
throw new TypeError(`Expected Boolean, got: <${bool}> (${typeof bool})`);
}
function isFunctionOrThrow(fn)
{
if (isFunction(fn) === false)
throw new TypeError(`Expected Function, got: <${fn}> (${typeof fn})`);
}
function isRegexpOrThrow(regexp)
{
if (isRegexp(regexp) === false)
throw new TypeError(`Expected RegExp, got: <${regexp}> (${typeof regexp})`);
}
function isParsedByRegexpOrThrow(str, regexp)
{
isRegexpOrThrow(regexp);
if (isParsedByRegexp(str, regexp) === false)
throw new SemanticError(`String could not be parsed by regexp: ${str}`);
}
function isLessThanNCharactersOrThrow(str, n)
{
if (isLessThanNCharacters(str, n) === false)
throw new LengthError(`Expected String to be less than ${n} characters, got: <${str.length}>`);
}
function isMoreThanNCharactersOrThrow(str, n)
{
if (isMoreThanNCharacters(str, n) === false)
throw new LengthError(`Expected String to be more than ${n} characters, got: <${str.length}>`);
}
function isNumberInRangeOrThrow(nbr, min, max)
{
isNumberOrThrow(nbr);
isNumberOrThrow(min);
isNumberOrThrow(max);
if (isNumberInRange(nbr, min, max) === false)
throw new SemanticError(`Expected Number > ${min} and < ${max}, got: <${nbr}>`);
}
function isStringInArrayOrThrow(str, array)
{
if (isStringInArray(str, array) === false)
throw new SemanticError(`Got <${str}>, expected it to be one of: ${array}`);
}
function isInstanceOfPrototypeOrThrow(instance, prototypeDef)
{
if (isInstanceOfPrototype(instance, prototypeDef) === false)
throw new InstanceOfError(`Expected instance of ${prototypeDef.name}, got: <${instance}>`);
}
function isSemanticErrorOrThrow(error)
{
if (isSemanticError(error) === false)
throw new InstanceOfError(`Expected instance of ${SemanticError.name}, got: <${error}>`);
}
function doesStringEndInOrThrow(str, ending)
{
isStringOrThrow(str);
isStringOrThrow(ending);
if (doesStringEndIn(str, ending) === false)
throw new SemanticError(`Expected string to end in <${ending}>, got <${str}>`);
}
function isValidGameTypeOrThrow(gameType)
{
if (isValidGameType(gameType) === false)
throw new SemanticError(`Value of gameType does not match any configured value, got: <${gameType}>`);
}
function isValidPathOrThrow(path)
{
if (isValidPath(path) === false)
throw new InvalidPathError(`Path does not exist: ${path}`);
}
function isValidDiscordIdOrThrow(id)
{
if (isValidDiscordId(id) === false)
throw new InvalidDiscordIdError(`Id is not a valid Discord Id: ${id}`);
}