@axi-engine/utils 0.1.6 → 0.1.8
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/README.md +1 -4
- package/dist/index.cjs +380 -0
- package/dist/{index.d.ts → index.d.cts} +94 -43
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +94 -43
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +298 -130
- package/dist/index.mjs.map +1 -0
- package/package.json +38 -33
- package/dist/index.js +0 -239
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Represents a path that can be provided as a single string
|
|
3
4
|
* or an array of segments.
|
|
@@ -38,16 +39,17 @@ type Constructor<T = {}> = new (...args: any[]) => T;
|
|
|
38
39
|
* @template T A tuple representing the types of the event arguments.
|
|
39
40
|
*/
|
|
40
41
|
type Subscribable<T extends any[]> = {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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;
|
|
49
50
|
};
|
|
50
|
-
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/arrays.d.ts
|
|
51
53
|
/**
|
|
52
54
|
* Generates an array of numbers from 0 to length-1.
|
|
53
55
|
* @param length The desired length of the array.
|
|
@@ -119,7 +121,8 @@ declare function unique<T>(array: T[]): T[];
|
|
|
119
121
|
* @returns A random element from the array, or `undefined` if the array is empty.
|
|
120
122
|
*/
|
|
121
123
|
declare function getRandomElement<T>(array: T[]): T | undefined;
|
|
122
|
-
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/assertion.d.ts
|
|
123
126
|
/**
|
|
124
127
|
* Throws an error if the condition is true.
|
|
125
128
|
* @param conditionForThrow - If true, an error will be thrown.
|
|
@@ -150,9 +153,10 @@ declare function throwIf(conditionForThrow: boolean, exceptionMessage: string):
|
|
|
150
153
|
* console.log('First item:', items[0]);
|
|
151
154
|
*/
|
|
152
155
|
declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
|
|
153
|
-
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/config.d.ts
|
|
154
158
|
interface AxiEngineConfig {
|
|
155
|
-
|
|
159
|
+
pathSeparator: string;
|
|
156
160
|
}
|
|
157
161
|
declare const axiSettings: AxiEngineConfig;
|
|
158
162
|
/**
|
|
@@ -160,38 +164,80 @@ declare const axiSettings: AxiEngineConfig;
|
|
|
160
164
|
* @param newConfig - configuration object
|
|
161
165
|
*/
|
|
162
166
|
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
163
|
-
|
|
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
|
|
164
209
|
/**
|
|
165
210
|
* A minimal, type-safe event emitter for a single event.
|
|
166
211
|
* It does not manage state, it only manages subscribers and event dispatching.
|
|
167
212
|
* @template T A tuple representing the types of the event arguments.
|
|
168
213
|
*/
|
|
169
214
|
declare class Emitter<T extends any[]> implements Subscribable<T> {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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;
|
|
193
238
|
}
|
|
194
|
-
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/guards.d.ts
|
|
195
241
|
declare function isNullOrUndefined(val: unknown): val is null | undefined;
|
|
196
242
|
declare function isUndefined(val: unknown): val is undefined;
|
|
197
243
|
declare function isNumber(val: unknown): val is number;
|
|
@@ -203,7 +249,8 @@ declare function isString(val: unknown): val is string;
|
|
|
203
249
|
* @returns `true` if the value is a percentage string.
|
|
204
250
|
*/
|
|
205
251
|
declare function isPercentageString(val: unknown): val is string;
|
|
206
|
-
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region src/math.d.ts
|
|
207
254
|
/**
|
|
208
255
|
* Clamps a number between an optional minimum and maximum value.
|
|
209
256
|
* @param val The number to clamp.
|
|
@@ -220,14 +267,16 @@ declare function clampNumber(val: number, min?: number | null, max?: number | nu
|
|
|
220
267
|
* @example getPercentOf(200, 10); // returns 20
|
|
221
268
|
*/
|
|
222
269
|
declare function getPercentOf(val: number, percents: number): number;
|
|
223
|
-
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/misc.d.ts
|
|
224
272
|
/**
|
|
225
273
|
* Returns the first key of an object.
|
|
226
274
|
* @param obj The object from which to get the key.
|
|
227
275
|
* @returns The first key of the object as a string.
|
|
228
276
|
*/
|
|
229
277
|
declare function firstKeyOf(obj: any): string;
|
|
230
|
-
|
|
278
|
+
//#endregion
|
|
279
|
+
//#region src/path.d.ts
|
|
231
280
|
/**
|
|
232
281
|
* Ensures that the given path is returned as an array of segments.
|
|
233
282
|
*/
|
|
@@ -236,7 +285,8 @@ declare function ensurePathArray(path: PathType, separator?: string): string[];
|
|
|
236
285
|
* Ensures that the given path is returned as a single string.
|
|
237
286
|
*/
|
|
238
287
|
declare function ensurePathString(path: PathType, separator?: string): string;
|
|
239
|
-
|
|
288
|
+
//#endregion
|
|
289
|
+
//#region src/random.d.ts
|
|
240
290
|
/**
|
|
241
291
|
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
242
292
|
* @param min The minimum integer (inclusive).
|
|
@@ -250,5 +300,6 @@ declare function randInt(min: number, max: number): number;
|
|
|
250
300
|
* @returns A unique string ID.
|
|
251
301
|
*/
|
|
252
302
|
declare function randId(): string;
|
|
253
|
-
|
|
254
|
-
export {
|
|
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.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,a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