@axi-engine/utils 0.2.4 → 0.2.6

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 CHANGED
@@ -155,6 +155,11 @@ declare function throwIf(condition: boolean, exceptionMessage: string): asserts
155
155
  * console.log('First item:', items[0]);
156
156
  */
157
157
  declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
158
+ /**
159
+ * Throws an error unconditionally.
160
+ * @param message The message for the error.
161
+ */
162
+ declare function throwError(message: string): never;
158
163
 
159
164
  interface AxiEngineConfig {
160
165
  pathSeparator: string;
@@ -166,45 +171,6 @@ declare const axiSettings: AxiEngineConfig;
166
171
  */
167
172
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
168
173
 
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
174
  /**
209
175
  * A read-only contract for any system that can provide data by path.
210
176
  */
@@ -364,4 +330,46 @@ declare function randInt(min: number, max: number): number;
364
330
  */
365
331
  declare function randId(): string;
366
332
 
367
- 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 };
333
+ /**
334
+ * A generic, type-safe wrapper around a Map for managing collections of items by key.
335
+ * This class provides a consistent API for registering, retrieving, and checking for
336
+ * the existence of items.
337
+ *
338
+ * @template K - The type of the key (must be a string).
339
+ * @template V - The type of the value being stored.
340
+ */
341
+ declare class Registry<K extends string, V> {
342
+ protected readonly items: Map<K, V>;
343
+ /**
344
+ * Registers an item with a specific key.
345
+ * Warns if an item with the same key is already registered.
346
+ * @param key The key to associate with the item.
347
+ * @param value The item to register.
348
+ */
349
+ register(key: K, value: V): void;
350
+ /**
351
+ * Checks if an item with the given key is registered.
352
+ * @param key The key to check.
353
+ */
354
+ has(key: K): boolean;
355
+ /**
356
+ * Retrieves an item by its key.
357
+ * @param key The key of the item to retrieve.
358
+ * @returns The item, or `undefined` if not found.
359
+ */
360
+ get(key: K): V | undefined;
361
+ /**
362
+ * Retrieves an item by its key, throwing an error if it's not found.
363
+ * @param key The key of the item to retrieve.
364
+ * @returns The item.
365
+ * @throws {Error} if no item is found for the given key.
366
+ */
367
+ getOrThrow(key: K): V;
368
+ delete(key: K): boolean;
369
+ /**
370
+ * Clears all registered items from the registry.
371
+ */
372
+ clear(): void;
373
+ }
374
+
375
+ 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, isNull, isNullOrUndefined, isNumber, isPercentageString, isScalar, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwError, throwIf, throwIfEmpty, unique };
package/dist/index.d.ts CHANGED
@@ -155,6 +155,11 @@ declare function throwIf(condition: boolean, exceptionMessage: string): asserts
155
155
  * console.log('First item:', items[0]);
156
156
  */
157
157
  declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
158
+ /**
159
+ * Throws an error unconditionally.
160
+ * @param message The message for the error.
161
+ */
162
+ declare function throwError(message: string): never;
158
163
 
