@axi-engine/utils 0.1.8 → 0.1.9

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.
Files changed (56) hide show
  1. package/dist/arrays.d.ts +72 -0
  2. package/dist/arrays.d.ts.map +1 -0
  3. package/dist/arrays.js +115 -0
  4. package/dist/arrays.js.map +1 -0
  5. package/dist/assertion.d.ts +31 -0
  6. package/dist/assertion.d.ts.map +1 -0
  7. package/dist/assertion.js +41 -0
  8. package/dist/assertion.js.map +1 -0
  9. package/dist/config.d.ts +10 -0
  10. package/dist/config.d.ts.map +1 -0
  11. package/dist/config.js +12 -0
  12. package/dist/config.js.map +1 -0
  13. package/dist/constructor-registry.d.ts +40 -0
  14. package/dist/constructor-registry.d.ts.map +1 -0
  15. package/dist/constructor-registry.js +52 -0
  16. package/dist/constructor-registry.js.map +1 -0
  17. package/dist/emitter.d.ts +32 -0
  18. package/dist/emitter.d.ts.map +1 -0
  19. package/dist/emitter.js +43 -0
  20. package/dist/emitter.js.map +1 -0
  21. package/dist/guards.d.ts +12 -0
  22. package/dist/guards.d.ts.map +1 -0
  23. package/dist/guards.js +24 -0
  24. package/dist/guards.js.map +1 -0
  25. package/dist/index.d.mts +72 -84
  26. package/dist/index.d.ts +12 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +12 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/index.mjs +172 -295
  31. package/dist/math.d.ts +17 -0
  32. package/dist/math.d.ts.map +1 -0
  33. package/dist/math.js +26 -0
  34. package/dist/math.js.map +1 -0
  35. package/dist/misc.d.ts +7 -0
  36. package/dist/misc.d.ts.map +1 -0
  37. package/dist/misc.js +9 -0
  38. package/dist/misc.js.map +1 -0
  39. package/dist/path.d.ts +10 -0
  40. package/dist/path.d.ts.map +1 -0
  41. package/dist/path.js +14 -0
  42. package/dist/path.js.map +1 -0
  43. package/dist/random.d.ts +14 -0
  44. package/dist/random.d.ts.map +1 -0
  45. package/dist/random.js +21 -0
  46. package/dist/random.js.map +1 -0
  47. package/dist/types.d.ts +50 -0
  48. package/dist/types.d.ts.map +1 -0
  49. package/dist/types.js +2 -0
  50. package/dist/types.js.map +1 -0
  51. package/package.json +38 -38
  52. package/dist/index.cjs +0 -380
  53. package/dist/index.d.cts +0 -305
  54. package/dist/index.d.cts.map +0 -1
  55. package/dist/index.d.mts.map +0 -1
  56. package/dist/index.mjs.map +0 -1
