@axi-engine/utils 0.1.5 → 0.1.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 CHANGED
@@ -6,6 +6,47 @@
6
6
  * ['player', 'stats', 'health']
7
7
  */
8
8
  type PathType = string | string[];
9
+ /**
10
+ * Represents a generic constructor for any class.
11
+ *
12
+ * This utility type is essential for implementing higher-order patterns
13
+ * like mixins, where a function takes a class as an argument and returns
14
+ * a new, extended class.
15
+ *
16
+ * The `...args: any[]` signature allows it to represent constructors
17
+ * with any number and type of arguments, making it universally applicable.
18
+ *
19
+ * @template T - The type of the instance created by the constructor. Defaults to `{}`.
20
+ *
21
+ * @example
22
+ * // Used as a constraint for a base class in a mixin
23
+ * function Timestamped<TBase extends Constructor>(Base: TBase) {
24
+ * return class extends Base {
25
+ * timestamp = new Date();
26
+ * };
27
+ * }
28
+ *
29
+ * class User {}
30
+ * const TimestampedUser = Timestamped(User);
31
+ * const userInstance = new TimestampedUser();
32
+ * console.log(userInstance.timestamp); // Logs the current date
33
+ */
34
+ type Constructor<T = {}> = new (...args: any[]) => T;
35
+ /**
36
+ * Defines the public, read-only contract for an event emitter.
37
+ * It allows subscribing to an event but not emitting it.
38
+ * @template T A tuple representing the types of the event arguments.
39
+ */
40
+ type Subscribable<T extends any[]> = {
41
+ readonly listenerCount: number;
42
+ /**
43
+ * Subscribes a listener to this event.
44
+ * @returns A function to unsubscribe the listener.
45
+ */
46
+ subscribe(listener: (...args: T) => void): () => void;
47
+ unsubscribe(listener: (...args: T) => void): boolean;
48
+ clear(): void;
49
+ };
9
50
 
10
51
  /**
11
52
  * Generates an array of numbers from 0 to length-1.
@@ -120,13 +161,56 @@ declare const axiSettings: AxiEngineConfig;
120
161
  */
121
162
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
122
163
 
164
+ /**
165
+ * A generic registry for mapping string identifiers to class constructors.
166
+ *
167
+ * This utility is fundamental for building extensible systems like dependency injection containers,
168
+ * factories, and serialization engines where types need to be dynamically resolved.
169
+ *
170
+ * @template T - A base type that all registered constructors must produce an instance of.
171
+ */
172
+ declare class ConstructorRegistry<T> {
173
+ private readonly items;
174
+ /**
175
+ * Registers a constructor with a unique string identifier.
176
+ *
177
+ * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
178
+ * @param ctor - The class constructor to register.
179
+ * @returns The registry instance for chainable calls.
180
+ * @throws If a constructor with the same `typeId` is already registered.
181
+ */
182
+ register(typeId: string, ctor: Constructor<T>): this;
183
+ /**
184
+ * Retrieves a constructor by its identifier.
185
+ *
186
+ * @param typeId - The identifier of the constructor to retrieve.
187
+ * @returns The found class constructor.
188
+ * @throws If no constructor is found for the given `typeId`.
189
+ */
190
+ get(typeId: string): Constructor<T>;
191
+ /**
192
+ * Checks if a constructor for a given identifier is registered.
193
+ * @param typeId - The identifier to check.
194
+ * @returns `true` if a constructor is registered, otherwise `false`.
195
+ */
196
+ has(typeId: string): boolean;
197
+ /**
198
+ * Clears all registered constructors from the registry.
199
+ */
200
+ clear(): void;
201
+ }
202
+
123
203
  /**
124
204
  * A minimal, type-safe event emitter for a single event.
125
205
  * It does not manage state, it only manages subscribers and event dispatching.
126
206
  * @template T A tuple representing the types of the event arguments.
127
207
  */
128
- declare class Emitter<T extends any[]> {
208
+ declare class Emitter<T extends any[]> implements Subscribable<T> {
129
209
  private listeners;
210
+ /**
211
+ * Returns the number of listeners.
212
+ */
213
+ get listenerCount(): number;
130
214
  /**
131
215
  * Subscribes a listener to this event.
132
216
  * @returns A function to unsubscribe the listener.
@@ -145,10 +229,6 @@ declare class Emitter<T extends any[]> {
145
229
  * Clears all listeners.
146
230
  */
147
231
  clear(): void;
148
- /**
149
- * Returns the number of listeners.
150
- */
151
- get listenerCount(): number;
152
232
  }
153
233
 
154
234
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
@@ -210,4 +290,4 @@ declare function randInt(min: number, max: number): number;
210
290
  */
211
291
  declare function randId(): string;
212
292
 
213
- export { type AxiEngineConfig, Emitter, type PathType, 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 };
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 };
package/dist/index.d.ts CHANGED
@@ -6,6 +6,47 @@
6
6
  * ['player', 'stats', 'health']
7
7
  */
8
8
  type PathType = string | string[];
