@axi-engine/utils 0.2.0 → 0.2.2

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 (51) hide show
  1. package/dist/index.d.mts +35 -1
  2. package/dist/index.d.ts +327 -12
  3. package/dist/index.js +296 -12
  4. package/dist/index.mjs +9 -0
  5. package/package.json +1 -1
  6. package/dist/arrays.d.ts +0 -72
  7. package/dist/arrays.d.ts.map +0 -1
  8. package/dist/arrays.js +0 -115
  9. package/dist/arrays.js.map +0 -1
  10. package/dist/assertion.d.ts +0 -31
  11. package/dist/assertion.d.ts.map +0 -1
  12. package/dist/assertion.js +0 -41
  13. package/dist/assertion.js.map +0 -1
  14. package/dist/config.d.ts +0 -10
  15. package/dist/config.d.ts.map +0 -1
  16. package/dist/config.js +0 -12
  17. package/dist/config.js.map +0 -1
  18. package/dist/constructor-registry.d.ts +0 -40
  19. package/dist/constructor-registry.d.ts.map +0 -1
  20. package/dist/constructor-registry.js +0 -52
  21. package/dist/constructor-registry.js.map +0 -1
  22. package/dist/emitter.d.ts +0 -32
  23. package/dist/emitter.d.ts.map +0 -1
  24. package/dist/emitter.js +0 -43
  25. package/dist/emitter.js.map +0 -1
  26. package/dist/guards.d.ts +0 -12
  27. package/dist/guards.d.ts.map +0 -1
  28. package/dist/guards.js +0 -24
  29. package/dist/guards.js.map +0 -1
  30. package/dist/index.d.ts.map +0 -1
  31. package/dist/index.js.map +0 -1
  32. package/dist/math.d.ts +0 -17
  33. package/dist/math.d.ts.map +0 -1
  34. package/dist/math.js +0 -26
  35. package/dist/math.js.map +0 -1
  36. package/dist/misc.d.ts +0 -7
  37. package/dist/misc.d.ts.map +0 -1
  38. package/dist/misc.js +0 -9
  39. package/dist/misc.js.map +0 -1
  40. package/dist/path.d.ts +0 -10
  41. package/dist/path.d.ts.map +0 -1
  42. package/dist/path.js +0 -14
  43. package/dist/path.js.map +0 -1
  44. package/dist/random.d.ts +0 -14
  45. package/dist/random.d.ts.map +0 -1
  46. package/dist/random.js +0 -21
  47. package/dist/random.js.map +0 -1
  48. package/dist/types.d.ts +0 -50
  49. package/dist/types.d.ts.map +0 -1
  50. package/dist/types.js +0 -2
  51. package/dist/types.js.map +0 -1
package/dist/index.d.mts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Simple scalar type
3
+ *
4
+ */
5
+ type ScalarType = string | number | boolean;
1
6
  /**
2
7
  * Represents a path that can be provided as a single string
3
8
  * or an array of segments.
@@ -200,6 +205,28 @@ declare class ConstructorRegistry<T> {
200
205
  clear(): void;
201
206
  }
202
207
 
208
+ /**
209
+ * A read-only contract for any system that can provide data by path.
210
+ */
211
+ interface DataSource {
212
+ get(path: PathType): unknown;
213
+ has(path: PathType): boolean;
214
+ }
215
+ /**
216
+ * A write-only contract for any system that can accept data by path.
217
+ */
218
+ interface DataSink {
219
+ set(path: PathType, value: unknown): void;
220
+ create(path: PathType, value: unknown): void;
221
+ delete(path: PathType): void;
222
+ }
223
+ /**
224
+ * A full CRUD contract for systems that provide complete data management.
225
+ * Combines both reading and writing capabilities.
226
+ */
227
+ interface DataStorage extends DataSource, DataSink {
228
+ }
229
+
203
230
  /**
204
231
  * A minimal, type-safe event emitter for a single event.
205
232
  * It does not manage state, it only manages subscribers and event dispatching.
@@ -231,11 +258,18 @@ declare class Emitter<T extends any[]> implements Subscribable<T> {
231
258
  clear(): void;
232
259
  }
233
260
 
261
+ /**
262
+ * Type guard that checks if a value is a valid ScalarType.
263
+ * @param value The value to check.
264
+ * @returns True if the value is a string, number, or boolean.
265
+ */
266
+ declare function isScalar(value: unknown): value is ScalarType;
234
267
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
235
268
  declare function isUndefined(val: unknown): val is undefined;
