-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
utils.ts
270 lines (249 loc) Β· 7.43 KB
/
utils.ts
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
import { readdir } from 'node:fs/promises';
import { basename, join } from 'node:path';
import { setTimeout } from 'node:timers/promises';
import { EmbedColors, type ColorResolvable, type Logger, type ObjectToLower, type ObjectToSnake } from '..';
/**
* Resolves the color to a numeric representation.
* @param color The color to resolve.
* @returns The numeric representation of the color.
*/
export function resolveColor(color: ColorResolvable): number {
switch (typeof color) {
case 'string':
if (color === 'Random') return Math.floor(Math.random() * (0xffffff + 1));
if (color.startsWith('#')) return Number.parseInt(color.slice(1), 16);
if (color in EmbedColors) return EmbedColors[color as keyof typeof EmbedColors];
return EmbedColors.Default;
case 'number':
return color;
case 'object':
if (Array.isArray(color)) return (color[0] << 16) + (color[1] << 8) + color[2];
break;
default:
return color;
}
return color;
}
/**
* Delays the resolution of a Promise by the specified time.
* @param time The time in milliseconds to delay the resolution.
* @param result The value to resolve with after the delay.
* @returns A Promise that resolves after the specified time with the provided result.
*/
export function delay<T>(time: number, result?: T): Promise<T> {
return setTimeout(time, result);
}
/**
* Checks if a given value is an object.
* @param o The value to check.
* @returns `true` if the value is an object, otherwise `false`.
*/
export function isObject(o: any): boolean {
return o && typeof o === 'object' && !Array.isArray(o);
}
/**
* Merges multiple options objects together, deeply extending objects.
* @param defaults The default options object.
* @param options Additional options objects to merge.
* @returns The merged options object.
*/
export function MergeOptions<T>(defaults: any, ...options: any[]): T {
const option = options.shift();
if (!option) {
return defaults;
}
return MergeOptions(
{
...option,
...Object.fromEntries(
Object.entries(defaults).map(([key, value]) => [
key,
isObject(value) ? MergeOptions(value, option?.[key] || {}) : option?.[key] ?? value,
]),
),
},
...options,
);
}
/**
* Splits an array into two arrays based on the result of a predicate function.
* @param arr The array to split.
* @param func The predicate function used to test elements of the array.
* @returns An object containing two arrays: one with elements that passed the test and one with elements that did not.
*/
export function filterSplit<Element, Predicate extends (value: Element) => boolean>(arr: Element[], func: Predicate) {
const expect: Element[] = [];
const never: Element[] = [];
for (const element of arr) {
const test = func(element);
if (test) expect.push(element);
else never.push(element);
}
return { expect, never };
}
/**
* Represents a base handler class.
*/
export class BaseHandler {
/**
* Initializes a new instance of the BaseHandler class.
* @param logger The logger instance.
*/
constructor(protected logger: Logger) {}
/**
* Filters a file path.
* @param path The path to filter.
* @returns `true` if the path passes the filter, otherwise `false`.
*/
protected filter = (path: string) => !!path;
/**
* Recursively retrieves all files in a directory.
* @param dir The directory path.
* @returns A Promise that resolves to an array of file paths.
*/
protected async getFiles(dir: string) {
const files: string[] = [];
for (const i of await readdir(dir, { withFileTypes: true })) {
if (i.isDirectory()) {
files.push(...(await this.getFiles(join(dir, i.name))));
} else {
if (this.filter(join(dir, i.name))) {
files.push(join(dir, i.name));
}
}
}
return files;
}
/**
* Loads files from given paths.
* @param paths The paths of the files to load.
* @returns A Promise that resolves to an array of loaded files.
*/
protected async loadFiles<T extends NonNullable<unknown>>(paths: string[]): Promise<T[]> {
return Promise.all(paths.map(path => magicImport(path).then(file => file.default ?? file)));
}
/**
* Loads files from given paths along with additional information.
* @param paths The paths of the files to load.
* @returns A Promise that resolves to an array of objects containing name, file, and path.
*/
protected async loadFilesK<T>(paths: string[]): Promise<{ name: string; file: T; path: string }[]> {
return Promise.all(
paths.map(path =>
magicImport(path).then(file => {
return {
name: basename(path),
file: file.default ?? file,
path,
};
}),
),
);
}
}
/**
* Convert a camelCase object to snake_case.
* @param target The object to convert.
* @returns The converted object.
*/
export function toSnakeCase<Obj extends Record<string, any>>(target: Obj): ObjectToSnake<Obj> {
const result: Record<string, any> = {};
for (const [key, value] of Object.entries(target)) {
switch (typeof value) {
case 'string':
case 'bigint':
case 'boolean':
case 'function':
case 'number':
case 'symbol':
case 'undefined':
result[ReplaceRegex.snake(key)] = value;
break;
case 'object':
if (Array.isArray(value)) {
result[ReplaceRegex.snake(key)] = value.map(prop =>
typeof prop === 'object' && prop ? toSnakeCase(prop) : prop,
);
break;
}
if (isObject(value)) {
result[ReplaceRegex.snake(key)] = toSnakeCase(value);
break;
}
if (!Number.isNaN(value)) {
result[ReplaceRegex.snake(key)] = null;
break;
}
result[ReplaceRegex.snake(key)] = toSnakeCase(value);
break;
}
}
return result as ObjectToSnake<Obj>;
}
/**
* Convert a snake_case object to camelCase.
* @param target The object to convert.
* @returns The converted object.
*/
export function toCamelCase<Obj extends Record<string, any>>(target: Obj): ObjectToLower<Obj> {
const result: Record<string, any> = {};
for (const [key, value] of Object.entries(target)) {
switch (typeof value) {
case 'string':
case 'bigint':
case 'boolean':
case 'function':
case 'symbol':
case 'number':
case 'undefined':
result[ReplaceRegex.camel(key)] = value;
break;
case 'object':
if (Array.isArray(value)) {
result[ReplaceRegex.camel(key)] = value.map(prop =>
typeof prop === 'object' && prop ? toCamelCase(prop) : prop,
);
break;
}
if (isObject(value)) {
result[ReplaceRegex.camel(key)] = toCamelCase(value);
break;
}
if (!Number.isNaN(value)) {
result[ReplaceRegex.camel(key)] = null;
break;
}
result[ReplaceRegex.camel(key)] = toCamelCase(value);
break;
}
}
return result as ObjectToLower<Obj>;
}
export const ReplaceRegex = {
camel: (s: string) => {
return s.toLowerCase().replace(/(_\S)/gi, a => a[1].toUpperCase());
},
snake: (s: string) => {
return s.replace(/[A-Z]/g, a => `_${a.toLowerCase()}`);
},
};
export async function magicImport(path: string) {
try {
return require(path);
} catch {
// biome-ignore lint/security/noGlobalEval: modules import broke
return eval('((path) => import(`file:///${path}?update=${Date.now()}`))')(path.split('\\').join('\\\\'));
}
}
export type OnFailCallback = (error: unknown) => any;
export function fakePromise<T = unknown | Promise<unknown>>(
value: T,
): {
then<R>(callback: (arg: Awaited<T>) => R): R;
} {
if (value instanceof Promise) return value as any;
return {
// biome-ignore lint/suspicious/noThenProperty: magic
then: callback => callback(value as Awaited<T>),
};
}