9
+ /**
10
+ * Represents a generic constructor for any class.
11
+ *
12
+ * This utility type is essential for implementing higher-order patterns
13
+ * like mixins, where a function takes a class as an argument and returns
14
+ * a new, extended class.
15
+ *
16
+ * The `...args: any[]` signature allows it to represent constructors
17
+ * with any number and type of arguments, making it universally applicable.
18
+ *
19
+ * @template T - The type of the instance created by the constructor. Defaults to `{}`.
20
+ *
21
+ * @example
22
+ * // Used as a constraint for a base class in a mixin
23
+ * function Timestamped<TBase extends Constructor>(Base: TBase) {
24
+ * return class extends Base {
25
+ * timestamp = new Date();
26
+ * };
27
+ * }
28
+ *
29
+ * class User {}
30
+ * const TimestampedUser = Timestamped(User);
31
+ * const userInstance = new TimestampedUser();
32
+ * console.log(userInstance.timestamp); // Logs the current date
33
+ */
34
+ type Constructor<T = {}> = new (...args: any[]) => T;
35
+ /**
36
+ * Defines the public, read-only contract for an event emitter.
37
+ * It allows subscribing to an event but not emitting it.
38
+ * @template T A tuple representing the types of the event arguments.
39
+ */
40
+ type Subscribable<T extends any[]> = {
41
+ readonly listenerCount: number;
42
+ /**
43
+ * Subscribes a listener to this event.
44
+ * @returns A function to unsubscribe the listener.
45
+ */
46
+ subscribe(listener: (...args: T) => void): () => void;
47
+ unsubscribe(listener: (...args: T) => void): boolean;
48
+ clear(): void;
49
+ };
9
50
 
10
51
  /**
11
52
  * Generates an array of numbers from 0 to length-1.
@@ -120,13 +161,56 @@ declare const axiSettings: AxiEngineConfig;
120
161
  */
121
162
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
122
163
 
164
+ /**
165
+ * A generic registry for mapping string identifiers to class constructors.
166
+ *
167
+ * This utility is fundamental for building extensible systems like dependency injection containers,
168
+ * factories, and serialization engines where types need to be dynamically resolved.
169
+ *
170
+ * @template T - A base type that all registered constructors must produce an instance of.
171
+ */
172
+ declare class ConstructorRegistry<T> {
173
+ private readonly items;
174
+ /**
175
+ * Registers a constructor with a unique string identifier.
176
+ *
177
+ * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
178
+ * @param ctor - The class constructor to register.
179
+ * @returns The registry instance for chainable calls.
180
+ * @throws If a constructor with the same `typeId` is already registered.
181
+ */
182
+ register(typeId: string, ctor: Constructor<T>): this;
183
+ /**
184
+ * Retrieves a constructor by its identifier.
185
+ *
186
+ * @param typeId - The identifier of the constructor to retrieve.
187
+ * @returns The found class constructor.
188
+ * @throws If no constructor is found for the given `typeId`.
189
+ */
190
+ get(typeId: string): Constructor<T>;
191
+ /**
192
+ * Checks if a constructor for a given identifier is registered.
193
+ * @param typeId - The identifier to check.
194
+ * @returns `true` if a constructor is registered, otherwise `false`.
195
+ */
196
+ has(typeId: string): boolean;
197
+ /**
198
+ * Clears all registered constructors from the registry.
199
+ */
200
+ clear(): void;
201
+ }
202
+
123
203
  /**
124
204
  * A minimal, type-safe event emitter for a single event.
125
205
  * It does not manage state, it only manages subscribers and event dispatching.
126
206
  * @template T A tuple representing the types of the event arguments.
127
207
  */
128
- declare class Emitter<T extends any[]> {
208
+ declare class Emitter<T extends any[]> implements Subscribable<T> {
129
209
  private listeners;
210
+ /**
211
+ * Returns the number of listeners.
212
+ */
213
+ get listenerCount(): number;
130
214
  /**
131
215
  * Subscribes a listener to this event.
132
216
  * @returns A function to unsubscribe the listener.
@@ -145,10 +229,6 @@ declare class Emitter<T extends any[]> {
145
229
  * Clears all listeners.
146
230
  */
147
231
  clear(): void;
148
- /**
149
- * Returns the number of listeners.
150
- */
151
- get listenerCount(): number;
152
232
  }
153
233
 
154
234
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
@@ -210,4 +290,4 @@ declare function randInt(min: number, max: number): number;
210
290
  */
211
291
  declare function randId(): string;
212
292
 
213
- export { type AxiEngineConfig, Emitter, type PathType, 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 };
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 };
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ 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,
23
24
  Emitter: () => Emitter,
24
25
  areArraysEqual: () => areArraysEqual,
25
26
  axiSettings: () => axiSettings,
@@ -137,10 +138,58 @@ function configure(newConfig) {
137
138
  Object.assign(axiSettings, newConfig);
138
139
  }
139
140
 
