@axi-engine/utils 0.2.5 → 0.2.7
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/dist/index.d.mts +97 -42
- package/dist/index.d.ts +97 -42
- package/dist/index.js +72 -51
- package/dist/index.mjs +68 -50
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -171,45 +171,6 @@ declare const axiSettings: AxiEngineConfig;
|
|
|
171
171
|
*/
|
|
172
172
|
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
173
173
|
|
|
174
|
-
/**
|
|
175
|
-
* A generic registry for mapping string identifiers to class constructors.
|
|
176
|
-
*
|
|
177
|
-
* This utility is fundamental for building extensible systems like dependency injection containers,
|
|
178
|
-
* factories, and serialization engines where types need to be dynamically resolved.
|
|
179
|
-
*
|
|
180
|
-
* @template T - A base type that all registered constructors must produce an instance of.
|
|
181
|
-
*/
|
|
182
|
-
declare class ConstructorRegistry<T> {
|
|
183
|
-
private readonly items;
|
|
184
|
-
/**
|
|
185
|
-
* Registers a constructor with a unique string identifier.
|
|
186
|
-
*
|
|
187
|
-
* @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
|
|
188
|
-
* @param ctor - The class constructor to register.
|
|
189
|
-
* @returns The registry instance for chainable calls.
|
|
190
|
-
* @throws If a constructor with the same `typeId` is already registered.
|
|
191
|
-
*/
|
|
192
|
-
register(typeId: string, ctor: Constructor<T>): this;
|
|
193
|
-
/**
|
|
194
|
-
* Retrieves a constructor by its identifier.
|
|
195
|
-
*
|
|
196
|
-
* @param typeId - The identifier of the constructor to retrieve.
|
|
197
|
-
* @returns The found class constructor.
|
|
198
|
-
* @throws If no constructor is found for the given `typeId`.
|
|
199
|
-
*/
|
|
200
|
-
get(typeId: string): Constructor<T>;
|
|
201
|
-
/**
|
|
202
|
-
* Checks if a constructor for a given identifier is registered.
|
|
203
|
-
* @param typeId - The identifier to check.
|
|
204
|
-
* @returns `true` if a constructor is registered, otherwise `false`.
|
|
205
|
-
*/
|
|
206
|
-
has(typeId: string): boolean;
|
|
207
|
-
/**
|
|
208
|
-
* Clears all registered constructors from the registry.
|
|
209
|
-
*/
|
|
210
|
-
clear(): void;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
174
|
/**
|
|
214
175
|
* A read-only contract for any system that can provide data by path.
|
|
215
176
|
*/
|
|
@@ -309,12 +270,64 @@ declare class Emitter<T extends any[]> implements Subscribable<T> {
|
|
|
309
270
|
* @returns True if the value is a string, number, or boolean.
|
|
310
271
|
*/
|
|
311
272
|
declare function isScalar(value: unknown): value is ScalarType;
|
|
312
|
-
|
|
273
|
+
/**
|
|
274
|
+
* Type guard that checks if a value is `null`.
|
|
275
|
+
* @param val The value to check.
|
|
276
|
+
* @returns {boolean} `true` if the value is `null`, otherwise `false`.
|
|
277
|
+
*/
|
|
278
|
+
declare function isNull(val: unknown): val is null;
|
|
279
|
+
/**
|
|
280
|
+
* Type guard that checks if a value is `undefined`.
|
|
281
|
+
* @param val The value to check.
|
|
282
|
+
* @returns {boolean} `true` if the value is `undefined`, otherwise `false`.
|
|
283
|
+
*/
|
|
313
284
|
declare function isUndefined(val: unknown): val is undefined;
|
|
285
|
+
/**
|
|
286
|
+
* Type guard that checks if a value is either `null` or `undefined`.
|
|
287
|
+
* @param val The value to check.
|
|
288
|
+
* @returns {boolean} `true` if the value is `null` or `undefined`, otherwise `false`.
|
|
289
|
+
*/
|
|
290
|
+
declare function isNullOrUndefined(val: unknown): val is null | undefined;
|
|
291
|
+
/**
|
|
292
|
+
* Type guard that checks if a value is a `number`.
|
|
293
|
+
* @param val The value to check.
|
|
294
|
+
* @returns {boolean} `true` if the value is a `number`, otherwise `false`.
|
|
295
|
+
*/
|
|
314
296
|
declare function isNumber(val: unknown): val is number;
|
|
297
|
+
/**
|
|
298
|
+
* Type guard that checks if a value is a `boolean`.
|
|
299
|
+
* @param val The value to check.
|
|
300
|
+
* @returns {boolean} `true` if the value is a `boolean`, otherwise `false`.
|
|
301
|
+
*/
|
|
315
302
|
declare function isBoolean(val: unknown): val is boolean;
|
|
303
|
+
/**
|
|
304
|
+
* Type guard that checks if a value is a `string`.
|
|
305
|
+
* @param val The value to check.
|
|
306
|
+
* @returns {boolean} `true` if the value is a `string`, otherwise `false`.
|
|
307
|
+
*/
|
|
316
308
|
declare function isString(val: unknown): val is string;
|
|
317
|
-
|
|
309
|
+
/**
|
|
310
|
+
* Check if a value is a plain object.
|
|
311
|
+
* Correctly handles `null` and arrays, returning `false` for them.
|
|
312
|
+
*
|
|
313
|
+
* @param value The value to check.
|
|
314
|
+
* @returns {boolean} `true` if the value is a non-null, non-array object.
|
|
315
|
+
*/
|
|
316
|
+
declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
317
|
+
/**
|
|
318
|
+
* Type guard that checks if a value is a function.
|
|
319
|
+
*
|
|
320
|
+
* @param value The value to check.
|
|
321
|
+
* @returns {boolean} `true` if the value is a function.
|
|
322
|
+
*/
|
|
323
|
+
declare function isFunction(value: unknown): value is (...args: any[]) => any;
|
|
324
|
+
/**
|
|
325
|
+
* Type guard that checks if a value is a Promise-like object.
|
|
326
|
+
*
|
|
327
|
+
* @param value The value to check.
|
|
328
|
+
* @returns {boolean} `true` if the value has a `then` function.
|
|
329
|
+
*/
|
|
330
|
+
declare function isPromise(value: unknown): value is Promise<unknown>;
|
|
318
331
|
/**
|
|
319
332
|
* Type guard to check if a value is a string that ends with '%'.
|
|
320
333
|
* @param val The value to check.
|
|
@@ -369,4 +382,46 @@ declare function randInt(min: number, max: number): number;
|
|
|
369
382
|
*/
|
|
370
383
|
declare function randId(): string;
|
|
371
384
|
|
|
372
|
-
|
|
385
|
+
/**
|
|
386
|
+
* A generic, type-safe wrapper around a Map for managing collections of items by key.
|
|
387
|
+
* This class provides a consistent API for registering, retrieving, and checking for
|
|
388
|
+
* the existence of items.
|
|
389
|
+
*
|
|
390
|
+
* @template K - The type of the key (must be a string).
|
|
391
|
+
* @template V - The type of the value being stored.
|
|
392
|
+
*/
|
|
393
|
+
declare class Registry<K extends PropertyKey, V> {
|
|
394
|
+
protected readonly items: Map<K, V>;
|
|
395
|
+
/**
|
|
396
|
+
* Registers an item with a specific key.
|
|
397
|
+
* Warns if an item with the same key is already registered.
|
|
398
|
+
* @param key The key to associate with the item.
|
|
399
|
+
* @param value The item to register.
|
|
400
|
+
*/
|
|
401
|
+
register(key: K, value: V): void;
|
|
402
|
+
/**
|
|
403
|
+
* Checks if an item with the given key is registered.
|
|
404
|
+
* @param key The key to check.
|
|
405
|
+
*/
|
|
406
|
+
has(key: K): boolean;
|
|
407
|
+
/**
|
|
408
|
+
* Retrieves an item by its key.
|
|
409
|
+
* @param key The key of the item to retrieve.
|
|
410
|
+
* @returns The item, or `undefined` if not found.
|
|
411
|
+
*/
|
|
412
|
+
get(key: K): V | undefined;
|
|
413
|
+
/**
|
|
414
|
+
* Retrieves an item by its key, throwing an error if it's not found.
|
|
415
|
+
* @param key The key of the item to retrieve.
|
|
416
|
+
* @returns The item.
|
|
417
|
+
* @throws {Error} if no item is found for the given key.
|
|
418
|
+
*/
|
|
419
|
+
getOrThrow(key: K): V;
|
|
420
|
+
delete(key: K): boolean;
|
|
421
|
+
/**
|
|
422
|
+
* Clears all registered items from the registry.
|
|
423
|
+
*/
|
|
424
|
+
clear(): void;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export { type AxiEngineConfig, type Constructor, type DataSink, type DataSource, type DataStorage, Emitter, type PathType, Registry, type ScalarType, type Subscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isPercentageString, isPromise, isScalar, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwError, throwIf, throwIfEmpty, unique };
|
package/dist/index.d.ts
CHANGED
|
@@ -171,45 +171,6 @@ declare const axiSettings: AxiEngineConfig;
|
|
|
171
171
|
*/
|
|
172
172
|
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
173
173
|
|
|
174
|
-
/**
|
|
175
|
-
* A generic registry for mapping string identifiers to class constructors.
|
|
176
|
-
*
|
|
177
|
-
* This utility is fundamental for building extensible systems like dependency injection containers,
|
|
178
|
-
* factories, and serialization engines where types need to be dynamically resolved.
|
|
179
|
-
*
|
|
180
|
-
* @template T - A base type that all registered constructors must produce an instance of.
|
|
181
|
-
*/
|
|
182
|
-
declare class ConstructorRegistry<T> {
|
|
183
|
-
private readonly items;
|
|
184
|
-
/**
|
|
185
|
-
* Registers a constructor with a unique string identifier.
|
|
186
|
-
*
|
|
187
|
-
* @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
|
|
188
|
-
* @param ctor - The class constructor to register.
|
|
189
|
-
* @returns The registry instance for chainable calls.
|
|
190
|
-
* @throws If a constructor with the same `typeId` is already registered.
|
|
191
|
-
*/
|
|
192
|
-
register(typeId: string, ctor: Constructor<T>): this;
|
|
193
|
-
/**
|
|
194
|
-
* Retrieves a constructor by its identifier.
|
|
195
|
-
*
|
|
196
|
-
* @param typeId - The identifier of the constructor to retrieve.
|
|
197
|
-
* @returns The found class constructor.
|
|
198
|
-
* @throws If no constructor is found for the given `typeId`.
|
|
199
|
-
*/
|
|
200
|
-
get(typeId: string): Constructor<T>;
|
|
201
|
-
/**
|
|
202
|
-
* Checks if a constructor for a given identifier is registered.
|
|
203
|
-
* @param typeId - The identifier to check.
|
|
204
|
-
* @returns `true` if a constructor is registered, otherwise `false`.
|
|
205
|
-
*/
|
|
206
|
-
has(typeId: string): boolean;
|
|
207
|
-
/**
|
|
208
|
-
* Clears all registered constructors from the registry.
|
|
209
|
-
*/
|
|
210
|
-
clear(): void;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
174
|
/**
|
|
214
175
|
* A read-only contract for any system that can provide data by path.
|
|
215
176
|
*/
|
|
@@ -309,12 +270,64 @@ declare class Emitter<T extends any[]> implements Subscribable<T> {
|
|
|
309
270
|
* @returns True if the value is a string, number, or boolean.
|
|
310
271
|
*/
|
|
311
272
|
declare function isScalar(value: unknown): value is ScalarType;
|
|
312
|
-
|
|
273
|
+
/**
|
|
274
|
+
* Type guard that checks if a value is `null`.
|
|
275
|
+
* @param val The value to check.
|
|
276
|
+
* @returns {boolean} `true` if the value is `null`, otherwise `false`.
|
|
277
|
+
*/
|
|
278
|
+
declare function isNull(val: unknown): val is null;
|
|
279
|
+
/**
|
|
280
|
+
* Type guard that checks if a value is `undefined`.
|
|
281
|
+
* @param val The value to check.
|
|
282
|
+
* @returns {boolean} `true` if the value is `undefined`, otherwise `false`.
|
|
283
|
+
*/
|
|
313
284
|
declare function isUndefined(val: unknown): val is undefined;
|
|
285
|
+
/**
|
|
286
|
+
* Type guard that checks if a value is either `null` or `undefined`.
|
|
287
|
+
* @param val The value to check.
|
|
288
|
+
* @returns {boolean} `true` if the value is `null` or `undefined`, otherwise `false`.
|
|
289
|
+
*/
|
|
290
|
+
declare function isNullOrUndefined(val: unknown): val is null | undefined;
|
|
291
|
+
/**
|
|
292
|
+
* Type guard that checks if a value is a `number`.
|
|
293
|
+
* @param val The value to check.
|
|
294
|
+
* @returns {boolean} `true` if the value is a `number`, otherwise `false`.
|
|
295
|
+
*/
|
|
314
296
|
declare function isNumber(val: unknown): val is number;
|
|
297
|
+
/**
|
|
298
|
+
* Type guard that checks if a value is a `boolean`.
|
|
299
|
+
* @param val The value to check.
|
|
300
|
+
* @returns {boolean} `true` if the value is a `boolean`, otherwise `false`.
|
|
301
|
+
*/
|
|
315
302
|
declare function isBoolean(val: unknown): val is boolean;
|
|
303
|
+
/**
|
|
304
|
+
* Type guard that checks if a value is a `string`.
|
|
305
|
+
* @param val The value to check.
|
|
306
|
+
* @returns {boolean} `true` if the value is a `string`, otherwise `false`.
|
|
307
|
+
*/
|
|
316
308
|
declare function isString(val: unknown): val is string;
|
|
317
|
-
|
|
309
|
+
/**
|
|
310
|
+
* Check if a value is a plain object.
|
|
311
|
+
* Correctly handles `null` and arrays, returning `false` for them.
|
|
312
|
+
*
|
|
313
|
+
* @param value The value to check.
|
|
314
|
+
* @returns {boolean} `true` if the value is a non-null, non-array object.
|
|
315
|
+
*/
|
|
316
|
+
declare function isObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
317
|
+
/**
|
|
318
|
+
* Type guard that checks if a value is a function.
|
|
319
|
+
*
|
|
320
|
+
* @param value The value to check.
|
|
321
|
+
* @returns {boolean} `true` if the value is a function.
|
|
322
|
+
*/
|
|
323
|
+
declare function isFunction(value: unknown): value is (...args: any[]) => any;
|
|
324
|
+
/**
|
|
325
|
+
* Type guard that checks if a value is a Promise-like object.
|
|
326
|
+
*
|
|
327
|
+
* @param value The value to check.
|
|
328
|
+
* @returns {boolean} `true` if the value has a `then` function.
|
|
329
|
+
*/
|
|
330
|
+
declare function isPromise(value: unknown): value is Promise<unknown>;
|
|
318
331
|
/**
|
|
319
332
|
* Type guard to check if a value is a string that ends with '%'.
|
|
320
333
|
* @param val The value to check.
|
|
@@ -369,4 +382,46 @@ declare function randInt(min: number, max: number): number;
|
|
|
369
382
|
*/
|
|
370
383
|
declare function randId(): string;
|
|
371
384
|
|
|
372
|
-
|
|
385
|
+
/**
|
|
386
|
+
* A generic, type-safe wrapper around a Map for managing collections of items by key.
|
|
387
|
+
* This class provides a consistent API for registering, retrieving, and checking for
|
|
388
|
+
* the existence of items.
|
|
389
|
+
*
|
|
390
|
+
* @template K - The type of the key (must be a string).
|
|
391
|
+
* @template V - The type of the value being stored.
|
|
392
|
+
*/
|
|
393
|
+
declare class Registry<K extends PropertyKey, V> {
|
|
394
|
+
protected readonly items: Map<K, V>;
|
|
395
|
+
/**
|
|
396
|
+
* Registers an item with a specific key.
|
|
397
|
+
* Warns if an item with the same key is already registered.
|
|
398
|
+
* @param key The key to associate with the item.
|
|
399
|
+
* @param value The item to register.
|
|
400
|
+
*/
|
|
401
|
+
register(key: K, value: V): void;
|
|
402
|
+
/**
|
|
403
|
+
* Checks if an item with the given key is registered.
|
|
404
|
+
* @param key The key to check.
|
|
405
|
+
*/
|
|
406
|
+
has(key: K): boolean;
|
|
407
|
+
/**
|
|
408
|
+
* Retrieves an item by its key.
|
|
409
|
+
* @param key The key of the item to retrieve.
|
|
410
|
+
* @returns The item, or `undefined` if not found.
|
|
411
|
+
*/
|
|
412
|
+
get(key: K): V | undefined;
|
|
413
|
+
/**
|
|
414
|
+
* Retrieves an item by its key, throwing an error if it's not found.
|
|
415
|
+
* @param key The key of the item to retrieve.
|
|
416
|
+
* @returns The item.
|
|
417
|
+
* @throws {Error} if no item is found for the given key.
|
|
418
|
+
*/
|
|
419
|
+
getOrThrow(key: K): V;
|
|
420
|
+
delete(key: K): boolean;
|
|
421
|
+
/**
|
|
422
|
+
* Clears all registered items from the registry.
|
|
423
|
+
*/
|
|
424
|
+
clear(): void;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export { type AxiEngineConfig, type Constructor, type DataSink, type DataSource, type DataStorage, Emitter, type PathType, Registry, type ScalarType, type Subscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isPercentageString, isPromise, isScalar, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwError, throwIf, throwIfEmpty, unique };
|
package/dist/index.js
CHANGED
|
@@ -20,8 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
ConstructorRegistry: () => ConstructorRegistry,
|
|
24
23
|
Emitter: () => Emitter,
|
|
24
|
+
Registry: () => Registry,
|
|
25
25
|
areArraysEqual: () => areArraysEqual,
|
|
26
26
|
axiSettings: () => axiSettings,
|
|
27
27
|
clampNumber: () => clampNumber,
|
|
@@ -34,10 +34,13 @@ __export(index_exports, {
|
|
|
34
34
|
getRandomElement: () => getRandomElement,
|
|
35
35
|
haveSameElements: () => haveSameElements,
|
|
36
36
|
isBoolean: () => isBoolean,
|
|
37
|
+
isFunction: () => isFunction,
|
|
37
38
|
isNull: () => isNull,
|
|
38
39
|
isNullOrUndefined: () => isNullOrUndefined,
|
|
39
40
|
isNumber: () => isNumber,
|
|
41
|
+
isObject: () => isObject,
|
|
40
42
|
isPercentageString: () => isPercentageString,
|
|
43
|
+
isPromise: () => isPromise,
|
|
41
44
|
isScalar: () => isScalar,
|
|
42
45
|
isSequentialStart: () => isSequentialStart,
|
|
43
46
|
isString: () => isString,
|
|
@@ -104,11 +107,14 @@ function isScalar(value) {
|
|
|
104
107
|
const type = typeof value;
|
|
105
108
|
return type === "string" || type === "number" || type === "boolean";
|
|
106
109
|
}
|
|
107
|
-
function
|
|
108
|
-
return val ===
|
|
110
|
+
function isNull(val) {
|
|
111
|
+
return val === null;
|
|
109
112
|
}
|
|
110
113
|
function isUndefined(val) {
|
|
111
|
-
return
|
|
114
|
+
return val === void 0;
|
|
115
|
+
}
|
|
116
|
+
function isNullOrUndefined(val) {
|
|
117
|
+
return val === null || val === void 0;
|
|
112
118
|
}
|
|
113
119
|
function isNumber(val) {
|
|
114
120
|
return typeof val === "number";
|
|
@@ -119,8 +125,14 @@ function isBoolean(val) {
|
|
|
119
125
|
function isString(val) {
|
|
120
126
|
return typeof val === "string";
|
|
121
127
|
}
|
|
122
|
-
function
|
|
123
|
-
return
|
|
128
|
+
function isObject(value) {
|
|
129
|
+
return value !== null && !Array.isArray(value) && typeof value === "object";
|
|
130
|
+
}
|
|
131
|
+
function isFunction(value) {
|
|
132
|
+
return typeof value === "function";
|
|
133
|
+
}
|
|
134
|
+
function isPromise(value) {
|
|
135
|
+
return value != null && typeof value.then === "function";
|
|
124
136
|
}
|
|
125
137
|
function isPercentageString(val) {
|
|
126
138
|
return typeof val === "string" && val.endsWith("%");
|
|
@@ -151,50 +163,6 @@ function configure(newConfig) {
|
|
|
151
163
|
Object.assign(axiSettings, newConfig);
|
|
152
164
|
}
|
|
153
165
|
|
|
154
|
-
// src/constructor-registry.ts
|
|
155
|
-
var ConstructorRegistry = class {
|
|
156
|
-
items = /* @__PURE__ */ new Map();
|
|
157
|
-
/**
|
|
158
|
-
* Registers a constructor with a unique string identifier.
|
|
159
|
-
*
|
|
160
|
-
* @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
|
|
161
|
-
* @param ctor - The class constructor to register.
|
|
162
|
-
* @returns The registry instance for chainable calls.
|
|
163
|
-
* @throws If a constructor with the same `typeId` is already registered.
|
|
164
|
-
*/
|
|
165
|
-
register(typeId, ctor) {
|
|
166
|
-
throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);
|
|
167
|
-
this.items.set(typeId, ctor);
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Retrieves a constructor by its identifier.
|
|
172
|
-
*
|
|
173
|
-
* @param typeId - The identifier of the constructor to retrieve.
|
|
174
|
-
* @returns The found class constructor.
|
|
175
|
-
* @throws If no constructor is found for the given `typeId`.
|
|
176
|
-
*/
|
|
177
|
-
get(typeId) {
|
|
178
|
-
const Ctor = this.items.get(typeId);
|
|
179
|
-
throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);
|
|
180
|
-
return Ctor;
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Checks if a constructor for a given identifier is registered.
|
|
184
|
-
* @param typeId - The identifier to check.
|
|
185
|
-
* @returns `true` if a constructor is registered, otherwise `false`.
|
|
186
|
-
*/
|
|
187
|
-
has(typeId) {
|
|
188
|
-
return this.items.has(typeId);
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Clears all registered constructors from the registry.
|
|
192
|
-
*/
|
|
193
|
-
clear() {
|
|
194
|
-
this.items.clear();
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
|
|
198
166
|
// src/emitter.ts
|
|
199
167
|
var Emitter = class {
|
|
200
168
|
listeners = /* @__PURE__ */ new Set();
|
|
@@ -266,10 +234,60 @@ function randInt(min, max) {
|
|
|
266
234
|
function randId() {
|
|
267
235
|
return (0, import_uuid.v4)();
|
|
268
236
|
}
|
|
237
|
+
|
|
238
|
+
// src/registry.ts
|
|
239
|
+
var Registry = class {
|
|
240
|
+
items = /* @__PURE__ */ new Map();
|
|
241
|
+
/**
|
|
242
|
+
* Registers an item with a specific key.
|
|
243
|
+
* Warns if an item with the same key is already registered.
|
|
244
|
+
* @param key The key to associate with the item.
|
|
245
|
+
* @param value The item to register.
|
|
246
|
+
*/
|
|
247
|
+
register(key, value) {
|
|
248
|
+
throwIf(this.items.has(key), `An item with the key '${String(key)}' is already registered and will be overwritten.`);
|
|
249
|
+
this.items.set(key, value);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Checks if an item with the given key is registered.
|
|
253
|
+
* @param key The key to check.
|
|
254
|
+
*/
|
|
255
|
+
has(key) {
|
|
256
|
+
return this.items.has(key);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Retrieves an item by its key.
|
|
260
|
+
* @param key The key of the item to retrieve.
|
|
261
|
+
* @returns The item, or `undefined` if not found.
|
|
262
|
+
*/
|
|
263
|
+
get(key) {
|
|
264
|
+
return this.items.get(key);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Retrieves an item by its key, throwing an error if it's not found.
|
|
268
|
+
* @param key The key of the item to retrieve.
|
|
269
|
+
* @returns The item.
|
|
270
|
+
* @throws {Error} if no item is found for the given key.
|
|
271
|
+
*/
|
|
272
|
+
getOrThrow(key) {
|
|
273
|
+
const item = this.get(key);
|
|
274
|
+
throwIfEmpty(item, `No item registered for the key '${String(key)}'.`);
|
|
275
|
+
return item;
|
|
276
|
+
}
|
|
277
|
+
delete(key) {
|
|
278
|
+
return this.items.delete(key);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Clears all registered items from the registry.
|
|
282
|
+
*/
|
|
283
|
+
clear() {
|
|
284
|
+
this.items.clear();
|
|
285
|
+
}
|
|
286
|
+
};
|
|
269
287
|
// Annotate the CommonJS export names for ESM import in node:
|
|
270
288
|
0 && (module.exports = {
|
|
271
|
-
ConstructorRegistry,
|
|
272
289
|
Emitter,
|
|
290
|
+
Registry,
|
|
273
291
|
areArraysEqual,
|
|
274
292
|
axiSettings,
|
|
275
293
|
clampNumber,
|
|
@@ -282,10 +300,13 @@ function randId() {
|
|
|
282
300
|
getRandomElement,
|
|
283
301
|
haveSameElements,
|
|
284
302
|
isBoolean,
|
|
303
|
+
isFunction,
|
|
285
304
|
isNull,
|
|
286
305
|
isNullOrUndefined,
|
|
287
306
|
isNumber,
|
|
307
|
+
isObject,
|
|
288
308
|
isPercentageString,
|
|
309
|
+
isPromise,
|
|
289
310
|
isScalar,
|
|
290
311
|
isSequentialStart,
|
|
291
312
|
isString,
|
package/dist/index.mjs
CHANGED
|
@@ -49,11 +49,14 @@ function isScalar(value) {
|
|
|
49
49
|
const type = typeof value;
|
|
50
50
|
return type === "string" || type === "number" || type === "boolean";
|
|
51
51
|
}
|
|
52
|
-
function
|
|
53
|
-
return val ===
|
|
52
|
+
function isNull(val) {
|
|
53
|
+
return val === null;
|
|
54
54
|
}
|
|
55
55
|
function isUndefined(val) {
|
|
56
|
-
return
|
|
56
|
+
return val === void 0;
|
|
57
|
+
}
|
|
58
|
+
function isNullOrUndefined(val) {
|
|
59
|
+
return val === null || val === void 0;
|
|
57
60
|
}
|
|
58
61
|
function isNumber(val) {
|
|
59
62
|
return typeof val === "number";
|
|
@@ -64,8 +67,14 @@ function isBoolean(val) {
|
|
|
64
67
|
function isString(val) {
|
|
65
68
|
return typeof val === "string";
|
|
66
69
|
}
|
|
67
|
-
function
|
|
68
|
-
return
|
|
70
|
+
function isObject(value) {
|
|
71
|
+
return value !== null && !Array.isArray(value) && typeof value === "object";
|
|
72
|
+
}
|
|
73
|
+
function isFunction(value) {
|
|
74
|
+
return typeof value === "function";
|
|
75
|
+
}
|
|
76
|
+
function isPromise(value) {
|
|
77
|
+
return value != null && typeof value.then === "function";
|
|
69
78
|
}
|
|
70
79
|
function isPercentageString(val) {
|
|
71
80
|
return typeof val === "string" && val.endsWith("%");
|
|
@@ -96,50 +105,6 @@ function configure(newConfig) {
|
|
|
96
105
|
Object.assign(axiSettings, newConfig);
|
|
97
106
|
}
|
|
98
107
|
|
|
99
|
-
// src/constructor-registry.ts
|
|
100
|
-
var ConstructorRegistry = class {
|
|
101
|
-
items = /* @__PURE__ */ new Map();
|
|
102
|
-
/**
|
|
103
|
-
* Registers a constructor with a unique string identifier.
|
|
104
|
-
*
|
|
105
|
-
* @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
|
|
106
|
-
* @param ctor - The class constructor to register.
|
|
107
|
-
* @returns The registry instance for chainable calls.
|
|
108
|
-
* @throws If a constructor with the same `typeId` is already registered.
|
|
109
|
-
*/
|
|
110
|
-
register(typeId, ctor) {
|
|
111
|
-
throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);
|
|
112
|
-
this.items.set(typeId, ctor);
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Retrieves a constructor by its identifier.
|
|
117
|
-
*
|
|
118
|
-
* @param typeId - The identifier of the constructor to retrieve.
|
|
119
|
-
* @returns The found class constructor.
|
|
120
|
-
* @throws If no constructor is found for the given `typeId`.
|
|
121
|
-
*/
|
|
122
|
-
get(typeId) {
|
|
123
|
-
const Ctor = this.items.get(typeId);
|
|
124
|
-
throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);
|
|
125
|
-
return Ctor;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Checks if a constructor for a given identifier is registered.
|
|
129
|
-
* @param typeId - The identifier to check.
|
|
130
|
-
* @returns `true` if a constructor is registered, otherwise `false`.
|
|
131
|
-
*/
|
|
132
|
-
has(typeId) {
|
|
133
|
-
return this.items.has(typeId);
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Clears all registered constructors from the registry.
|
|
137
|
-
*/
|
|
138
|
-
clear() {
|
|
139
|
-
this.items.clear();
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
108
|
// src/emitter.ts
|
|
144
109
|
var Emitter = class {
|
|
145
110
|
listeners = /* @__PURE__ */ new Set();
|
|
@@ -211,9 +176,59 @@ function randInt(min, max) {
|
|
|
211
176
|
function randId() {
|
|
212
177
|
return uuidv4();
|
|
213
178
|
}
|
|
179
|
+
|
|
180
|
+
// src/registry.ts
|
|
181
|
+
var Registry = class {
|
|
182
|
+
items = /* @__PURE__ */ new Map();
|
|
183
|
+
/**
|
|
184
|
+
* Registers an item with a specific key.
|
|
185
|
+
* Warns if an item with the same key is already registered.
|
|
186
|
+
* @param key The key to associate with the item.
|
|
187
|
+
* @param value The item to register.
|
|
188
|
+
*/
|
|
189
|
+
register(key, value) {
|
|
190
|
+
throwIf(this.items.has(key), `An item with the key '${String(key)}' is already registered and will be overwritten.`);
|
|
191
|
+
this.items.set(key, value);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Checks if an item with the given key is registered.
|
|
195
|
+
* @param key The key to check.
|
|
196
|
+
*/
|
|
197
|
+
has(key) {
|
|
198
|
+
return this.items.has(key);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Retrieves an item by its key.
|
|
202
|
+
* @param key The key of the item to retrieve.
|
|
203
|
+
* @returns The item, or `undefined` if not found.
|
|
204
|
+
*/
|
|
205
|
+
get(key) {
|
|
206
|
+
return this.items.get(key);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Retrieves an item by its key, throwing an error if it's not found.
|
|
210
|
+
* @param key The key of the item to retrieve.
|
|
211
|
+
* @returns The item.
|
|
212
|
+
* @throws {Error} if no item is found for the given key.
|
|
213
|
+
*/
|
|
214
|
+
getOrThrow(key) {
|
|
215
|
+
const item = this.get(key);
|
|
216
|
+
throwIfEmpty(item, `No item registered for the key '${String(key)}'.`);
|
|
217
|
+
return item;
|
|
218
|
+
}
|
|
219
|
+
delete(key) {
|
|
220
|
+
return this.items.delete(key);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Clears all registered items from the registry.
|
|
224
|
+
*/
|
|
225
|
+
clear() {
|
|
226
|
+
this.items.clear();
|
|
227
|
+
}
|
|
228
|
+
};
|
|
214
229
|
export {
|
|
215
|
-
ConstructorRegistry,
|
|
216
230
|
Emitter,
|
|
231
|
+
Registry,
|
|
217
232
|
areArraysEqual,
|
|
218
233
|
axiSettings,
|
|
219
234
|
clampNumber,
|
|
@@ -226,10 +241,13 @@ export {
|
|
|
226
241
|
getRandomElement,
|
|
227
242
|
haveSameElements,
|
|
228
243
|
isBoolean,
|
|
244
|
+
isFunction,
|
|
229
245
|
isNull,
|
|
230
246
|
isNullOrUndefined,
|
|
231
247
|
isNumber,
|
|
248
|
+
isObject,
|
|
232
249
|
isPercentageString,
|
|
250
|
+
isPromise,
|
|
233
251
|
isScalar,
|
|
234
252
|
isSequentialStart,
|
|
235
253
|
isString,
|
package/package.json
CHANGED