@axi-engine/utils 0.1.9 → 0.2.1

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.
Files changed (51) hide show
  1. package/dist/index.d.mts +13 -1
  2. package/dist/index.d.ts +305 -12
  3. package/dist/index.js +296 -12
  4. package/dist/index.mjs +9 -0
  5. package/package.json +1 -1
  6. package/dist/arrays.d.ts +0 -72
  7. package/dist/arrays.d.ts.map +0 -1
  8. package/dist/arrays.js +0 -115
  9. package/dist/arrays.js.map +0 -1
  10. package/dist/assertion.d.ts +0 -31
  11. package/dist/assertion.d.ts.map +0 -1
  12. package/dist/assertion.js +0 -41
  13. package/dist/assertion.js.map +0 -1
  14. package/dist/config.d.ts +0 -10
  15. package/dist/config.d.ts.map +0 -1
  16. package/dist/config.js +0 -12
  17. package/dist/config.js.map +0 -1
  18. package/dist/constructor-registry.d.ts +0 -40
  19. package/dist/constructor-registry.d.ts.map +0 -1
  20. package/dist/constructor-registry.js +0 -52
  21. package/dist/constructor-registry.js.map +0 -1
  22. package/dist/emitter.d.ts +0 -32
  23. package/dist/emitter.d.ts.map +0 -1
  24. package/dist/emitter.js +0 -43
  25. package/dist/emitter.js.map +0 -1
  26. package/dist/guards.d.ts +0 -12
  27. package/dist/guards.d.ts.map +0 -1
  28. package/dist/guards.js +0 -24
  29. package/dist/guards.js.map +0 -1
  30. package/dist/index.d.ts.map +0 -1
  31. package/dist/index.js.map +0 -1
  32. package/dist/math.d.ts +0 -17
  33. package/dist/math.d.ts.map +0 -1
  34. package/dist/math.js +0 -26
  35. package/dist/math.js.map +0 -1
  36. package/dist/misc.d.ts +0 -7
  37. package/dist/misc.d.ts.map +0 -1
  38. package/dist/misc.js +0 -9
  39. package/dist/misc.js.map +0 -1
  40. package/dist/path.d.ts +0 -10
  41. package/dist/path.d.ts.map +0 -1
  42. package/dist/path.js +0 -14
  43. package/dist/path.js.map +0 -1
  44. package/dist/random.d.ts +0 -14
  45. package/dist/random.d.ts.map +0 -1
  46. package/dist/random.js +0 -21
  47. package/dist/random.js.map +0 -1
  48. package/dist/types.d.ts +0 -50
  49. package/dist/types.d.ts.map +0 -1
  50. package/dist/types.js +0 -2
  51. package/dist/types.js.map +0 -1
package/dist/index.d.mts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Simple scalar type
3
+ *
4
+ */
5
+ type ScalarType = string | number | boolean;
1
6
  /**
2
7
  * Represents a path that can be provided as a single string
3
8
  * or an array of segments.
@@ -231,11 +236,18 @@ declare class Emitter<T extends any[]> implements Subscribable<T> {
231
236
  clear(): void;
232
237
  }
233
238
 
239
+ /**
240
+ * Type guard that checks if a value is a valid ScalarType.
241
+ * @param value The value to check.
242
+ * @returns True if the value is a string, number, or boolean.
243
+ */
244
+ declare function isScalar(value: unknown): value is ScalarType;
234
245
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
235
246
  declare function isUndefined(val: unknown): val is undefined;
236
247
  declare function isNumber(val: unknown): val is number;
237
248
  declare function isBoolean(val: unknown): val is boolean;
238
249
  declare function isString(val: unknown): val is string;
250
+ declare function isNull(val: unknown): val is null;
239
251
  /**
240
252
  * Type guard to check if a value is a string that ends with '%'.
241
253
  * @param val The value to check.
@@ -290,4 +302,4 @@ declare function randInt(min: number, max: number): number;
290
302
  */
291
303
  declare function randId(): string;