159
164
  interface AxiEngineConfig {
160
165
  pathSeparator: string;
@@ -166,45 +171,6 @@ declare const axiSettings: AxiEngineConfig;
166
171
  */
167
172
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
168
173
 
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
174
  /**
209
175
  * A read-only contract for any system that can provide data by path.
210
176
  */
@@ -364,4 +330,46 @@ declare function randInt(min: number, max: number): number;
364
330
  */
365
331
  declare function randId(): string;
366
332
 
367
- 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 };
333
+ /**
334
+ * A generic, type-safe wrapper around a Map for managing collections of items by key.
335
+ * This class provides a consistent API for registering, retrieving, and checking for
336
+ * the existence of items.
337
+ *
338
+ * @template K - The type of the key (must be a string).
339
+ * @template V - The type of the value being stored.
340
+ */
341
+ declare class Registry<K extends string, V> {
342
+ protected readonly items: Map<K, V>;
343
+ /**
344
+ * Registers an item with a specific key.
345
+ * Warns if an item with the same key is already registered.
346
+ * @param key The key to associate with the item.
347
+ * @param value The item to register.
348
+ */
349
+ register(key: K, value: V): void;
350
+ /**
351
+ * Checks if an item with the given key is registered.
352
+ * @param key The key to check.
353
+ */
354
+ has(key: K): boolean;
355
+ /**
356
+ * Retrieves an item by its key.
357
+ * @param key The key of the item to retrieve.
358
+ * @returns The item, or `undefined` if not found.
359
+ */
360
+ get(key: K): V | undefined;
361
+ /**
362
+ * Retrieves an item by its key, throwing an error if it's not found.
363
+ * @param key The key of the item to retrieve.
364
+ * @returns The item.
365
+ * @throws {Error} if no item is found for the given key.
366
+ */
367
+ getOrThrow(key: K): V;
368
+ delete(key: K): boolean;
369
+ /**
370
+ * Clears all registered items from the registry.
371
+ */
372
+ clear(): void;
373
+ }
374
+
375
+ 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, isNull, isNullOrUndefined, isNumber, isPercentageString, 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,
@@ -46,6 +46,7 @@ __export(index_exports, {
46
46
  randId: () => randId,
47
47
  randInt: () => randInt,
48
48
  shuffleArray: () => shuffleArray,
49
+ throwError: () => throwError,
49
50
  throwIf: () => throwIf,
50
51
  throwIfEmpty: () => throwIfEmpty,
51
52
  unique: () => unique
@@ -137,6 +138,9 @@ function throwIfEmpty(value, exceptionMessage) {
137
138
  throw new Error(exceptionMessage);
138
139
  }
139
140
  }
141
+ function throwError(message) {
142
+ throw new Error(message);
143
+ }
140
144
 
141
145
  // src/config.ts
142
146
  var defaultConfig = {
@@ -147,50 +151,6 @@ function configure(newConfig) {
147
151
  Object.assign(axiSettings, newConfig);
148
152
  }
149
153
 
150
- // src/constructor-registry.ts
151
- var ConstructorRegistry = class {
152
- items = /* @__PURE__ */ new Map();
153
- /**
154
- * Registers a constructor with a unique string identifier.
155
- *
156
- * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
157
- * @param ctor - The class constructor to register.
158
- * @returns The registry instance for chainable calls.
159
- * @throws If a constructor with the same `typeId` is already registered.
160
- */
161
- register(typeId, ctor) {
162
- throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);
163
- this.items.set(typeId, ctor);
164
- return this;
165
- }
166
- /**
167
- * Retrieves a constructor by its identifier.
168
- *
169
- * @param typeId - The identifier of the constructor to retrieve.
170
- * @returns The found class constructor.
171
- * @throws If no constructor is found for the given `typeId`.
172
- */
173
- get(typeId) {
174
- const Ctor = this.items.get(typeId);
175
- throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);
176
- return Ctor;
177
- }
178
- /**
179
- * Checks if a constructor for a given identifier is registered.
180
- * @param typeId - The identifier to check.
181
- * @returns `true` if a constructor is registered, otherwise `false`.
182
- */
183
- has(typeId) {
184
- return this.items.has(typeId);
185
- }
186
- /**
187
- * Clears all registered constructors from the registry.
188
- */
189
- clear() {
190
- this.items.clear();
191
- }
192
- };
193
-
194
154
  // src/emitter.ts
