@oxog/utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/index.cjs +645 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +780 -0
- package/dist/index.d.ts +780 -0
- package/dist/index.js +611 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/async.cjs +188 -0
- package/dist/plugins/async.cjs.map +1 -0
- package/dist/plugins/async.d.cts +169 -0
- package/dist/plugins/async.d.ts +169 -0
- package/dist/plugins/async.js +182 -0
- package/dist/plugins/async.js.map +1 -0
- package/dist/plugins/deep.cjs +157 -0
- package/dist/plugins/deep.cjs.map +1 -0
- package/dist/plugins/deep.d.cts +157 -0
- package/dist/plugins/deep.d.ts +157 -0
- package/dist/plugins/deep.js +152 -0
- package/dist/plugins/deep.js.map +1 -0
- package/dist/plugins/guard.cjs +47 -0
- package/dist/plugins/guard.cjs.map +1 -0
- package/dist/plugins/guard.d.cts +213 -0
- package/dist/plugins/guard.d.ts +213 -0
- package/dist/plugins/guard.js +38 -0
- package/dist/plugins/guard.js.map +1 -0
- package/dist/plugins/transform.cjs +49 -0
- package/dist/plugins/transform.cjs.map +1 -0
- package/dist/plugins/transform.d.cts +231 -0
- package/dist/plugins/transform.d.ts +231 -0
- package/dist/plugins/transform.js +42 -0
- package/dist/plugins/transform.js.map +1 -0
- package/dist/types-XmRC5wYO.d.cts +97 -0
- package/dist/types-XmRC5wYO.d.ts +97 -0
- package/package.json +115 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __export = (target, all) => {
|
|
5
|
+
for (var name in all)
|
|
6
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/plugins/core/object.ts
|
|
10
|
+
var object_exports = {};
|
|
11
|
+
__export(object_exports, {
|
|
12
|
+
entries: () => entries,
|
|
13
|
+
fromEntries: () => fromEntries,
|
|
14
|
+
get: () => get,
|
|
15
|
+
has: () => has,
|
|
16
|
+
keys: () => keys,
|
|
17
|
+
merge: () => merge,
|
|
18
|
+
omit: () => omit,
|
|
19
|
+
pick: () => pick,
|
|
20
|
+
set: () => set,
|
|
21
|
+
setMut: () => setMut,
|
|
22
|
+
values: () => values
|
|
23
|
+
});
|
|
24
|
+
function get(obj, path, defaultValue) {
|
|
25
|
+
if (obj == null) {
|
|
26
|
+
return defaultValue;
|
|
27
|
+
}
|
|
28
|
+
const keys2 = path.toString().split(/[\.\[]/g).map((k) => k.replace(/[\]']/g, "")).filter((k) => k !== "");
|
|
29
|
+
let result = obj;
|
|
30
|
+
for (const key of keys2) {
|
|
31
|
+
if (result == null) {
|
|
32
|
+
return defaultValue;
|
|
33
|
+
}
|
|
34
|
+
result = result[key];
|
|
35
|
+
}
|
|
36
|
+
if (typeof result === "function") {
|
|
37
|
+
return defaultValue;
|
|
38
|
+
}
|
|
39
|
+
return result ?? defaultValue;
|
|
40
|
+
}
|
|
41
|
+
function set(obj, path, value) {
|
|
42
|
+
if (obj == null || typeof obj !== "object") {
|
|
43
|
+
return obj;
|
|
44
|
+
}
|
|
45
|
+
const keys2 = path.toString().split(".");
|
|
46
|
+
const deepSet = (current, keyIndex) => {
|
|
47
|
+
if (keyIndex === keys2.length) {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
const key = keys2[keyIndex];
|
|
51
|
+
const isLast = keyIndex === keys2.length - 1;
|
|
52
|
+
if (isLast) {
|
|
53
|
+
const arrayMatch = key.match(/^(.+)\[(\d+)\]$/);
|
|
54
|
+
if (arrayMatch) {
|
|
55
|
+
const [, arrayPath, index] = arrayMatch;
|
|
56
|
+
const arrayIndex = parseInt(index, 10);
|
|
57
|
+
const arrayValue = current?.[arrayPath];
|
|
58
|
+
if (Array.isArray(arrayValue)) {
|
|
59
|
+
return {
|
|
60
|
+
...current,
|
|
61
|
+
[arrayPath]: [
|
|
62
|
+
...arrayValue.slice(0, arrayIndex),
|
|
63
|
+
value,
|
|
64
|
+
...arrayValue.slice(arrayIndex + 1)
|
|
65
|
+
]
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { ...current, [key]: value };
|
|
70
|
+
}
|
|
71
|
+
const nextValue = current?.[key];
|
|
72
|
+
const nestedObj = nextValue != null && typeof nextValue === "object" ? nextValue : {};
|
|
73
|
+
return { ...current, [key]: deepSet(nestedObj, keyIndex + 1) };
|
|
74
|
+
};
|
|
75
|
+
return deepSet(obj, 0);
|
|
76
|
+
}
|
|
77
|
+
function setMut(obj, path, value) {
|
|
78
|
+
if (obj == null || typeof obj !== "object") {
|
|
79
|
+
return obj;
|
|
80
|
+
}
|
|
81
|
+
const keys2 = path.toString().split(".");
|
|
82
|
+
let result = obj;
|
|
83
|
+
for (let i = 0; i < keys2.length - 1; i++) {
|
|
84
|
+
const key = keys2[i];
|
|
85
|
+
if (result[key] == null || typeof result[key] !== "object") {
|
|
86
|
+
result[key] = {};
|
|
87
|
+
}
|
|
88
|
+
result = result[key];
|
|
89
|
+
}
|
|
90
|
+
result[keys2[keys2.length - 1]] = value;
|
|
91
|
+
return obj;
|
|
92
|
+
}
|
|
93
|
+
function has(obj, path) {
|
|
94
|
+
if (obj == null) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const keys2 = path.toString().split(/[\.\[]/g).map((k) => k.replace(/[\]']/g, "")).filter((k) => k !== "");
|
|
98
|
+
let result = obj;
|
|
99
|
+
for (const key of keys2) {
|
|
100
|
+
if (Array.isArray(result) && /^\d+$/.test(key)) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
if (result == null || !(key in result)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
result = result[key];
|
|
107
|
+
}
|
|
108
|
+
return result !== void 0;
|
|
109
|
+
}
|
|
110
|
+
function pick(obj, keys2) {
|
|
111
|
+
const result = {};
|
|
112
|
+
for (const key of keys2) {
|
|
113
|
+
if (key in obj) {
|
|
114
|
+
result[key] = obj[key];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
function omit(obj, keys2) {
|
|
120
|
+
const result = { ...obj };
|
|
121
|
+
for (const key of keys2) {
|
|
122
|
+
delete result[key];
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
function merge(target, ...sources) {
|
|
127
|
+
const DANGEROUS_KEYS = ["__proto__", "constructor", "prototype"];
|
|
128
|
+
const result = { ...target };
|
|
129
|
+
for (const source of sources) {
|
|
130
|
+
for (const key of Object.keys(source)) {
|
|
131
|
+
if (DANGEROUS_KEYS.includes(key)) continue;
|
|
132
|
+
result[key] = source[key];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
function keys(obj) {
|
|
138
|
+
return Object.keys(obj);
|
|
139
|
+
}
|
|
140
|
+
function values(obj) {
|
|
141
|
+
return Object.values(obj);
|
|
142
|
+
}
|
|
143
|
+
function entries(obj) {
|
|
144
|
+
return Object.entries(obj);
|
|
145
|
+
}
|
|
146
|
+
function fromEntries(entries2) {
|
|
147
|
+
return Object.fromEntries(entries2);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/plugins/core/array.ts
|
|
151
|
+
var array_exports = {};
|
|
152
|
+
__export(array_exports, {
|
|
153
|
+
chunk: () => chunk,
|
|
154
|
+
compact: () => compact,
|
|
155
|
+
first: () => first,
|
|
156
|
+
flatten: () => flatten,
|
|
157
|
+
groupBy: () => groupBy,
|
|
158
|
+
keyBy: () => keyBy,
|
|
159
|
+
last: () => last,
|
|
160
|
+
sample: () => sample,
|
|
161
|
+
uniq: () => uniq,
|
|
162
|
+
uniqBy: () => uniqBy
|
|
163
|
+
});
|
|
164
|
+
function groupBy(arr, key) {
|
|
165
|
+
const result = {};
|
|
166
|
+
for (const item of arr) {
|
|
167
|
+
const groupKey = typeof key === "function" ? key(item) : String(item[key]);
|
|
168
|
+
if (!result[groupKey]) {
|
|
169
|
+
result[groupKey] = [];
|
|
170
|
+
}
|
|
171
|
+
result[groupKey].push(item);
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
function keyBy(arr, key) {
|
|
176
|
+
const result = {};
|
|
177
|
+
for (const item of arr) {
|
|
178
|
+
const keyValue = typeof key === "function" ? key(item) : String(item[key]);
|
|
179
|
+
result[keyValue] = item;
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
function chunk(arr, size) {
|
|
184
|
+
const result = [];
|
|
185
|
+
for (let i = 0; i < arr.length; i += size) {
|
|
186
|
+
result.push(arr.slice(i, i + size));
|
|
187
|
+
}
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
function uniq(arr) {
|
|
191
|
+
return Array.from(new Set(arr));
|
|
192
|
+
}
|
|
193
|
+
function uniqBy(arr, key) {
|
|
194
|
+
const seen = /* @__PURE__ */ new Set();
|
|
195
|
+
const result = [];
|
|
196
|
+
for (const item of arr) {
|
|
197
|
+
const keyValue = typeof key === "function" ? key(item) : String(item[key]);
|
|
198
|
+
if (!seen.has(keyValue)) {
|
|
199
|
+
seen.add(keyValue);
|
|
200
|
+
result.push(item);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
function flatten(arr, depth = 1) {
|
|
206
|
+
const result = [];
|
|
207
|
+
for (const item of arr) {
|
|
208
|
+
if (Array.isArray(item) && depth > 0) {
|
|
209
|
+
result.push(...flatten(item, depth - 1));
|
|
210
|
+
} else {
|
|
211
|
+
result.push(item);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
function compact(arr) {
|
|
217
|
+
return arr.filter(Boolean);
|
|
218
|
+
}
|
|
219
|
+
function first(arr, n = 1) {
|
|
220
|
+
if (arr.length === 0) return void 0;
|
|
221
|
+
if (n === 1) return arr[0];
|
|
222
|
+
const result = arr.slice(0, n);
|
|
223
|
+
return result.length === 1 ? result[0] : result;
|
|
224
|
+
}
|
|
225
|
+
function last(arr, n = 1) {
|
|
226
|
+
if (arr.length === 0) return void 0;
|
|
227
|
+
if (n === 1) return arr[arr.length - 1];
|
|
228
|
+
return arr.slice(-n);
|
|
229
|
+
}
|
|
230
|
+
function sample(arr, n = 1) {
|
|
231
|
+
if (arr.length === 0) return [];
|
|
232
|
+
if (n >= arr.length) return [...arr];
|
|
233
|
+
const shuffled = [...arr];
|
|
234
|
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
235
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
236
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
237
|
+
}
|
|
238
|
+
return shuffled.slice(0, n);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// src/plugins/core/string.ts
|
|
242
|
+
var string_exports = {};
|
|
243
|
+
__export(string_exports, {
|
|
244
|
+
camelCase: () => camelCase,
|
|
245
|
+
capitalize: () => capitalize,
|
|
246
|
+
kebabCase: () => kebabCase,
|
|
247
|
+
pascalCase: () => pascalCase,
|
|
248
|
+
slugify: () => slugify,
|
|
249
|
+
snakeCase: () => snakeCase,
|
|
250
|
+
template: () => template,
|
|
251
|
+
truncate: () => truncate
|
|
252
|
+
});
|
|
253
|
+
function camelCase(str) {
|
|
254
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase().split(/[\s\-_]+/).filter((word) => word.length > 0).map(
|
|
255
|
+
(word, index) => index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1)
|
|
256
|
+
).join("");
|
|
257
|
+
}
|
|
258
|
+
function kebabCase(str) {
|
|
259
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase().replace(/[\s_]+/g, "-").replace(/^-+|-+$/g, "");
|
|
260
|
+
}
|
|
261
|
+
function snakeCase(str) {
|
|
262
|
+
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase().replace(/[\s-]+/g, "_").replace(/^_+|_+$/g, "");
|
|
263
|
+
}
|
|
264
|
+
function pascalCase(str) {
|
|
265
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase().split(/[\s\-_]+/).filter((word) => word.length > 0).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
266
|
+
}
|
|
267
|
+
function capitalize(str) {
|
|
268
|
+
if (!str) return str;
|
|
269
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
270
|
+
}
|
|
271
|
+
function truncate(str, length, suffix = "...") {
|
|
272
|
+
if (str.length <= length) return str;
|
|
273
|
+
return str.slice(0, length) + suffix;
|
|
274
|
+
}
|
|
275
|
+
function slugify(str) {
|
|
276
|
+
const charMap = {
|
|
277
|
+
// Turkish
|
|
278
|
+
"\u015F": "s",
|
|
279
|
+
"\u015E": "s",
|
|
280
|
+
"\u0131": "i",
|
|
281
|
+
"\u0130": "i",
|
|
282
|
+
"\u011F": "g",
|
|
283
|
+
"\u011E": "g",
|
|
284
|
+
"\xFC": "u",
|
|
285
|
+
"\xDC": "u",
|
|
286
|
+
"\xF6": "o",
|
|
287
|
+
"\xD6": "o",
|
|
288
|
+
"\xE7": "c",
|
|
289
|
+
"\xC7": "c",
|
|
290
|
+
// Russian (Cyrillic to Latin - basic transliteration)
|
|
291
|
+
"\u0430": "a",
|
|
292
|
+
"\u0431": "b",
|
|
293
|
+
"\u0432": "v",
|
|
294
|
+
"\u0433": "g",
|
|
295
|
+
"\u0434": "d",
|
|
296
|
+
"\u0435": "e",
|
|
297
|
+
"\u0451": "yo",
|
|
298
|
+
"\u0436": "zh",
|
|
299
|
+
"\u0437": "z",
|
|
300
|
+
"\u0438": "i",
|
|
301
|
+
"\u0439": "y",
|
|
302
|
+
"\u043A": "k",
|
|
303
|
+
"\u043B": "l",
|
|
304
|
+
"\u043C": "m",
|
|
305
|
+
"\u043D": "n",
|
|
306
|
+
"\u043E": "o",
|
|
307
|
+
"\u043F": "p",
|
|
308
|
+
"\u0440": "r",
|
|
309
|
+
"\u0441": "s",
|
|
310
|
+
"\u0442": "t",
|
|
311
|
+
"\u0443": "u",
|
|
312
|
+
"\u0444": "f",
|
|
313
|
+
"\u0445": "h",
|
|
314
|
+
"\u0446": "ts",
|
|
315
|
+
"\u0447": "ch",
|
|
316
|
+
"\u0448": "sh",
|
|
317
|
+
"\u0449": "shch",
|
|
318
|
+
"\u044A": "",
|
|
319
|
+
"\u044B": "y",
|
|
320
|
+
"\u044C": "",
|
|
321
|
+
"\u044D": "e",
|
|
322
|
+
"\u044E": "yu",
|
|
323
|
+
"\u044F": "ya",
|
|
324
|
+
// Greek (basic)
|
|
325
|
+
"\u03B1": "a",
|
|
326
|
+
"\u03B2": "b",
|
|
327
|
+
"\u03B3": "g",
|
|
328
|
+
"\u03B4": "d",
|
|
329
|
+
"\u03B5": "e",
|
|
330
|
+
"\u03B6": "z",
|
|
331
|
+
"\u03B7": "i",
|
|
332
|
+
"\u03B8": "th",
|
|
333
|
+
"\u03B9": "i",
|
|
334
|
+
"\u03BA": "k",
|
|
335
|
+
"\u03BB": "l",
|
|
336
|
+
"\u03BC": "m",
|
|
337
|
+
"\u03BD": "n",
|
|
338
|
+
"\u03BE": "x",
|
|
339
|
+
"\u03BF": "o",
|
|
340
|
+
"\u03C0": "p",
|
|
341
|
+
"\u03C1": "r",
|
|
342
|
+
"\u03C3": "s",
|
|
343
|
+
"\u03C4": "t",
|
|
344
|
+
"\u03C5": "y",
|
|
345
|
+
"\u03C6": "f",
|
|
346
|
+
"\u03C7": "ch",
|
|
347
|
+
"\u03C8": "ps",
|
|
348
|
+
"\u03C9": "o"
|
|
349
|
+
};
|
|
350
|
+
return str.split("").map((char) => charMap[char] || char).join("").toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "").replace(/[\s\-_]+/g, "-").replace(/[^a-z0-9-]/g, "").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
351
|
+
}
|
|
352
|
+
function template(str, data) {
|
|
353
|
+
return str.replace(/\{\{([^}]+)\}\}/g, (match, path) => {
|
|
354
|
+
const keys2 = path.trim().split(".");
|
|
355
|
+
let value = data;
|
|
356
|
+
for (const key of keys2) {
|
|
357
|
+
if (value == null) {
|
|
358
|
+
return "";
|
|
359
|
+
}
|
|
360
|
+
value = value[key];
|
|
361
|
+
}
|
|
362
|
+
return value !== void 0 ? String(value) : "";
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/utils/eventBus.ts
|
|
367
|
+
var EventBus = class {
|
|
368
|
+
listeners = /* @__PURE__ */ new Map();
|
|
369
|
+
/**
|
|
370
|
+
* Register an event listener.
|
|
371
|
+
*
|
|
372
|
+
* @param event - Event name to listen for
|
|
373
|
+
* @param handler - Function to call when event is emitted
|
|
374
|
+
*/
|
|
375
|
+
on(event, handler) {
|
|
376
|
+
if (!this.listeners.has(event)) {
|
|
377
|
+
this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
378
|
+
}
|
|
379
|
+
this.listeners.get(event).add(handler);
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Unregister an event listener.
|
|
383
|
+
*
|
|
384
|
+
* @param event - Event name
|
|
385
|
+
* @param handler - Function to remove
|
|
386
|
+
*/
|
|
387
|
+
off(event, handler) {
|
|
388
|
+
const handlers = this.listeners.get(event);
|
|
389
|
+
if (handlers) {
|
|
390
|
+
handlers.delete(handler);
|
|
391
|
+
if (handlers.size === 0) {
|
|
392
|
+
this.listeners.delete(event);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Emit an event to all registered listeners.
|
|
398
|
+
*
|
|
399
|
+
* @param event - Event name to emit
|
|
400
|
+
* @param args - Arguments to pass to handlers
|
|
401
|
+
*/
|
|
402
|
+
emit(event, ...args) {
|
|
403
|
+
const handlers = this.listeners.get(event);
|
|
404
|
+
if (handlers) {
|
|
405
|
+
for (const handler of handlers) {
|
|
406
|
+
try {
|
|
407
|
+
handler(...args);
|
|
408
|
+
} catch (error) {
|
|
409
|
+
console.error(`Error in event handler for "${event}":`, error);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Remove all listeners for an event or all events.
|
|
416
|
+
*
|
|
417
|
+
* @param event - Event name to clear (if not provided, clears all)
|
|
418
|
+
*/
|
|
419
|
+
removeAllListeners(event) {
|
|
420
|
+
if (event) {
|
|
421
|
+
this.listeners.delete(event);
|
|
422
|
+
} else {
|
|
423
|
+
this.listeners.clear();
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Get the number of listeners for an event.
|
|
428
|
+
*
|
|
429
|
+
* @param event - Event name
|
|
430
|
+
* @returns Number of listeners
|
|
431
|
+
*/
|
|
432
|
+
listenerCount(event) {
|
|
433
|
+
return this.listeners.get(event)?.size ?? 0;
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// src/errors.ts
|
|
438
|
+
var UtilsError = class extends Error {
|
|
439
|
+
code;
|
|
440
|
+
constructor(message, code) {
|
|
441
|
+
super(message);
|
|
442
|
+
this.name = "UtilsError";
|
|
443
|
+
this.code = code;
|
|
444
|
+
Error.captureStackTrace(this, this.constructor);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
var PluginAlreadyRegisteredError = class extends UtilsError {
|
|
448
|
+
constructor(name) {
|
|
449
|
+
super(`Plugin "${name}" already registered`, "PLUGIN_ALREADY_REGISTERED");
|
|
450
|
+
this.name = "PluginAlreadyRegisteredError";
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
var PluginNotFoundError = class extends UtilsError {
|
|
454
|
+
constructor(name) {
|
|
455
|
+
super(`Plugin "${name}" not found`, "PLUGIN_NOT_FOUND");
|
|
456
|
+
this.name = "PluginNotFoundError";
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
var PluginDependencyMissingError = class extends UtilsError {
|
|
460
|
+
constructor(plugin, dependency) {
|
|
461
|
+
super(`Plugin "${plugin}" requires "${dependency}" to be registered first`, "PLUGIN_DEPENDENCY_MISSING");
|
|
462
|
+
this.name = "PluginDependencyMissingError";
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
// src/kernel.ts
|
|
467
|
+
var Kernel = class {
|
|
468
|
+
plugins = /* @__PURE__ */ new Map();
|
|
469
|
+
context = {};
|
|
470
|
+
eventBus = new EventBus();
|
|
471
|
+
/**
|
|
472
|
+
* Register a plugin with the kernel.
|
|
473
|
+
*
|
|
474
|
+
* @param plugin - Plugin to register
|
|
475
|
+
* @throws {PluginAlreadyRegisteredError} If plugin name already exists
|
|
476
|
+
* @throws {PluginDependencyMissingError} If plugin dependencies are not met
|
|
477
|
+
*/
|
|
478
|
+
register(plugin) {
|
|
479
|
+
if (this.plugins.has(plugin.name)) {
|
|
480
|
+
throw new PluginAlreadyRegisteredError(plugin.name);
|
|
481
|
+
}
|
|
482
|
+
if (plugin.dependencies) {
|
|
483
|
+
for (const dep of plugin.dependencies) {
|
|
484
|
+
if (!this.plugins.has(dep)) {
|
|
485
|
+
throw new PluginDependencyMissingError(plugin.name, dep);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
this.plugins.set(plugin.name, plugin);
|
|
490
|
+
try {
|
|
491
|
+
plugin.install(this);
|
|
492
|
+
} catch (error) {
|
|
493
|
+
this.plugins.delete(plugin.name);
|
|
494
|
+
throw error;
|
|
495
|
+
}
|
|
496
|
+
this.eventBus.emit("plugin:registered", { name: plugin.name, version: plugin.version });
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Initialize all registered plugins.
|
|
500
|
+
*
|
|
501
|
+
* Calls onInit() for each plugin that has it.
|
|
502
|
+
* Plugins are initialized in the order they were registered.
|
|
503
|
+
*/
|
|
504
|
+
async init() {
|
|
505
|
+
for (const plugin of this.plugins.values()) {
|
|
506
|
+
if (plugin.onInit) {
|
|
507
|
+
try {
|
|
508
|
+
await plugin.onInit(this.context);
|
|
509
|
+
} catch (error) {
|
|
510
|
+
if (plugin.onError) {
|
|
511
|
+
plugin.onError(error);
|
|
512
|
+
}
|
|
513
|
+
throw error;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
this.eventBus.emit("kernel:initialized");
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Unregister a plugin.
|
|
521
|
+
*
|
|
522
|
+
* @param name - Plugin name to unregister
|
|
523
|
+
* @throws {PluginNotFoundError} If plugin doesn't exist
|
|
524
|
+
*/
|
|
525
|
+
unregister(name) {
|
|
526
|
+
const plugin = this.plugins.get(name);
|
|
527
|
+
if (!plugin) {
|
|
528
|
+
throw new PluginNotFoundError(name);
|
|
529
|
+
}
|
|
530
|
+
this.plugins.delete(name);
|
|
531
|
+
if (plugin.onDestroy) {
|
|
532
|
+
plugin.onDestroy();
|
|
533
|
+
}
|
|
534
|
+
this.eventBus.emit("plugin:unregistered", { name });
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Get the shared context object.
|
|
538
|
+
*
|
|
539
|
+
* @returns Shared context
|
|
540
|
+
*/
|
|
541
|
+
getContext() {
|
|
542
|
+
return this.context;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Get a registered plugin by name.
|
|
546
|
+
*
|
|
547
|
+
* @param name - Plugin name
|
|
548
|
+
* @returns Plugin instance or undefined
|
|
549
|
+
*/
|
|
550
|
+
getPlugin(name) {
|
|
551
|
+
return this.plugins.get(name);
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Check if a plugin is registered.
|
|
555
|
+
*
|
|
556
|
+
* @param name - Plugin name
|
|
557
|
+
* @returns True if plugin exists
|
|
558
|
+
*/
|
|
559
|
+
hasPlugin(name) {
|
|
560
|
+
return this.plugins.has(name);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Get list of all registered plugin names.
|
|
564
|
+
*
|
|
565
|
+
* @returns Array of plugin names
|
|
566
|
+
*/
|
|
567
|
+
listPlugins() {
|
|
568
|
+
return Array.from(this.plugins.keys());
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Register an event listener.
|
|
572
|
+
*
|
|
573
|
+
* @param event - Event name
|
|
574
|
+
* @param handler - Event handler function
|
|
575
|
+
*/
|
|
576
|
+
on(event, handler) {
|
|
577
|
+
this.eventBus.on(event, handler);
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Unregister an event listener.
|
|
581
|
+
*
|
|
582
|
+
* @param event - Event name
|
|
583
|
+
* @param handler - Event handler function
|
|
584
|
+
*/
|
|
585
|
+
off(event, handler) {
|
|
586
|
+
this.eventBus.off(event, handler);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Emit an event.
|
|
590
|
+
*
|
|
591
|
+
* @param event - Event name
|
|
592
|
+
* @param args - Event data
|
|
593
|
+
*/
|
|
594
|
+
emit(event, ...args) {
|
|
595
|
+
this.eventBus.emit(event, ...args);
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Destroy the kernel and all plugins.
|
|
599
|
+
*
|
|
600
|
+
* Unregisters all plugins and cleans up resources.
|
|
601
|
+
*/
|
|
602
|
+
async destroy() {
|
|
603
|
+
const pluginNames = Array.from(this.plugins.keys());
|
|
604
|
+
for (const name of pluginNames) {
|
|
605
|
+
this.unregister(name);
|
|
606
|
+
}
|
|
607
|
+
this.eventBus.removeAllListeners();
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
|
|
611
|
+
exports.Kernel = Kernel;
|
|
612
|
+
exports.array = array_exports;
|
|
613
|
+
exports.camelCase = camelCase;
|
|
614
|
+
exports.capitalize = capitalize;
|
|
615
|
+
exports.chunk = chunk;
|
|
616
|
+
exports.compact = compact;
|
|
617
|
+
exports.entries = entries;
|
|
618
|
+
exports.first = first;
|
|
619
|
+
exports.flatten = flatten;
|
|
620
|
+
exports.fromEntries = fromEntries;
|
|
621
|
+
exports.get = get;
|
|
622
|
+
exports.groupBy = groupBy;
|
|
623
|
+
exports.has = has;
|
|
624
|
+
exports.kebabCase = kebabCase;
|
|
625
|
+
exports.keyBy = keyBy;
|
|
626
|
+
exports.keys = keys;
|
|
627
|
+
exports.last = last;
|
|
628
|
+
exports.merge = merge;
|
|
629
|
+
exports.object = object_exports;
|
|
630
|
+
exports.omit = omit;
|
|
631
|
+
exports.pascalCase = pascalCase;
|
|
632
|
+
exports.pick = pick;
|
|
633
|
+
exports.sample = sample;
|
|
634
|
+
exports.set = set;
|
|
635
|
+
exports.setMut = setMut;
|
|
636
|
+
exports.slugify = slugify;
|
|
637
|
+
exports.snakeCase = snakeCase;
|
|
638
|
+
exports.string = string_exports;
|
|
639
|
+
exports.template = template;
|
|
640
|
+
exports.truncate = truncate;
|
|
641
|
+
exports.uniq = uniq;
|
|
642
|
+
exports.uniqBy = uniqBy;
|
|
643
|
+
exports.values = values;
|
|
644
|
+
//# sourceMappingURL=index.cjs.map
|
|
645
|
+
//# sourceMappingURL=index.cjs.map
|