292
304
 
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 };
305
+ export { type AxiEngineConfig, type Constructor, ConstructorRegistry, 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 };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,305 @@
1
- export type * from './types';
2
- export * from './arrays';
3
- export * from './assertion';
4
- export * from './config';
5
- export * from './constructor-registry';
6
- export * from './emitter';
7
- export * from './guards';
8
- export * from './math';
9
- export * from './misc';
10
- export * from './path';
11
- export * from './random';
12
- //# sourceMappingURL=index.d.ts.map
1
+ /**
2
+ * Simple scalar type
3
+ *
4
+ */
5
+ type ScalarType = string | number | boolean;
6
+ /**
7
+ * Represents a path that can be provided as a single string
8
+ * or an array of segments.
9
+ * @example
10
+ * 'player/stats/health'
11
+ * ['player', 'stats', 'health']
12
+ */
13
+ type PathType = string | string[];
14
+ /**
15
+ * Represents a generic constructor for any class.
16
+ *
17
+ * This utility type is essential for implementing higher-order patterns
18
+ * like mixins, where a function takes a class as an argument and returns
19
+ * a new, extended class.
20
+ *
21
+ * The `...args: any[]` signature allows it to represent constructors
22
+ * with any number and type of arguments, making it universally applicable.
23
+ *
24
+ * @template T - The type of the instance created by the constructor. Defaults to `{}`.
25
+ *
26
+ * @example
27
+ * // Used as a constraint for a base class in a mixin
28
+ * function Timestamped<TBase extends Constructor>(Base: TBase) {
29
+ * return class extends Base {
30
+ * timestamp = new Date();
31
+ * };
32
+ * }
33
+ *
34
+ * class User {}
35
+ * const TimestampedUser = Timestamped(User);
36
+ * const userInstance = new TimestampedUser();
37
+ * console.log(userInstance.timestamp); // Logs the current date
38
+ */
39
+ type Constructor<T = {}> = new (...args: any[]) => T;
40
+ /**
41
+ * Defines the public, read-only contract for an event emitter.
42
+ * It allows subscribing to an event but not emitting it.
43
+ * @template T A tuple representing the types of the event arguments.
44
+ */
45
+ type Subscribable<T extends any[]> = {
46
+ readonly listenerCount: number;
47
+ /**
48
+ * Subscribes a listener to this event.
49
+ * @returns A function to unsubscribe the listener.
50
+ */
51
+ subscribe(listener: (...args: T) => void): () => void;
52
+ unsubscribe(listener: (...args: T) => void): boolean;
53
+ clear(): void;
54
+ };
55
+
56
+ /**
57
+ * Generates an array of numbers from 0 to length-1.
58
+ * @param length The desired length of the array.
59
+ * @returns An array of sequential numbers.
60
+ * @example genArray(3); // returns [0, 1, 2]
61
+ */
62
+ declare function genArray(length: number): number[];
63
+ /**
64
+ * Creates a new array with its elements shuffled in a random order.
65
+ * This function does not mutate the original array.
66
+ * @template T The type of elements in the array.
67
+ * @param array The array to shuffle.
68
+ * @returns A new, shuffled array.
69
+ */
70
+ declare function shuffleArray<T>(array: T[]): T[];
71
+ /**
72
+ * Checks if the first array is a sequential starting subset of the second array.
73
+ * @param arr1 The potential subset array.
74
+ * @param arr2 The array to check against.
75
+ * @returns `true` if arr1 is a sequential start of arr2, otherwise `false`.
76
+ * @example
77
+ * isSequentialStart([1, 2], [1, 2, 3]); // true
78
+ * isSequentialStart([1, 3], [1, 2, 3]); // false
79
+ */
80
+ declare function isSequentialStart<T>(arr1: T[], arr2: T[]): boolean;
81
+ /**
82
+ * Checks if two arrays contain the same elements, ignoring order.
83
+ * Works for arrays of primitives like strings or numbers.
84
+ * @template T The type of elements in the array.
85
+ * @param arr1 The first array.
86
+ * @param arr2 The second array.
87
+ * @returns `true` if both arrays contain the same elements, otherwise `false`.
88
+ * @example
89
+ * haveSameElements(['a', 'b'], ['b', 'a']); // true
90
+ * haveSameElements([1, 2, 3], [3, 1, 2]); // true
91
+ * haveSameElements(['a', 'b'], ['a', 'c']); // false
92
+ */
93
+ declare function haveSameElements<T>(arr1?: T[], arr2?: T[]): boolean;
94
+ /**
95
+ * Checks if two arrays are strictly equal (same elements in the same order).
96
+ * @template T The type of elements in the array.
97
+ * @param arr1 The first array.
98
+ * @param arr2 The second array.
99
+ * @returns `true` if the arrays are strictly equal, otherwise `false`.
100
+ * @example
101
+ * areArraysEqual(['a', 'b'], ['a', 'b']); // true
102
+ * areArraysEqual(['a', 'b'], ['b', 'a']); // false
103
+ * areArraysEqual([1, 2], [1, 2, 3]); // false
104
+ */
105
+ declare function areArraysEqual<T>(arr1?: T[], arr2?: T[]): boolean;
106
+ /**
107
+ * Gets the last element of an array.
108
+ * @template T The type of elements in the array.
109
+ * @param array The array to query.
110
+ * @returns The last element of the array, or `undefined` if the array is empty.
111
+ */
112
+ declare function last<T>(array: T[]): T | undefined;
113
+ /**
114
+ * Creates a duplicate-free version of an array.
115
+ * @template T The type of elements in the array.
116
+ * @param array The array to process.
117
+ * @returns A new array with only unique elements.
118
+ */
119
+ declare function unique<T>(array: T[]): T[];
120
+ /**
121
+ * Gets a random element from an array.
122
+ * @template T The type of elements in the array.
123
+ * @param array The array to choose from.
124
+ * @returns A random element from the array, or `undefined` if the array is empty.
125
+ */
126
+ declare function getRandomElement<T>(array: T[]): T | undefined;
127
+
128
+ /**
129
+ * Throws an error if the condition is true.
130
+ * @param conditionForThrow - If true, an error will be thrown.
131
+ * @param exceptionMessage - The message for the error.
132
+ * @throws {Error} if the value is true
133
+ */
134
+ declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
135
+ /**
136
+ * Throws an error if the value is null, undefined, or an empty array.
137
+ *
138
+ * @template T The type of the value being checked.
139
+ * @param value The value to check.
140
+ * @param exceptionMessage The message for the error.
141
+ * @throws {Error} if the value is null, undefined, or an empty array.
142
+ *
143
+ * @example
144
+ * // Example with a potentially undefined variable
145
+ * const user: { name: string } | undefined = findUser();
146
+ * throwIfEmpty(user, 'User not found');
147
+ * // From here, TypeScript knows `user` is not undefined.
148
+ * console.log(user.name);
149
+ *
150
+ * @example
151
+ * // Example with an array
152
+ * const items: string[] = getItems();
153
+ * throwIfEmpty(items, 'Items array cannot be empty');
154
+ * // From here, you can safely access items[0] without checking for an empty array again.
155
+ * console.log('First item:', items[0]);
156
+ */
157
+ declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
158
+
159
+ interface AxiEngineConfig {
160
+ pathSeparator: string;
161
+ }
162
+ declare const axiSettings: AxiEngineConfig;
163
+ /**
164
+ * set up global configuration for axi-engine.
165
+ * @param newConfig - configuration object
166
+ */
167
+ declare function configure(newConfig: Partial<AxiEngineConfig>): void;
168
+
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
+ /**
209
+ * A minimal, type-safe event emitter for a single event.
210
+ * It does not manage state, it only manages subscribers and event dispatching.
211
+ * @template T A tuple representing the types of the event arguments.
212
+ */
213
+ declare class Emitter<T extends any[]> implements Subscribable<T> {
214
+ private listeners;
215
+ /**
216
+ * Returns the number of listeners.
217
+ */
218
+ get listenerCount(): number;
219
+ /**
220
+ * Subscribes a listener to this event.
221
+ * @returns A function to unsubscribe the listener.
222
+ */
223
+ subscribe(listener: (...args: T) => void): () => void;
224
+ /**
225
+ * Manually unsubscribe by listener
226
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
227
+ */
228
+ unsubscribe(listener: (...args: T) => void): boolean;
229
+ /**
230
+ * Dispatches the event to all subscribed listeners.
231
+ */
232
+ emit(...args: T): void;
233
+ /**
234
+ * Clears all listeners.
235
+ */
236
+ clear(): void;
237
+ }
238
+
239
+ /**
240
+ * Type guard that checks if a value is a valid ScalarType.
241
+ * @param value The value to check.
242
+ * @returns True if the value is a string, number, or boolean.
243
+ */
244
+ declare function isScalar(value: unknown): value is ScalarType;
245
+ declare function isNullOrUndefined(val: unknown): val is null | undefined;
246
+ declare function isUndefined(val: unknown): val is undefined;
247
+ declare function isNumber(val: unknown): val is number;
248
+ declare function isBoolean(val: unknown): val is boolean;
249
+ declare function isString(val: unknown): val is string;
250
+ declare function isNull(val: unknown): val is null;
251
+ /**
252
+ * Type guard to check if a value is a string that ends with '%'.
253
+ * @param val The value to check.
254
+ * @returns `true` if the value is a percentage string.
255
+ */
256
+ declare function isPercentageString(val: unknown): val is string;
257
+
258
+ /**
259
+ * Clamps a number between an optional minimum and maximum value.
260
+ * @param val The number to clamp.
261
+ * @param min The minimum value. If null or undefined, it's ignored.
262
+ * @param max The maximum value. If null or undefined, it's ignored.
263
+ * @returns The clamped number.
264
+ */
265
+ declare function clampNumber(val: number, min?: number | null, max?: number | null): number;
266
+ /**
267
+ * Calculates a percentage of a given value.
268
+ * @param val The base value.
269
+ * @param percents The percentage to get.
270
+ * @returns The calculated percentage of the value.
271
+ * @example getPercentOf(200, 10); // returns 20
272
+ */
273
+ declare function getPercentOf(val: number, percents: number): number;
274
+
275
+ /**
276
+ * Returns the first key of an object.
277
+ * @param obj The object from which to get the key.
278
+ * @returns The first key of the object as a string.
279
+ */
280
+ declare function firstKeyOf(obj: any): string;
281
+
282
+ /**
283
+ * Ensures that the given path is returned as an array of segments.
284
+ */
285
+ declare function ensurePathArray(path: PathType, separator?: string): string[];
286
+ /**
287
+ * Ensures that the given path is returned as a single string.
288
+ */
289
+ declare function ensurePathString(path: PathType, separator?: string): string;
290
+
291
+ /**
292
+ * Returns a random integer between min (inclusive) and max (exclusive).
293
+ * @param min The minimum integer (inclusive).
294
+ * @param max The maximum integer (exclusive).
295
+ * @returns A random integer.
296
+ * @example randInt(1, 5); // returns 1, 2, 3, or 4
297
+ */
298
+ declare function randInt(min: number, max: number): number;
299
+ /**
300
+ * Generates a unique identifier using uuidv4.
301
+ * @returns A unique string ID.
302
+ */
303
+ declare function randId(): string;
304
+
305
+ export { type AxiEngineConfig, type Constructor, ConstructorRegistry, 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 };
package/dist/index.js CHANGED
@@ -1,12 +1,296 @@
1
- //
2
- export * from './arrays';
3
- export * from './assertion';
4
- export * from './config';
5
- export * from './constructor-registry';
6
- export * from './emitter';
7
- export * from './guards';
8
- export * from './math';
9
- export * from './misc';
10
- export * from './path';
11
- export * from './random';
12
- //# sourceMappingURL=index.js.map
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ ConstructorRegistry: () => ConstructorRegistry,
24
+ Emitter: () => Emitter,
25
+ areArraysEqual: () => areArraysEqual,
26
+ axiSettings: () => axiSettings,
27
+ clampNumber: () => clampNumber,
28
+ configure: () => configure,
29
+ ensurePathArray: () => ensurePathArray,
30
+ ensurePathString: () => ensurePathString,
31
+ firstKeyOf: () => firstKeyOf,
32
+ genArray: () => genArray,
33
+ getPercentOf: () => getPercentOf,
34
+ getRandomElement: () => getRandomElement,
35
+ haveSameElements: () => haveSameElements,
36
+ isBoolean: () => isBoolean,
37
+ isNull: () => isNull,
38
+ isNullOrUndefined: () => isNullOrUndefined,
39
+ isNumber: () => isNumber,
40
+ isPercentageString: () => isPercentageString,
41
+ isScalar: () => isScalar,
42
+ isSequentialStart: () => isSequentialStart,
43
+ isString: () => isString,
44
+ isUndefined: () => isUndefined,
45
+ last: () => last,
46
+ randId: () => randId,
47
+ randInt: () => randInt,
48
+ shuffleArray: () => shuffleArray,
49
+ throwIf: () => throwIf,
50
+ throwIfEmpty: () => throwIfEmpty,
51
+ unique: () => unique
52
+ });
53
+ module.exports = __toCommonJS(index_exports);
54
+
55
+ // src/arrays.ts
56
+ function genArray(length) {
57
+ return Array.from({ length }, (_v, i) => i);
58
+ }
59
+ function shuffleArray(array) {
60
+ const result = [...array];
61
+ for (let i = result.length - 1; i > 0; i--) {
62
+ const j = Math.floor(Math.random() * (i + 1));
63
+ [result[i], result[j]] = [result[j], result[i]];
64
+ }
65
+ return result;
66
+ }
67
+ function isSequentialStart(arr1, arr2) {
68
+ if (arr1.length > arr2.length) {
69
+ return false;
70
+ }
71
+ return arr1.every((element, index) => element === arr2[index]);
72
+ }
73
+ function haveSameElements(arr1, arr2) {
74
+ if (!arr1 && !arr2) return true;
75
+ if (!arr1 || !arr2) return false;
76
+ if (arr1.length !== arr2.length) return false;
77
+ const sortedArr1 = [...arr1].sort();
78
+ const sortedArr2 = [...arr2].sort();
79
+ return sortedArr1.every((value, index) => value === sortedArr2[index]);
80
+ }
81
+ function areArraysEqual(arr1, arr2) {
82
+ if (!arr1 && !arr2) return true;
83
+ if (!arr1 || !arr2) return false;
84
+ if (arr1.length !== arr2.length) return false;
85
+ return arr1.every((value, index) => value === arr2[index]);
86
+ }
87
+ function last(array) {
88
+ return array[array.length - 1];
89
+ }
90
+ function unique(array) {
91
+ return [...new Set(array)];
92
+ }
93
+ function getRandomElement(array) {
94
+ if (array.length === 0) {
95
+ return void 0;
96
+ }
97
+ const index = Math.floor(Math.random() * array.length);
98
+ return array[index];
99
+ }
100
+
101
+ // src/guards.ts
102
+ function isScalar(value) {
103
+ const type = typeof value;
104
+ return type === "string" || type === "number" || type === "boolean";
105
+ }
106
+ function isNullOrUndefined(val) {
107
+ return val === void 0 || val === null;
108
+ }
109
+ function isUndefined(val) {
110
+ return typeof val === "undefined";
111
+ }
112
+ function isNumber(val) {
113
+ return typeof val === "number";
114
+ }
115
+ function isBoolean(val) {
116
+ return typeof val === "boolean";
117
+ }
118
+ function isString(val) {
119
+ return typeof val === "string";
120
+ }
121
+ function isNull(val) {
122
+ return val === null;
123
+ }
124
+ function isPercentageString(val) {
125
+ return typeof val === "string" && val.endsWith("%");
126
+ }
127
+
128
+ // src/assertion.ts
129
+ function throwIf(conditionForThrow, exceptionMessage) {
130
+ if (conditionForThrow) {
131
+ throw new Error(exceptionMessage);
132
+ }
133
+ }
134
+ function throwIfEmpty(value, exceptionMessage) {
135
+ const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
136
+ if (isNullOrUndefined(value) || isArrayAndEmpty) {
137
+ throw new Error(exceptionMessage);
138
+ }
139
+ }
140
+
141
+ // src/config.ts
142
+ var defaultConfig = {
143
+ pathSeparator: "/"
144
+ };
145
+ var axiSettings = { ...defaultConfig };
146
+ function configure(newConfig) {
147
+ Object.assign(axiSettings, newConfig);
148
+ }
149
+
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
+ // src/emitter.ts
195
+ var Emitter = class {
196
+ listeners = /* @__PURE__ */ new Set();
197
+ /**
198
+ * Returns the number of listeners.
199
+ */
200
+ get listenerCount() {
201
+ return this.listeners.size;
202
+ }
203
+ /**
204
+ * Subscribes a listener to this event.
205
+ * @returns A function to unsubscribe the listener.
206
+ */
207
+ subscribe(listener) {
208
+ this.listeners.add(listener);
209
+ return () => this.listeners.delete(listener);
210
+ }
211
+ /**
212
+ * Manually unsubscribe by listener
213
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
214
+ */
215
+ unsubscribe(listener) {
216
+ return this.listeners.delete(listener);
217
+ }
218
+ /**
219
+ * Dispatches the event to all subscribed listeners.
220
+ */
221
+ emit(...args) {
222
+ this.listeners.forEach((listener) => listener(...args));
223
+ }
224
+ /**
225
+ * Clears all listeners.
226
+ */
227
+ clear() {
228
+ this.listeners.clear();
229
+ }
230
+ };
231
+
232
+ // src/math.ts
233
+ function clampNumber(val, min, max) {
234
+ if (!isNullOrUndefined(min)) val = Math.max(val, min);
235
+ if (!isNullOrUndefined(max)) val = Math.min(val, max);
236
+ return val;
237
+ }
238
+ function getPercentOf(val, percents) {
239
+ return percents / 100 * val;
240
+ }
241
+
242
+ // src/misc.ts
243
+ function firstKeyOf(obj) {
244
+ return Object.keys(obj)[0];
245
+ }
246
+
247
+ // src/path.ts
248
+ function ensurePathArray(path, separator = axiSettings.pathSeparator) {
249
+ return Array.isArray(path) ? [...path] : path.split(separator);
250
+ }
251
+ function ensurePathString(path, separator = axiSettings.pathSeparator) {
252
+ return !Array.isArray(path) ? path : path.join(separator);
253
+ }
254
+
255
+ // src/random.ts
256
+ var import_uuid = require("uuid");
257
+ function randInt(min, max) {
258
+ min = Math.ceil(min);
259
+ max = Math.floor(max);
260
+ return Math.floor(Math.random() * (max - min) + min);
261
+ }
262
+ function randId() {
263
+ return (0, import_uuid.v4)();
264
+ }
265
+ // Annotate the CommonJS export names for ESM import in node:
266
+ 0 && (module.exports = {
267
+ ConstructorRegistry,
268
+ Emitter,
269
+ areArraysEqual,
270
+ axiSettings,
271
+ clampNumber,
272
+ configure,
273
+ ensurePathArray,
274
+ ensurePathString,
275
+ firstKeyOf,
276
+ genArray,
277
+ getPercentOf,
278
+ getRandomElement,
279
+ haveSameElements,
280
+ isBoolean,
281
+ isNull,
282
+ isNullOrUndefined,
283
+ isNumber,
284
+ isPercentageString,
285
+ isScalar,
286
+ isSequentialStart,
287
+ isString,
288
+ isUndefined,
289
+ last,
290
+ randId,
291
+ randInt,
292
+ shuffleArray,
293
+ throwIf,
294
+ throwIfEmpty,
295
+ unique
296
+ });
package/dist/index.mjs CHANGED
@@ -45,6 +45,10 @@ function getRandomElement(array) {
45
45
  }
46
46
 
47
47
  // src/guards.ts
48
+ function isScalar(value) {
49
+ const type = typeof value;
50
+ return type === "string" || type === "number" || type === "boolean";
51
+ }
48
52
  function isNullOrUndefined(val) {
49
53
  return val === void 0 || val === null;
50
54
  }
@@ -60,6 +64,9 @@ function isBoolean(val) {
60
64
  function isString(val) {
61
65
  return typeof val === "string";
62
66
  }
67
+ function isNull(val) {
68
+ return val === null;
69
+ }
63
70
  function isPercentageString(val) {
64
71
  return typeof val === "string" && val.endsWith("%");
65
72
  }
@@ -216,9 +223,11 @@ export {
216
223
  getRandomElement,
217
224
  haveSameElements,
218
225
  isBoolean,
226
+ isNull,
219
227
  isNullOrUndefined,
220
228
  isNumber,
221
229
  isPercentageString,
230
+ isScalar,
222
231
  isSequentialStart,
223
232
  isString,
224
233
  isUndefined,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axi-engine/utils",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
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": {