195
155
  var Emitter = class {
196
156
  listeners = /* @__PURE__ */ new Set();
@@ -262,10 +222,60 @@ function randInt(min, max) {
262
222
  function randId() {
263
223
  return (0, import_uuid.v4)();
264
224
  }
225
+
226
+ // src/registry.ts
227
+ var Registry = class {
228
+ items = /* @__PURE__ */ new Map();
229
+ /**
230
+ * Registers an item with a specific key.
231
+ * Warns if an item with the same key is already registered.
232
+ * @param key The key to associate with the item.
233
+ * @param value The item to register.
234
+ */
235
+ register(key, value) {
236
+ throwIf(this.items.has(key), `An item with the key '${key}' is already registered and will be overwritten.`);
237
+ this.items.set(key, value);
238
+ }
239
+ /**
240
+ * Checks if an item with the given key is registered.
241
+ * @param key The key to check.
242
+ */
243
+ has(key) {
244
+ return this.items.has(key);
245
+ }
246
+ /**
247
+ * Retrieves an item by its key.
248
+ * @param key The key of the item to retrieve.
249
+ * @returns The item, or `undefined` if not found.
250
+ */
251
+ get(key) {
252
+ return this.items.get(key);
253
+ }
254
+ /**
255
+ * Retrieves an item by its key, throwing an error if it's not found.
256
+ * @param key The key of the item to retrieve.
257
+ * @returns The item.
258
+ * @throws {Error} if no item is found for the given key.
259
+ */
260
+ getOrThrow(key) {
261
+ const item = this.get(key);
262
+ throwIfEmpty(item, `No item registered for the key '${key}'.`);
263
+ return item;
264
+ }
265
+ delete(key) {
266
+ return this.items.delete(key);
267
+ }
268
+ /**
269
+ * Clears all registered items from the registry.
270
+ */
271
+ clear() {
272
+ this.items.clear();
273
+ }
274
+ };
265
275
  // Annotate the CommonJS export names for ESM import in node:
266
276
  0 && (module.exports = {
267
- ConstructorRegistry,
268
277
  Emitter,
278
+ Registry,
269
279
  areArraysEqual,
270
280
  axiSettings,
271
281
  clampNumber,
@@ -290,6 +300,7 @@ function randId() {
290
300
  randId,
291
301
  randInt,
292
302
  shuffleArray,
303
+ throwError,
293
304
  throwIf,
294
305
  throwIfEmpty,
295
306
  unique
package/dist/index.mjs CHANGED
@@ -83,6 +83,9 @@ function throwIfEmpty(value, exceptionMessage) {
83
83
  throw new Error(exceptionMessage);
84
84
  }
85
85
  }
86
+ function throwError(message) {
87
+ throw new Error(message);
88
+ }
86
89
 
87
90
  // src/config.ts
88
91
  var defaultConfig = {
@@ -93,50 +96,6 @@ function configure(newConfig) {
93
96
  Object.assign(axiSettings, newConfig);
94
97
  }
95
98
 
96
- // src/constructor-registry.ts
97
- var ConstructorRegistry = class {
98
- items = /* @__PURE__ */ new Map();
99
- /**
100
- * Registers a constructor with a unique string identifier.
101
- *
102
- * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
103
- * @param ctor - The class constructor to register.
104
- * @returns The registry instance for chainable calls.
105
- * @throws If a constructor with the same `typeId` is already registered.
106
- */
107
- register(typeId, ctor) {
108
- throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);
109
- this.items.set(typeId, ctor);
110
- return this;
111
- }
112
- /**
113
- * Retrieves a constructor by its identifier.
114
- *
115
- * @param typeId - The identifier of the constructor to retrieve.
116
- * @returns The found class constructor.
117
- * @throws If no constructor is found for the given `typeId`.
118
- */
119
- get(typeId) {
120
- const Ctor = this.items.get(typeId);
121
- throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);
122
- return Ctor;
123
- }
124
- /**
125
- * Checks if a constructor for a given identifier is registered.
126
- * @param typeId - The identifier to check.
127
- * @returns `true` if a constructor is registered, otherwise `false`.
128
- */
129
- has(typeId) {
130
- return this.items.has(typeId);
131
- }
132
- /**
133
- * Clears all registered constructors from the registry.
134
- */
135
- clear() {
136
- this.items.clear();
137
- }
138
- };
139
-
140
99
  // src/emitter.ts
141
100
  var Emitter = class {
142
101
  listeners = /* @__PURE__ */ new Set();
@@ -208,9 +167,59 @@ function randInt(min, max) {
208
167
  function randId() {
209
168
  return uuidv4();
210
169
  }
170
+
171
+ // src/registry.ts
172
+ var Registry = class {
173
+ items = /* @__PURE__ */ new Map();
174
+ /**
175
+ * Registers an item with a specific key.
176
+ * Warns if an item with the same key is already registered.
177
+ * @param key The key to associate with the item.
178
+ * @param value The item to register.
179
+ */
180
+ register(key, value) {
181
+ throwIf(this.items.has(key), `An item with the key '${key}' is already registered and will be overwritten.`);
182
+ this.items.set(key, value);
183
+ }
184
+ /**
185
+ * Checks if an item with the given key is registered.
186
+ * @param key The key to check.
187
+ */
188
+ has(key) {
189
+ return this.items.has(key);
190
+ }
191
+ /**
192
+ * Retrieves an item by its key.
193
+ * @param key The key of the item to retrieve.
194
+ * @returns The item, or `undefined` if not found.
195
+ */
196
+ get(key) {
197
+ return this.items.get(key);
198
+ }
199
+ /**
200
+ * Retrieves an item by its key, throwing an error if it's not found.
201
+ * @param key The key of the item to retrieve.
202
+ * @returns The item.
203
+ * @throws {Error} if no item is found for the given key.
204
+ */
205
+ getOrThrow(key) {
206
+ const item = this.get(key);
207
+ throwIfEmpty(item, `No item registered for the key '${key}'.`);
208
+ return item;
209
+ }
210
+ delete(key) {
211
+ return this.items.delete(key);
212
+ }
213
+ /**
214
+ * Clears all registered items from the registry.
215
+ */
216
+ clear() {
217
+ this.items.clear();
218
+ }
219
+ };
211
220
  export {
212
- ConstructorRegistry,
213
221
  Emitter,
222
+ Registry,
214
223
  areArraysEqual,
215
224
  axiSettings,
216
225
  clampNumber,
@@ -235,6 +244,7 @@ export {
235
244
  randId,
236
245
  randInt,
237
246
  shuffleArray,
247
+ throwError,
238
248
  throwIf,
239
249
  throwIfEmpty,
240
250
  unique
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axi-engine/utils",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "Core utility library for Axi Engine, providing common functions for arrays, math, type guards, and more.",
5
5
  "license": "MIT",
6
6
  "repository": {