@axi-engine/utils 0.1.6 → 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 +40 -1
- package/dist/index.d.ts +40 -1
- package/dist/index.js +46 -0
- package/dist/index.mjs +45 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -161,6 +161,45 @@ declare const axiSettings: AxiEngineConfig;
|
|
|
161
161
|
*/
|
|
162
162
|
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
163
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
|
+
|
|
164
203
|
/**
|
|
165
204
|
* A minimal, type-safe event emitter for a single event.
|
|
166
205
|
* It does not manage state, it only manages subscribers and event dispatching.
|
|
@@ -251,4 +290,4 @@ declare function randInt(min: number, max: number): number;
|
|
|
251
290
|
*/
|
|
252
291
|
declare function randId(): string;
|
|
253
292
|
|
|
254
|
-
export { type AxiEngineConfig, type Constructor, 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 };
|
|
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
|
@@ -161,6 +161,45 @@ declare const axiSettings: AxiEngineConfig;
|
|
|
161
161
|
*/
|
|
162
162
|
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
163
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
|
+
|
|
164
203
|
/**
|
|
165
204
|
* A minimal, type-safe event emitter for a single event.
|
|
166
205
|
* It does not manage state, it only manages subscribers and event dispatching.
|
|
@@ -251,4 +290,4 @@ declare function randInt(min: number, max: number): number;
|
|
|
251
290
|
*/
|
|
252
291
|
declare function randId(): string;
|
|
253
292
|
|
|
254
|
-
export { type AxiEngineConfig, type Constructor, 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 };
|
|
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,6 +138,50 @@ 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
187
|
listeners = /* @__PURE__ */ new Set();
|
|
@@ -210,6 +255,7 @@ function randId() {
|
|
|
210
255
|
}
|
|
211
256
|
// Annotate the CommonJS export names for ESM import in node:
|
|
212
257
|
0 && (module.exports = {
|
|
258
|
+
ConstructorRegistry,
|
|
213
259
|
Emitter,
|
|
214
260
|
areArraysEqual,
|
|
215
261
|
axiSettings,
|
package/dist/index.mjs
CHANGED
|
@@ -86,6 +86,50 @@ 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
135
|
listeners = /* @__PURE__ */ new Set();
|
|
@@ -158,6 +202,7 @@ function randId() {
|
|
|
158
202
|
return uuidv4();
|
|
159
203
|
}
|
|
160
204
|
export {
|
|
205
|
+
ConstructorRegistry,
|
|
161
206
|
Emitter,
|
|
162
207
|
areArraysEqual,
|
|
163
208
|
axiSettings,
|