package/dist/index.d.cts DELETED
@@ -1,305 +0,0 @@
1
- //#region src/types.d.ts
2
- /**
3
- * Represents a path that can be provided as a single string
4
- * or an array of segments.
5
- * @example
6
- * 'player/stats/health'
7
- * ['player', 'stats', 'health']
8
- */
9
- type PathType = string | string[];
10
- /**
11
- * Represents a generic constructor for any class.
12
- *
13
- * This utility type is essential for implementing higher-order patterns
14
- * like mixins, where a function takes a class as an argument and returns
15
- * a new, extended class.
16
- *
17
- * The `...args: any[]` signature allows it to represent constructors
18
- * with any number and type of arguments, making it universally applicable.
19
- *
20
- * @template T - The type of the instance created by the constructor. Defaults to `{}`.
21
- *
22
- * @example
23
- * // Used as a constraint for a base class in a mixin
24
- * function Timestamped<TBase extends Constructor>(Base: TBase) {
25
- * return class extends Base {
26
- * timestamp = new Date();
27
- * };
28
- * }
29
- *
30
- * class User {}
31
- * const TimestampedUser = Timestamped(User);
32
- * const userInstance = new TimestampedUser();
33
- * console.log(userInstance.timestamp); // Logs the current date
34
- */
35
- type Constructor<T = {}> = new (...args: any[]) => T;
36
- /**
37
- * Defines the public, read-only contract for an event emitter.
38
- * It allows subscribing to an event but not emitting it.
39
- * @template T A tuple representing the types of the event arguments.
40
- */
41
- type Subscribable<T extends any[]> = {
42
- readonly listenerCount: number;
43
- /**
44
- * Subscribes a listener to this event.
45
- * @returns A function to unsubscribe the listener.
46
- */
47
- subscribe(listener: (...args: T) => void): () => void;
48
- unsubscribe(listener: (...args: T) => void): boolean;
49
- clear(): void;
50
- };
51
- //#endregion
52
- //#region src/arrays.d.ts
53
- /**
54
- * Generates an array of numbers from 0 to length-1.
55
- * @param length The desired length of the array.
56
- * @returns An array of sequential numbers.
57
- * @example genArray(3); // returns [0, 1, 2]
58
- */
59
- declare function genArray(length: number): number[];
60
- /**
61
- * Creates a new array with its elements shuffled in a random order.
62
- * This function does not mutate the original array.
63
- * @template T The type of elements in the array.
64
- * @param array The array to shuffle.
65
- * @returns A new, shuffled array.
66
- */
67
- declare function shuffleArray<T>(array: T[]): T[];
68
- /**
69
- * Checks if the first array is a sequential starting subset of the second array.
70
- * @param arr1 The potential subset array.
71
- * @param arr2 The array to check against.
72
- * @returns `true` if arr1 is a sequential start of arr2, otherwise `false`.
73
- * @example
74
- * isSequentialStart([1, 2], [1, 2, 3]); // true
75
- * isSequentialStart([1, 3], [1, 2, 3]); // false
76
- */
77
- declare function isSequentialStart<T>(arr1: T[], arr2: T[]): boolean;
78
- /**
79
- * Checks if two arrays contain the same elements, ignoring order.
80
- * Works for arrays of primitives like strings or numbers.
81
- * @template T The type of elements in the array.
82
- * @param arr1 The first array.
83
- * @param arr2 The second array.
84
- * @returns `true` if both arrays contain the same elements, otherwise `false`.
85
- * @example
86
- * haveSameElements(['a', 'b'], ['b', 'a']); // true
87
- * haveSameElements([1, 2, 3], [3, 1, 2]); // true
88
- * haveSameElements(['a', 'b'], ['a', 'c']); // false
89
- */
90
- declare function haveSameElements<T>(arr1?: T[], arr2?: T[]): boolean;
91
- /**
92
- * Checks if two arrays are strictly equal (same elements in the same order).
93
- * @template T The type of elements in the array.
94
- * @param arr1 The first array.
95
- * @param arr2 The second array.
96
- * @returns `true` if the arrays are strictly equal, otherwise `false`.
97
- * @example
98
- * areArraysEqual(['a', 'b'], ['a', 'b']); // true
99
- * areArraysEqual(['a', 'b'], ['b', 'a']); // false
100
- * areArraysEqual([1, 2], [1, 2, 3]); // false
101
- */
102
- declare function areArraysEqual<T>(arr1?: T[], arr2?: T[]): boolean;
103
- /**
104
- * Gets the last element of an array.
105
- * @template T The type of elements in the array.
106
- * @param array The array to query.
107
- * @returns The last element of the array, or `undefined` if the array is empty.
108
- */
109
- declare function last<T>(array: T[]): T | undefined;
110
- /**
111
- * Creates a duplicate-free version of an array.
112
- * @template T The type of elements in the array.
113
- * @param array The array to process.
114
- * @returns A new array with only unique elements.
115
- */
116
- declare function unique<T>(array: T[]): T[];
117
- /**
118
- * Gets a random element from an array.
119
- * @template T The type of elements in the array.
120
- * @param array The array to choose from.
121
- * @returns A random element from the array, or `undefined` if the array is empty.
122
- */
123
- declare function getRandomElement<T>(array: T[]): T | undefined;
124
- //#endregion
125
- //#region src/assertion.d.ts
126
- /**
127
- * Throws an error if the condition is true.
128
- * @param conditionForThrow - If true, an error will be thrown.
129
- * @param exceptionMessage - The message for the error.
130
- * @throws {Error} if the value is true
131
- */
132
- declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
133
- /**
134
- * Throws an error if the value is null, undefined, or an empty array.
135
- *
136
- * @template T The type of the value being checked.
137
- * @param value The value to check.
138
- * @param exceptionMessage The message for the error.
139
- * @throws {Error} if the value is null, undefined, or an empty array.
140
- *
141
- * @example
142
- * // Example with a potentially undefined variable
143
- * const user: { name: string } | undefined = findUser();
144
- * throwIfEmpty(user, 'User not found');
145
- * // From here, TypeScript knows `user` is not undefined.
146
- * console.log(user.name);
147
- *
148
- * @example
149
- * // Example with an array
150
- * const items: string[] = getItems();
151
- * throwIfEmpty(items, 'Items array cannot be empty');
152
- * // From here, you can safely access items[0] without checking for an empty array again.
153
- * console.log('First item:', items[0]);
154
- */
155
- declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
156
- //#endregion
157
- //#region src/config.d.ts
158
- interface AxiEngineConfig {
159
- pathSeparator: string;
160
- }
161
- declare const axiSettings: AxiEngineConfig;
162
- /**
163
- * set up global configuration for axi-engine.
164
- * @param newConfig - configuration object
165
- */
166
- declare function configure(newConfig: Partial<AxiEngineConfig>): void;
167
- //#endregion
168
- //#region src/constructor-registry.d.ts
169
- /**
170
- * A generic registry for mapping string identifiers to class constructors.
171
- *
172
- * This utility is fundamental for building extensible systems like dependency injection containers,
173
- * factories, and serialization engines where types need to be dynamically resolved.
174
- *
175
- * @template T - A base type that all registered constructors must produce an instance of.
176
- */
177
- declare class ConstructorRegistry<T> {
178
- private readonly items;
179
- /**
180
- * Registers a constructor with a unique string identifier.
181
- *
182
- * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
183
- * @param ctor - The class constructor to register.
184
- * @returns The registry instance for chainable calls.
185
- * @throws If a constructor with the same `typeId` is already registered.
186
- */
187
- register(typeId: string, ctor: Constructor<T>): this;
188
- /**
189
- * Retrieves a constructor by its identifier.
190
- *
191
- * @param typeId - The identifier of the constructor to retrieve.
192
- * @returns The found class constructor.
193
- * @throws If no constructor is found for the given `typeId`.
194
- */
195
- get(typeId: string): Constructor<T>;
196
- /**
197
- * Checks if a constructor for a given identifier is registered.
198
- * @param typeId - The identifier to check.
199
- * @returns `true` if a constructor is registered, otherwise `false`.
200
- */
201
- has(typeId: string): boolean;
202
- /**
203
- * Clears all registered constructors from the registry.
204
- */
205
- clear(): void;
206
- }
207
- //#endregion
208
- //#region src/emitter.d.ts
209
- /**
210
- * A minimal, type-safe event emitter for a single event.
211
- * It does not manage state, it only manages subscribers and event dispatching.
212
- * @template T A tuple representing the types of the event arguments.
213
- */
214
- declare class Emitter<T extends any[]> implements Subscribable<T> {
215
- private listeners;
216
- /**
217
- * Returns the number of listeners.
218
- */
219
- get listenerCount(): number;
220
- /**
221
- * Subscribes a listener to this event.
222
- * @returns A function to unsubscribe the listener.
223
- */
224
- subscribe(listener: (...args: T) => void): () => void;
225
- /**
226
- * Manually unsubscribe by listener
227
- * @returns returns true if an listener has been removed, or false if the listener does not exist.
228
- */
229
- unsubscribe(listener: (...args: T) => void): boolean;
230
- /**
231
- * Dispatches the event to all subscribed listeners.
232
- */
233
- emit(...args: T): void;
234
- /**
235
- * Clears all listeners.
236
- */
237
- clear(): void;
238
- }
239
- //#endregion
240
- //#region src/guards.d.ts
241
- declare function isNullOrUndefined(val: unknown): val is null | undefined;
242
- declare function isUndefined(val: unknown): val is undefined;
243
- declare function isNumber(val: unknown): val is number;
244
- declare function isBoolean(val: unknown): val is boolean;
245
- declare function isString(val: unknown): val is string;
246
- /**
247
- * Type guard to check if a value is a string that ends with '%'.
248
- * @param val The value to check.
249
- * @returns `true` if the value is a percentage string.
250
- */
251
- declare function isPercentageString(val: unknown): val is string;
252
- //#endregion
253
- //#region src/math.d.ts
254
- /**
255
- * Clamps a number between an optional minimum and maximum value.
256
- * @param val The number to clamp.
257
- * @param min The minimum value. If null or undefined, it's ignored.
258
- * @param max The maximum value. If null or undefined, it's ignored.
259
- * @returns The clamped number.
260
- */
261
- declare function clampNumber(val: number, min?: number | null, max?: number | null): number;
262
- /**
263
- * Calculates a percentage of a given value.
264
- * @param val The base value.
265
- * @param percents The percentage to get.
266
- * @returns The calculated percentage of the value.
267
- * @example getPercentOf(200, 10); // returns 20
268
- */
269
- declare function getPercentOf(val: number, percents: number): number;
270
- //#endregion
271
- //#region src/misc.d.ts
272
- /**
273
- * Returns the first key of an object.
274
- * @param obj The object from which to get the key.
275
- * @returns The first key of the object as a string.
276
- */
277
- declare function firstKeyOf(obj: any): string;
278
- //#endregion
279
- //#region src/path.d.ts
280
- /**
281
- * Ensures that the given path is returned as an array of segments.
282
- */
283
- declare function ensurePathArray(path: PathType, separator?: string): string[];
284
- /**
285
- * Ensures that the given path is returned as a single string.
286
- */
287
- declare function ensurePathString(path: PathType, separator?: string): string;
288
- //#endregion
289
- //#region src/random.d.ts
290
- /**
291
- * Returns a random integer between min (inclusive) and max (exclusive).
292
- * @param min The minimum integer (inclusive).
293
- * @param max The maximum integer (exclusive).
294
- * @returns A random integer.
295
- * @example randInt(1, 5); // returns 1, 2, 3, or 4
296
- */
297
- declare function randInt(min: number, max: number): number;
298
- /**
299
- * Generates a unique identifier using uuidv4.
300
- * @returns A unique string ID.
301
- */
302
- declare function randId(): string;
303
- //#endregion
304
- export { AxiEngineConfig, Constructor, ConstructorRegistry, Emitter, PathType, Subscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isNullOrUndefined, isNumber, isPercentageString, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwIf, throwIfEmpty, unique };
305
- //# sourceMappingURL=index.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/arrays.ts","../src/assertion.ts","../src/config.ts","../src/constructor-registry.ts","../src/emitter.ts","../src/guards.ts","../src/math.ts","../src/misc.ts","../src/path.ts","../src/random.ts"],"mappings":";;AAOA;AA2BA;AAOA;;;;KAlCY,QAAA;AAAA;AA2BZ;AAOA;;;;ACnCA;AAWA;AAkBA;AAmBA;AAuBA;AAcA;AAUA;AAUA;;;;ACvGA;AA4BA;;;;;;;AF7BY,KA2BA,WAAA,mBAAA,IAAA,YAA8C,CAAA;AAAA;AAO1D;;;;AAP0D,KAO9C,YAAA;EAAA,SAAA,aAAA;EAAA;;;;EAAA,SAAA,CAAA,QAAA,MAAA,IAAA,EAOoB,CAAA;EAAA,WAAA,CAAA,QAAA,MAAA,IAAA,EAEE,CAAA;EAAA,KAAA;AAAA;;;;AC5ClC;AAWA;AAkBA;AAmBA;AAuBA;iBAvEgB,QAAA,CAAA,MAAA;AAAA;AAWhB;AAkBA;AAmBA;AAuBA;AAcA;AAUA;AA/FgB,iBAWA,YAAA,GAAA,CAAA,KAAA,EAAuB,CAAA,KAAM,CAAA;AAAA;AAkB7C;AAmBA;AAuBA;AAcA;AAUA;AAUA;;;AA9F6C,iBAkB7B,iBAAA,GAAA,CAAA,IAAA,EAA2B,CAAA,IAAA,IAAA,EAAW,CAAA;AAAA;AAmBtD;AAuBA;AAcA;AAUA;AAUA;;;;ACvGA;AA4BA;;ADDsD,iBAmBtC,gBAAA,GAAA,CAAA,IAAA,GAA2B,CAA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;;;EAAA,KAAA,GAAA,IAAA,EAOlB,CAAA;EAAA;;;EAAA,MAAA;AAAA;;;iBCvCA,iBAAA,CAAA,GAAA,YAAA,GAAA;AAAA,iBAIA,WAAA,CAAA,GAAA,YAAA,GAAA;AAAA,iBAIA,QAAA,CAAA,GAAA,YAAA,GAAA;AAAA,iBAIA,SAAA,CAAA,GAAA,YAAA,GAAA;AAAA,iBAIA,QAAA,CAAA,GAAA,YAAA,GAAA;AAAA;AAShB;;;;AATgB,iBASA,kBAAA,CAAA,GAAA,YAAA,GAAA;;;;ACfhB;AAaA;;;;AClBA;iBDKgB,WAAA,CAAA,GAAA,UAAA,GAAA,kBAAA,GAAA;AAAA;AAahB;;;;AClBA;;ADKgB,iBAaA,YAAA,CAAA,GAAA,UAAA,QAAA;;;;AClBhB;;;;iBAAgB,UAAA,CAAA,GAAA;;;;ACChB;AAOA;iBAPgB,eAAA,CAAA,IAAA,EAAsB,QAAA,EAAA,SAAA;AAAA;AAOtC;;AAPsC,iBAOtB,gBAAA,CAAA,IAAA,EAAuB,QAAA,EAAA,SAAA;;;;ACJvC;AAUA;;;;;iBAVgB,OAAA,CAAA,GAAA,UAAA,GAAA;AAAA;AAUhB;;;AAVgB,iBAUA,MAAA,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/types.ts","../src/arrays.ts","../src/assertion.ts","../src/config.ts","../src/constructor-registry.ts","../src/emitter.ts","../src/guards.ts","../src/math.ts","../src/misc.ts","../src/path.ts","../src/random.ts"],"mappings":";;AAOA;AA2BA;AAOA;;;;KAlCY,QAAA;AAAA;AA2BZ;AAOA;;;;ACnCA;AAWA;AAkBA;AAmBA;AAuBA;AAcA;AAUA;AAUA;;;;ACvGA;AA4BA;;;;;;;AF7BY,KA2BA,WAAA,mBAAA,IAAA,YAA8C,CAAA;AAAA;AAO1D;;;;AAP0D,KAO9C,YAAA;EAAA,SAAA,aAAA;EAAA;;;;EAAA,SAAA,CAAA,QAAA,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AAA,GAAA;AAAA;AAUhB;;;AAVgB,iBAUA,MAAA,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","names":["uuidv4"],"sources":["../src/arrays.ts","../src/guards.ts","../src/assertion.ts","../src/config.ts","../src/constructor-registry.ts","../src/emitter.ts","../src/math.ts","../src/misc.ts","../src/path.ts","../src/random.ts"],"sourcesContent":["/**\r\n * Generates an array of numbers from 0 to length-1.\r\n * @param length The desired length of the array.\r\n * @returns An array of sequential numbers.\r\n * @example genArray(3); // returns [0, 1, 2]\r\n */\r\nexport function genArray(length: number) {\r\n return Array.from({length}, (_v, i) => i);\r\n}\r\n\r\n/**\r\n * Creates a new array with its elements shuffled in a random order.\r\n * This function does not mutate the original array.\r\n * @template T The type of elements in the array.\r\n * @param array The array to shuffle.\r\n * @returns A new, shuffled array.\r\n */\r\nexport function shuffleArray<T>(array: T[]): T[] {\r\n const result = [...array];\r\n for (let i = result.length - 1; i > 0; i--) {\r\n const j = Math.floor(Math.random() * (i + 1));\r\n [result[i], result[j]] = [result[j], result[i]];\r\n }\r\n return result;\r\n}\r\n\r\n/**\r\n * Checks if the first array is a sequential starting subset of the second array.\r\n * @param arr1 The potential subset array.\r\n * @param arr2 The array to check against.\r\n * @returns `true` if arr1 is a sequential start of arr2, otherwise `false`.\r\n * @example\r\n * isSequentialStart([1, 2], [1, 2, 3]); // true\r\n * isSequentialStart([1, 3], [1, 2, 3]); // false\r\n */\r\nexport function isSequentialStart<T>(arr1: T[], arr2: T[]): boolean {\r\n if (arr1.length > arr2.length) {\r\n return false;\r\n }\r\n return arr1.every((element, index) => element === arr2[index]);\r\n}\r\n\r\n/**\r\n * Checks if two arrays contain the same elements, ignoring order.\r\n * Works for arrays of primitives like strings or numbers.\r\n * @template T The type of elements in the array.\r\n * @param arr1 The first array.\r\n * @param arr2 The second array.\r\n * @returns `true` if both arrays contain the same elements, otherwise `false`.\r\n * @example\r\n * haveSameElements(['a', 'b'], ['b', 'a']); // true\r\n * haveSameElements([1, 2, 3], [3, 1, 2]); // true\r\n * haveSameElements(['a', 'b'], ['a', 'c']); // false\r\n */\r\nexport function haveSameElements<T>(arr1?: T[], arr2?: T[]): boolean {\r\n if (!arr1 && !arr2) return true;\r\n if (!arr1 || !arr2) return false;\r\n if (arr1.length !== arr2.length) return false;\r\n\r\n // Create sorted copies to compare\r\n const sortedArr1 = [...arr1].sort();\r\n const sortedArr2 = [...arr2].sort();\r\n\r\n return sortedArr1.every((value, index) => value === sortedArr2[index]);\r\n}\r\n\r\n/**\r\n * Checks if two arrays are strictly equal (same elements in the same order).\r\n * @template T The type of elements in the array.\r\n * @param arr1 The first array.\r\n * @param arr2 The second array.\r\n * @returns `true` if the arrays are strictly equal, otherwise `false`.\r\n * @example\r\n * areArraysEqual(['a', 'b'], ['a', 'b']); // true\r\n * areArraysEqual(['a', 'b'], ['b', 'a']); // false\r\n * areArraysEqual([1, 2], [1, 2, 3]); // false\r\n */\r\nexport function areArraysEqual<T>(arr1?: T[], arr2?: T[]): boolean {\r\n if (!arr1 && !arr2) return true;\r\n if (!arr1 || !arr2) return false;\r\n if (arr1.length !== arr2.length) return false;\r\n\r\n return arr1.every((value, index) => value === arr2[index]);\r\n}\r\n\r\n/**\r\n * Gets the last element of an array.\r\n * @template T The type of elements in the array.\r\n * @param array The array to query.\r\n * @returns The last element of the array, or `undefined` if the array is empty.\r\n */\r\nexport function last<T>(array: T[]): T | undefined {\r\n return array[array.length - 1];\r\n}\r\n\r\n/**\r\n * Creates a duplicate-free version of an array.\r\n * @template T The type of elements in the array.\r\n * @param array The array to process.\r\n * @returns A new array with only unique elements.\r\n */\r\nexport function unique<T>(array: T[]): T[] {\r\n return [...new Set(array)];\r\n}\r\n\r\n/**\r\n * Gets a random element from an array.\r\n * @template T The type of elements in the array.\r\n * @param array The array to choose from.\r\n * @returns A random element from the array, or `undefined` if the array is empty.\r\n */\r\nexport function getRandomElement<T>(array: T[]): T | undefined {\r\n if (array.length === 0) {\r\n return undefined;\r\n }\r\n const index = Math.floor(Math.random() * array.length);\r\n return array[index];\r\n}\r\n","export function isNullOrUndefined(val: unknown): val is null | undefined {\r\n return val === undefined || val === null;\r\n}\r\n\r\nexport function isUndefined(val: unknown): val is undefined {\r\n return typeof val === 'undefined';\r\n}\r\n\r\nexport function isNumber(val: unknown): val is number {\r\n return typeof val === \"number\";\r\n}\r\n\r\nexport function isBoolean(val: unknown): val is boolean {\r\n return typeof val === \"boolean\";\r\n}\r\n\r\nexport function isString(val: unknown): val is string {\r\n return typeof val === \"string\";\r\n}\r\n\r\n/**\r\n * Type guard to check if a value is a string that ends with '%'.\r\n * @param val The value to check.\r\n * @returns `true` if the value is a percentage string.\r\n */\r\nexport function isPercentageString(val: unknown): val is string {\r\n return typeof val === \"string\" && val.endsWith(\"%\");\r\n}\r\n","import {isNullOrUndefined} from './guards';\r\n\r\n/**\r\n * Throws an error if the condition is true.\r\n * @param conditionForThrow - If true, an error will be thrown.\r\n * @param exceptionMessage - The message for the error.\r\n * @throws {Error} if the value is true\r\n */\r\nexport function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never {\r\n if (conditionForThrow) {\r\n throw new Error(exceptionMessage);\r\n }\r\n}\r\n\r\n/**\r\n * Throws an error if the value is null, undefined, or an empty array.\r\n *\r\n * @template T The type of the value being checked.\r\n * @param value The value to check.\r\n * @param exceptionMessage The message for the error.\r\n * @throws {Error} if the value is null, undefined, or an empty array.\r\n *\r\n * @example\r\n * // Example with a potentially undefined variable\r\n * const user: { name: string } | undefined = findUser();\r\n * throwIfEmpty(user, 'User not found');\r\n * // From here, TypeScript knows `user` is not undefined.\r\n * console.log(user.name);\r\n *\r\n * @example\r\n * // Example with an array\r\n * const items: string[] = getItems();\r\n * throwIfEmpty(items, 'Items array cannot be empty');\r\n * // From here, you can safely access items[0] without checking for an empty array again.\r\n * console.log('First item:', items[0]);\r\n */\r\nexport function throwIfEmpty<T>(\r\n value: T,\r\n exceptionMessage: string\r\n): asserts value is NonNullable<T> {\r\n const isArrayAndEmpty = Array.isArray(value) && value.length === 0;\r\n\r\n if (isNullOrUndefined(value) || isArrayAndEmpty) {\r\n throw new Error(exceptionMessage);\r\n }\r\n}\r\n","export interface AxiEngineConfig {\r\n pathSeparator: string;\r\n\r\n /** logging and debugging logic in future */\r\n // logLevel: 'debug' | 'info' | 'warn' | 'error';\r\n // defaultLocale: string;\r\n}\r\n\r\nconst defaultConfig: AxiEngineConfig = {\r\n pathSeparator: '/'\r\n};\r\n\r\nexport const axiSettings: AxiEngineConfig = { ...defaultConfig };\r\n\r\n/**\r\n * set up global configuration for axi-engine.\r\n * @param newConfig - configuration object\r\n */\r\nexport function configure(newConfig: Partial<AxiEngineConfig>): void {\r\n Object.assign(axiSettings, newConfig);\r\n}\r\n","import {Constructor, throwIf, throwIfEmpty} from '@axi-engine/utils';\r\n\r\n/**\r\n * A generic registry for mapping string identifiers to class constructors.\r\n *\r\n * This utility is fundamental for building extensible systems like dependency injection containers,\r\n * factories, and serialization engines where types need to be dynamically resolved.\r\n *\r\n * @template T - A base type that all registered constructors must produce an instance of.\r\n */\r\nexport class ConstructorRegistry<T> {\r\n private readonly items = new Map<string, Constructor<T>>();\r\n\r\n /**\r\n * Registers a constructor with a unique string identifier.\r\n *\r\n * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).\r\n * @param ctor - The class constructor to register.\r\n * @returns The registry instance for chainable calls.\r\n * @throws If a constructor with the same `typeId` is already registered.\r\n */\r\n register(typeId: string, ctor: Constructor<T>): this {\r\n throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);\r\n this.items.set(typeId, ctor);\r\n return this;\r\n }\r\n\r\n /**\r\n * Retrieves a constructor by its identifier.\r\n *\r\n * @param typeId - The identifier of the constructor to retrieve.\r\n * @returns The found class constructor.\r\n * @throws If no constructor is found for the given `typeId`.\r\n */\r\n get(typeId: string): Constructor<T> {\r\n const Ctor = this.items.get(typeId);\r\n throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);\r\n return Ctor!;\r\n }\r\n\r\n /**\r\n * Checks if a constructor for a given identifier is registered.\r\n * @param typeId - The identifier to check.\r\n * @returns `true` if a constructor is registered, otherwise `false`.\r\n */\r\n has(typeId: string): boolean {\r\n return this.items.has(typeId);\r\n }\r\n\r\n /**\r\n * Clears all registered constructors from the registry.\r\n */\r\n clear(): void {\r\n this.items.clear();\r\n }\r\n}\r\n","// file: packages/utils/src/emitter.ts\r\n\r\nimport {Subscribable} from './types';\r\n\r\n/**\r\n * A minimal, type-safe event emitter for a single event.\r\n * It does not manage state, it only manages subscribers and event dispatching.\r\n * @template T A tuple representing the types of the event arguments.\r\n */\r\nexport class Emitter<T extends any[]> implements Subscribable<T>{\r\n private listeners: Set<(...args: T) => void> = new Set();\r\n\r\n /**\r\n * Returns the number of listeners.\r\n */\r\n get listenerCount(): number {\r\n return this.listeners.size;\r\n }\r\n\r\n /**\r\n * Subscribes a listener to this event.\r\n * @returns A function to unsubscribe the listener.\r\n */\r\n subscribe(listener: (...args: T) => void): () => void {\r\n this.listeners.add(listener);\r\n return () => this.listeners.delete(listener);\r\n }\r\n\r\n /**\r\n * Manually unsubscribe by listener\r\n * @returns returns true if an listener has been removed, or false if the listener does not exist.\r\n */\r\n unsubscribe(listener: (...args: T) => void) {\r\n return this.listeners.delete(listener);\r\n }\r\n\r\n /**\r\n * Dispatches the event to all subscribed listeners.\r\n */\r\n emit(...args: T): void {\r\n this.listeners.forEach(listener => listener(...args));\r\n }\r\n\r\n /**\r\n * Clears all listeners.\r\n */\r\n clear(): void {\r\n this.listeners.clear();\r\n }\r\n}\r\n","import {isNullOrUndefined} from './guards';\r\n\r\n\r\n/**\r\n * Clamps a number between an optional minimum and maximum value.\r\n * @param val The number to clamp.\r\n * @param min The minimum value. If null or undefined, it's ignored.\r\n * @param max The maximum value. If null or undefined, it's ignored.\r\n * @returns The clamped number.\r\n */\r\nexport function clampNumber(val: number, min?: number | null, max?: number | null): number {\r\n if (!isNullOrUndefined(min)) val = Math.max(val, min);\r\n if (!isNullOrUndefined(max)) val = Math.min(val, max);\r\n return val;\r\n}\r\n\r\n/**\r\n * Calculates a percentage of a given value.\r\n * @param val The base value.\r\n * @param percents The percentage to get.\r\n * @returns The calculated percentage of the value.\r\n * @example getPercentOf(200, 10); // returns 20\r\n */\r\nexport function getPercentOf(val: number, percents: number) {\r\n return (percents / 100) * val;\r\n}\r\n","/**\r\n * Returns the first key of an object.\r\n * @param obj The object from which to get the key.\r\n * @returns The first key of the object as a string.\r\n */\r\nexport function firstKeyOf(obj: any) {\r\n return Object.keys(obj)[0];\r\n}\r\n","import {PathType} from \"./types\";\r\nimport {axiSettings} from './config';\r\n\r\n/**\r\n * Ensures that the given path is returned as an array of segments.\r\n */\r\nexport function ensurePathArray(path: PathType, separator = axiSettings.pathSeparator): string[] {\r\n return Array.isArray(path) ? [...path] : path.split(separator);\r\n}\r\n\r\n/**\r\n * Ensures that the given path is returned as a single string.\r\n */\r\nexport function ensurePathString(path: PathType, separator = axiSettings.pathSeparator): string {\r\n return !Array.isArray(path) ? path : path.join(separator);\r\n}\r\n","import { v4 as uuidv4 } from 'uuid';\r\n\r\n/**\r\n * Returns a random integer between min (inclusive) and max (exclusive).\r\n * @param min The minimum integer (inclusive).\r\n * @param max The maximum integer (exclusive).\r\n * @returns A random integer.\r\n * @example randInt(1, 5); // returns 1, 2, 3, or 4\r\n */\r\nexport function randInt(min: number, max: number): number {\r\n min = Math.ceil(min);\r\n max = Math.floor(max);\r\n return Math.floor(Math.random() * (max - min) + min);\r\n}\r\n\r\n/**\r\n * Generates a unique identifier using uuidv4.\r\n * @returns A unique string ID.\r\n */\r\nexport function randId() {\r\n return uuidv4();\r\n}\r\n"],"mappings":";;;;;;;;;AAMA,SAAgB,SAAS,QAAgB;AACvC,QAAO,MAAM,KAAK,EAAC,QAAO,GAAG,IAAI,MAAM,EAAE;;;;;;;;;AAU3C,SAAgB,aAAgB,OAAiB;CAC/C,MAAM,SAAS,CAAC,GAAG,MAAM;AACzB,MAAK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