@axi-engine/utils 0.1.4 → 0.1.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/README.md CHANGED
@@ -30,7 +30,7 @@ const isSame = haveSameElements(['a', 'b'], ['b', 'a']); // Returns true
30
30
  ```
31
31
 
32
32
 
33
- API Reference
33
+ ## API Reference
34
34
 
35
35
  Will be available when code and repository will be fully published
36
36
 
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,6 +161,37 @@ declare const axiSettings: AxiEngineConfig;
120
161
  */
121
162
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
122
163
 
164
+ /**
165
+ * A minimal, type-safe event emitter for a single event.
166
+ * It does not manage state, it only manages subscribers and event dispatching.
167
+ * @template T A tuple representing the types of the event arguments.
168
+ */
169
+ declare class Emitter<T extends any[]> implements Subscribable<T> {
170
+ private listeners;
171
+ /**
172
+ * Returns the number of listeners.
173
+ */
174
+ get listenerCount(): number;
175
+ /**
176
+ * Subscribes a listener to this event.
177
+ * @returns A function to unsubscribe the listener.
178
+ */
179
+ subscribe(listener: (...args: T) => void): () => void;
180
+ /**
181
+ * Manually unsubscribe by listener
182
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
183
+ */
184
+ unsubscribe(listener: (...args: T) => void): boolean;
185
+ /**
186
+ * Dispatches the event to all subscribed listeners.
187
+ */
188
+ emit(...args: T): void;
189
+ /**
190
+ * Clears all listeners.
191
+ */
192
+ clear(): void;
193
+ }
194
+
123
195
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
124
196
  declare function isUndefined(val: unknown): val is undefined;
125
197
  declare function isNumber(val: unknown): val is number;
@@ -179,4 +251,4 @@ declare function randInt(min: number, max: number): number;
179
251
  */
180
252
  declare function randId(): string;
181
253
 
182
- export { type AxiEngineConfig, 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 };
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 };
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,6 +161,37 @@ declare const axiSettings: AxiEngineConfig;
120
161
  */
121
162
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
122
163
 
164
+ /**
165
+ * A minimal, type-safe event emitter for a single event.
166
+ * It does not manage state, it only manages subscribers and event dispatching.
167
+ * @template T A tuple representing the types of the event arguments.
168
+ */
169
+ declare class Emitter<T extends any[]> implements Subscribable<T> {
170
+ private listeners;
171
+ /**
172
+ * Returns the number of listeners.
173
+ */
174
+ get listenerCount(): number;
175
+ /**
176
+ * Subscribes a listener to this event.
177
+ * @returns A function to unsubscribe the listener.
178
+ */
179
+ subscribe(listener: (...args: T) => void): () => void;
180
+ /**
181
+ * Manually unsubscribe by listener
182
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
183
+ */
184
+ unsubscribe(listener: (...args: T) => void): boolean;
185
+ /**
186
+ * Dispatches the event to all subscribed listeners.
187
+ */
188
+ emit(...args: T): void;
189
+ /**
190
+ * Clears all listeners.
191
+ */
192
+ clear(): void;
193
+ }
194
+
123
195
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
124
196
  declare function isUndefined(val: unknown): val is undefined;
125
197
  declare function isNumber(val: unknown): val is number;
@@ -179,4 +251,4 @@ declare function randInt(min: number, max: number): number;
179
251
  */
180
252
  declare function randId(): string;
181
253
 
182
- export { type AxiEngineConfig, 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 };
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 };
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
+ Emitter: () => Emitter,
23
24
  areArraysEqual: () => areArraysEqual,
24
25
  axiSettings: () => axiSettings,
25
26
  clampNumber: () => clampNumber,
@@ -136,6 +137,44 @@ function configure(newConfig) {
136
137
  Object.assign(axiSettings, newConfig);
137
138
  }
138
139
 
140
+ // src/emitter.ts
141
+ var Emitter = class {
142
+ listeners = /* @__PURE__ */ new Set();
143
+ /**
144
+ * Returns the number of listeners.
145
+ */
146
+ get listenerCount() {
147
+ return this.listeners.size;
148
+ }
149
+ /**
150
+ * Subscribes a listener to this event.
151
+ * @returns A function to unsubscribe the listener.
152
+ */
153
+ subscribe(listener) {
154
+ this.listeners.add(listener);
155
+ return () => this.listeners.delete(listener);
156
+ }
157
+ /**
158
+ * Manually unsubscribe by listener
159
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
160
+ */
161
+ unsubscribe(listener) {
162
+ return this.listeners.delete(listener);
163
+ }
164
+ /**
165
+ * Dispatches the event to all subscribed listeners.
166
+ */
167
+ emit(...args) {
168
+ this.listeners.forEach((listener) => listener(...args));
169
+ }
170
+ /**
171
+ * Clears all listeners.
172
+ */
173
+ clear() {
174
+ this.listeners.clear();
175
+ }
176
+ };
177
+
139
178
  // src/math.ts
140
179
  function clampNumber(val, min, max) {
141
180
  if (!isNullOrUndefined(min)) val = Math.max(val, min);
@@ -171,6 +210,7 @@ function randId() {
171
210
  }
172
211
  // Annotate the CommonJS export names for ESM import in node:
173
212
  0 && (module.exports = {
213
+ Emitter,
174
214
  areArraysEqual,
175
215
  axiSettings,
176
216
  clampNumber,
package/dist/index.mjs CHANGED
@@ -86,6 +86,44 @@ function configure(newConfig) {
86
86
  Object.assign(axiSettings, newConfig);
87
87
  }
88
88
 
89
+ // src/emitter.ts
90
+ var Emitter = class {
91
+ listeners = /* @__PURE__ */ new Set();
92
+ /**
93
+ * Returns the number of listeners.
94
+ */
95
+ get listenerCount() {
96
+ return this.listeners.size;
97
+ }
98
+ /**
99
+ * Subscribes a listener to this event.
100
+ * @returns A function to unsubscribe the listener.
101
+ */
102
+ subscribe(listener) {
103
+ this.listeners.add(listener);
104
+ return () => this.listeners.delete(listener);
105
+ }
106
+ /**
107
+ * Manually unsubscribe by listener
108
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
109
+ */
110
+ unsubscribe(listener) {
111
+ return this.listeners.delete(listener);
112
+ }
113
+ /**
114
+ * Dispatches the event to all subscribed listeners.
115
+ */
116
+ emit(...args) {
117
+ this.listeners.forEach((listener) => listener(...args));
118
+ }
119
+ /**
120
+ * Clears all listeners.
121
+ */
122
+ clear() {
123
+ this.listeners.clear();
124
+ }
125
+ };
126
+
89
127
  // src/math.ts
90
128
  function clampNumber(val, min, max) {
91
129
  if (!isNullOrUndefined(min)) val = Math.max(val, min);
@@ -120,6 +158,7 @@ function randId() {
120
158
  return uuidv4();
121
159
  }
122
160
  export {
161
+ Emitter,
123
162
  areArraysEqual,
124
163
  axiSettings,
125
164
  clampNumber,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axi-engine/utils",
3
- "version": "0.1.4",
3
+ "version": "0.1.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
  "keywords": [
@@ -9,14 +9,14 @@
9
9
  "gamedev",
10
10
  "utils"
11
11
  ],
12
+ "types": "./dist/index.d.ts",
12
13
  "main": "./dist/index.js",
13
14
  "module": "./dist/index.mjs",
14
- "types": "./dist/index.d.ts",
15
15
  "exports": {
16
16
  ".": {
17
+ "types": "./dist/index.d.ts",
17
18
  "import": "./dist/index.mjs",
18
- "require": "./dist/index.js",
19
- "types": "./dist/index.d.ts"
19
+ "require": "./dist/index.js"
20
20
  }
21
21
  },
22
22
  "scripts": {