236
269
  declare function isNumber(val: unknown): val is number;
237
270
  declare function isBoolean(val: unknown): val is boolean;
238
271
  declare function isString(val: unknown): val is string;
272
+ declare function isNull(val: unknown): val is null;
239
273
  /**
240
274
  * Type guard to check if a value is a string that ends with '%'.
241
275
  * @param val The value to check.
@@ -290,4 +324,4 @@ declare function randInt(min: number, max: number): number;
290
324
  */
291
325
  declare function randId(): string;
292
326
 
293
- export { type AxiEngineConfig, type Constructor, ConstructorRegistry, Emitter, type PathType, type 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 };
327
+ export { type AxiEngineConfig, type Constructor, ConstructorRegistry, type DataSink, type DataSource, type DataStorage, Emitter, type PathType, type ScalarType, type Subscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isNull, isNullOrUndefined, isNumber, isPercentageString, isScalar, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwIf, throwIfEmpty, unique };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,327 @@
1
- export type * from './types';
2
- export * from './arrays';
3
- export * from './assertion';
4
- export * from './config';
5
- export * from './constructor-registry';
6
- export * from './emitter';
7
- export * from './guards';
8
- export * from './math';
9
- export * from './misc';
10
- export * from './path';
11
- export * from './random';
12
- //# sourceMappingURL=index.d.ts.map
1
+ /**
2
+ * Simple scalar type
3
+ *
4
+ */
5
+ type ScalarType = string | number | boolean;
6
+ /**
7
+ * Represents a path that can be provided as a single string
8
+ * or an array of segments.
9
+ * @example
10
+ * 'player/stats/health'
11
+ * ['player', 'stats', 'health']
12
+ */
13
+ type PathType = string | string[];
14
+ /**
15
+ * Represents a generic constructor for any class.
16
+ *
17
+ * This utility type is essential for implementing higher-order patterns
18
+ * like mixins, where a function takes a class as an argument and returns
19
+ * a new, extended class.
20
+ *
21
+ * The `...args: any[]` signature allows it to represent constructors
22
+ * with any number and type of arguments, making it universally applicable.
23
+ *
24
+ * @template T - The type of the instance created by the constructor. Defaults to `{}`.
25
+ *
26
+ * @example
27
+ * // Used as a constraint for a base class in a mixin
28
+ * function Timestamped<TBase extends Constructor>(Base: TBase) {
29
+ * return class extends Base {
30
+ * timestamp = new Date();
31
+ * };
32
+ * }
33
+ *
34
+ * class User {}
35
+ * const TimestampedUser = Timestamped(User);
36
+ * const userInstance = new TimestampedUser();
37
+ * console.log(userInstance.timestamp); // Logs the current date
38
+ */
39
+ type Constructor<T = {}> = new (...args: any[]) => T;
40
+ /**
41
+ * Defines the public, read-only contract for an event emitter.
42
+ * It allows subscribing to an event but not emitting it.
43
+ * @template T A tuple representing the types of the event arguments.
44
+ */
45
+ type Subscribable<T extends any[]> = {
46
+ readonly listenerCount: number;
47
+ /**
48
+ * Subscribes a listener to this event.
49
+ * @returns A function to unsubscribe the listener.
50
+ */
51
+ subscribe(listener: (...args: T) => void): () => void;
52
+ unsubscribe(listener: (...args: T) => void): boolean;
53
+ clear(): void;
54
+ };
55
+
56
+ /**
57
+ * Generates an array of numbers from 0 to length-1.
58
+ * @param length The desired length of the array.
59
+ * @returns An array of sequential numbers.
60
+ * @example genArray(3); // returns [0, 1, 2]
61
+ */
62
+ declare function genArray(length: number): number[];
63
+ /**
64
+ * Creates a new array with its elements shuffled in a random order.
65
+ * This function does not mutate the original array.
66
+ * @template T The type of elements in the array.
67
+ * @param array The array to shuffle.
68
+ * @returns A new, shuffled array.
69
+ */
70
+ declare function shuffleArray<T>(array: T[]): T[];
71
+ /**
72
+ * Checks if the first array is a sequential starting subset of the second array.
73
+ * @param arr1 The potential subset array.
74
+ * @param arr2 The array to check against.
75
+ * @returns `true` if arr1 is a sequential start of arr2, otherwise `false`.
76
+ * @example
77
+ * isSequentialStart([1, 2], [1, 2, 3]); // true
78
+ * isSequentialStart([1, 3], [1, 2, 3]); // false
79
+ */
80
+ declare function isSequentialStart<T>(arr1: T[], arr2: T[]): boolean;
81
+ /**
82
+ * Checks if two arrays contain the same elements, ignoring order.
83
+ * Works for arrays of primitives like strings or numbers.
84
+ * @template T The type of elements in the array.
85
+ * @param arr1 The first array.
86
+ * @param arr2 The second array.
87
+ * @returns `true` if both arrays contain the same elements, otherwise `false`.
88
+ * @example
89
+ * haveSameElements(['a', 'b'], ['b', 'a']); // true
90
+ * haveSameElements([1, 2, 3], [3, 1, 2]); // true
91
+ * haveSameElements(['a', 'b'], ['a', 'c']); // false
92
+ */
93
+ declare function haveSameElements<T>(arr1?: T[], arr2?: T[]): boolean;
94
+ /**
95
+ * Checks if two arrays are strictly equal (same elements in the same order).
96
+ * @template T The type of elements in the array.
97
+ * @param arr1 The first array.
98
+ * @param arr2 The second array.
99
+ * @returns `true` if the arrays are strictly equal, otherwise `false`.
100
+ * @example
101
+ * areArraysEqual(['a', 'b'], ['a', 'b']); // true
102
+ * areArraysEqual(['a', 'b'], ['b', 'a']); // false
103
+ * areArraysEqual([1, 2], [1, 2, 3]); // false
104
+ */
105
+ declare function areArraysEqual<T>(arr1?: T[], arr2?: T[]): boolean;
106
+ /**
107
+ * Gets the last element of an array.
108
+ * @template T The type of elements in the array.
109
+ * @param array The array to query.
110
+ * @returns The last element of the array, or `undefined` if the array is empty.
111
+ */
112
+ declare function last<T>(array: T[]): T | undefined;
113
+ /**
114
+ * Creates a duplicate-free version of an array.
115
+ * @template T The type of elements in the array.
116
+ * @param array The array to process.
117
+ * @returns A new array with only unique elements.
118
+ */
119
+ declare function unique<T>(array: T[]): T[];
120
+ /**
121
+ * Gets a random element from an array.
122
+ * @template T The type of elements in the array.
123
+ * @param array The array to choose from.
124
+ * @returns A random element from the array, or `undefined` if the array is empty.
125
+ */
126
+ declare function getRandomElement<T>(array: T[]): T | undefined;
127
+
128
+ /**
129
+ * Throws an error if the condition is true.
130
+ * @param conditionForThrow - If true, an error will be thrown.
131
+ * @param exceptionMessage - The message for the error.
132
+ * @throws {Error} if the value is true
133
+ */
134
+ declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
135
+ /**
136
+ * Throws an error if the value is null, undefined, or an empty array.
137
+ *
138
+ * @template T The type of the value being checked.
139
+ * @param value The value to check.
140
+ * @param exceptionMessage The message for the error.
141
+ * @throws {Error} if the value is null, undefined, or an empty array.
142
+ *
143
+ * @example
144
+ * // Example with a potentially undefined variable
145
+ * const user: { name: string } | undefined = findUser();
146
+ * throwIfEmpty(user, 'User not found');
147
+ * // From here, TypeScript knows `user` is not undefined.
148
+ * console.log(user.name);
149
+ *
150
+ * @example
151
+ * // Example with an array
152
+ * const items: string[] = getItems();
153
+ * throwIfEmpty(items, 'Items array cannot be empty');
154
+ * // From here, you can safely access items[0] without checking for an empty array again.
155
+ * console.log('First item:', items[0]);
156
+ */
157
+ declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
158
+
159
+ interface AxiEngineConfig {
160
+ pathSeparator: string;
161
+ }
162
+ declare const axiSettings: AxiEngineConfig;
163
+ /**
164
+ * set up global configuration for axi-engine.
165
+ * @param newConfig - configuration object
166
+ */
167
+ declare function configure(newConfig: Partial<AxiEngineConfig>): void;
168
+
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
+
208
+ /**
209
+ * A read-only contract for any system that can provide data by path.
210
+ */
211
+ interface DataSource {
212
+ get(path: PathType): unknown;
213
+ has(path: PathType): boolean;
214
+ }
215
+ /**
216
+ * A write-only contract for any system that can accept data by path.
217
+ */
218
+ interface DataSink {
219
+ set(path: PathType, value: unknown): void;
220
+ create(path: PathType, value: unknown): void;
221
+ delete(path: PathType): void;
222
+ }
223
+ /**
224
+ * A full CRUD contract for systems that provide complete data management.
225
+ * Combines both reading and writing capabilities.
226
+ */
227
+ interface DataStorage extends DataSource, DataSink {
228
+ }
229
+
230
+ /**
231
+ * A minimal, type-safe event emitter for a single event.
232
+ * It does not manage state, it only manages subscribers and event dispatching.
233
+ * @template T A tuple representing the types of the event arguments.
234
+ */
235
+ declare class Emitter<T extends any[]> implements Subscribable<T> {
236
+ private listeners;
237
+ /**
238
+ * Returns the number of listeners.
239
+ */
240
+ get listenerCount(): number;
241
+ /**
242
+ * Subscribes a listener to this event.
243
+ * @returns A function to unsubscribe the listener.
244
+ */
245
+ subscribe(listener: (...args: T) => void): () => void;
246
+ /**
247
+ * Manually unsubscribe by listener
248
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
249
+ */
250
+ unsubscribe(listener: (...args: T) => void): boolean;
251
+ /**
252
+ * Dispatches the event to all subscribed listeners.
253
+ */
254
+ emit(...args: T): void;
255
+ /**
256
+ * Clears all listeners.
257
+ */
258
+ clear(): void;
259
+ }
260
+
261
+ /**
262
+ * Type guard that checks if a value is a valid ScalarType.
263
+ * @param value The value to check.
264
+ * @returns True if the value is a string, number, or boolean.
265
+ */
266
+ declare function isScalar(value: unknown): value is ScalarType;
267
+ declare function isNullOrUndefined(val: unknown): val is null | undefined;
268
+ declare function isUndefined(val: unknown): val is undefined;
269
+ declare function isNumber(val: unknown): val is number;
270
+ declare function isBoolean(val: unknown): val is boolean;
271
+ declare function isString(val: unknown): val is string;
272
+ declare function isNull(val: unknown): val is null;
273
+ /**
274
+ * Type guard to check if a value is a string that ends with '%'.
275
+ * @param val The value to check.
276
+ * @returns `true` if the value is a percentage string.
277
+ */
278
+ declare function isPercentageString(val: unknown): val is string;
279
+
280
+ /**
281
+ * Clamps a number between an optional minimum and maximum value.
282
+ * @param val The number to clamp.
283
+ * @param min The minimum value. If null or undefined, it's ignored.
284
+ * @param max The maximum value. If null or undefined, it's ignored.
285
+ * @returns The clamped number.
286
+ */
287
+ declare function clampNumber(val: number, min?: number | null, max?: number | null): number;
288
+ /**
289
+ * Calculates a percentage of a given value.
290
+ * @param val The base value.
291
+ * @param percents The percentage to get.
292
+ * @returns The calculated percentage of the value.
293
+ * @example getPercentOf(200, 10); // returns 20
294
+ */
295
+ declare function getPercentOf(val: number, percents: number): number;
296
+
297
+ /**
298
+ * Returns the first key of an object.
299
+ * @param obj The object from which to get the key.
300
+ * @returns The first key of the object as a string.
301
+ */
302
+ declare function firstKeyOf(obj: any): string;
303
+
304
+ /**
305
+ * Ensures that the given path is returned as an array of segments.
306
+ */
307
+ declare function ensurePathArray(path: PathType, separator?: string): string[];
308
+ /**
309
+ * Ensures that the given path is returned as a single string.
310
+ */
311
+ declare function ensurePathString(path: PathType, separator?: string): string;
312
+
313
+ /**
314
+ * Returns a random integer between min (inclusive) and max (exclusive).
315
+ * @param min The minimum integer (inclusive).
316
+ * @param max The maximum integer (exclusive).
317
+ * @returns A random integer.
318
+ * @example randInt(1, 5); // returns 1, 2, 3, or 4
319
+ */
320
+ declare function randInt(min: number, max: number): number;
321
+ /**
322
+ * Generates a unique identifier using uuidv4.
323
+ * @returns A unique string ID.
324
+ */
325
+ declare function randId(): string;
326
+
327
+ export { type AxiEngineConfig, type Constructor, ConstructorRegistry, type DataSink, type DataSource, type DataStorage, Emitter, type PathType, type ScalarType, type Subscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isNull, isNullOrUndefined, isNumber, isPercentageString, isScalar, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwIf, throwIfEmpty, unique };