141
+ // src/constructor-registry.ts
142
+ var ConstructorRegistry = class {
143
+ items = /* @__PURE__ */ new Map();
144
+ /**
145
+ * Registers a constructor with a unique string identifier.
146
+ *
147
+ * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
148
+ * @param ctor - The class constructor to register.
149
+ * @returns The registry instance for chainable calls.
150
+ * @throws If a constructor with the same `typeId` is already registered.
151
+ */
152
+ register(typeId, ctor) {
153
+ throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);
154
+ this.items.set(typeId, ctor);
155
+ return this;
156
+ }
157
+ /**
158
+ * Retrieves a constructor by its identifier.
159
+ *
160
+ * @param typeId - The identifier of the constructor to retrieve.
161
+ * @returns The found class constructor.
162
+ * @throws If no constructor is found for the given `typeId`.
163
+ */
164
+ get(typeId) {
165
+ const Ctor = this.items.get(typeId);
166
+ throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);
167
+ return Ctor;
168
+ }
169
+ /**
170
+ * Checks if a constructor for a given identifier is registered.
171
+ * @param typeId - The identifier to check.
172
+ * @returns `true` if a constructor is registered, otherwise `false`.
173
+ */
174
+ has(typeId) {
175
+ return this.items.has(typeId);
176
+ }
177
+ /**
178
+ * Clears all registered constructors from the registry.
179
+ */
180
+ clear() {
181
+ this.items.clear();
182
+ }
183
+ };
184
+
140
185
  // src/emitter.ts
141
186
  var Emitter = class {
142
- constructor() {
143
- this.listeners = /* @__PURE__ */ new Set();
187
+ listeners = /* @__PURE__ */ new Set();
188
+ /**
189
+ * Returns the number of listeners.
190
+ */
191
+ get listenerCount() {
192
+ return this.listeners.size;
144
193
  }
145
194
  /**
146
195
  * Subscribes a listener to this event.
@@ -169,12 +218,6 @@ var Emitter = class {
169
218
  clear() {
170
219
  this.listeners.clear();
171
220
  }
172
- /**
173
- * Returns the number of listeners.
174
- */
175
- get listenerCount() {
176
- return this.listeners.size;
177
- }
178
221
  };
179
222
 
180
223
  // src/math.ts
@@ -212,6 +255,7 @@ function randId() {
212
255
  }
213
256
  // Annotate the CommonJS export names for ESM import in node:
214
257
  0 && (module.exports = {
258
+ ConstructorRegistry,
215
259
  Emitter,
216
260
  areArraysEqual,
217
261
  axiSettings,
package/dist/index.mjs CHANGED
@@ -86,10 +86,58 @@ function configure(newConfig) {
86
86
  Object.assign(axiSettings, newConfig);
87
87
  }
88
88
 
89
+ // src/constructor-registry.ts
90
+ var ConstructorRegistry = class {
91
+ items = /* @__PURE__ */ new Map();
92
+ /**
93
+ * Registers a constructor with a unique string identifier.
94
+ *
95
+ * @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
96
+ * @param ctor - The class constructor to register.
97
+ * @returns The registry instance for chainable calls.
98
+ * @throws If a constructor with the same `typeId` is already registered.
99
+ */
100
+ register(typeId, ctor) {
101
+ throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);
102
+ this.items.set(typeId, ctor);
103
+ return this;
104
+ }
105
+ /**
106
+ * Retrieves a constructor by its identifier.
107
+ *
108
+ * @param typeId - The identifier of the constructor to retrieve.
109
+ * @returns The found class constructor.
110
+ * @throws If no constructor is found for the given `typeId`.
111
+ */
112
+ get(typeId) {
113
+ const Ctor = this.items.get(typeId);
114
+ throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);
115
+ return Ctor;
116
+ }
117
+ /**
118
+ * Checks if a constructor for a given identifier is registered.
119
+ * @param typeId - The identifier to check.
120
+ * @returns `true` if a constructor is registered, otherwise `false`.
121
+ */
122
+ has(typeId) {
123
+ return this.items.has(typeId);
124
+ }
125
+ /**
126
+ * Clears all registered constructors from the registry.
127
+ */
128
+ clear() {
129
+ this.items.clear();
130
+ }
131
+ };
132
+
89
133
  // src/emitter.ts
90
134
  var Emitter = class {
91
- constructor() {
92
- this.listeners = /* @__PURE__ */ new Set();
135
+ listeners = /* @__PURE__ */ new Set();
136
+ /**
137
+ * Returns the number of listeners.
138
+ */
139
+ get listenerCount() {
140
+ return this.listeners.size;
93
141
  }
94
142
  /**
95
143
  * Subscribes a listener to this event.
@@ -118,12 +166,6 @@ var Emitter = class {
118
166
  clear() {
119
167
  this.listeners.clear();
120
168
  }
121
- /**
122
- * Returns the number of listeners.
123
- */
124
- get listenerCount() {
125
- return this.listeners.size;
126
- }
127
169
  };
128
170
 
129
171
  // src/math.ts
@@ -160,6 +202,7 @@ function randId() {
160
202
  return uuidv4();
161
203
  }
162
204
  export {
205
+ ConstructorRegistry,
163
206
  Emitter,
164
207
  areArraysEqual,
165
208
  axiSettings,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axi-engine/utils",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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
  "keywords": [