@axi-engine/utils 0.1.4 → 0.1.5

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
@@ -120,6 +120,37 @@ declare const axiSettings: AxiEngineConfig;
120
120
  */
121
121
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
122
122
 
123
+ /**
124
+ * A minimal, type-safe event emitter for a single event.
125
+ * It does not manage state, it only manages subscribers and event dispatching.
126
+ * @template T A tuple representing the types of the event arguments.
127
+ */
128
+ declare class Emitter<T extends any[]> {
129
+ private listeners;
130
+ /**
131
+ * Subscribes a listener to this event.
132
+ * @returns A function to unsubscribe the listener.
133
+ */
134
+ subscribe(listener: (...args: T) => void): () => void;
135
+ /**
136
+ * Manually unsubscribe by listener
137
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
138
+ */
139
+ unsubscribe(listener: (...args: T) => void): boolean;
140
+ /**
141
+ * Dispatches the event to all subscribed listeners.
142
+ */
143
+ emit(...args: T): void;
144
+ /**
145
+ * Clears all listeners.
146
+ */
147
+ clear(): void;
148
+ /**
149
+ * Returns the number of listeners.
150
+ */
151
+ get listenerCount(): number;
152
+ }
153
+
123
154
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
124
155
  declare function isUndefined(val: unknown): val is undefined;
125
156
  declare function isNumber(val: unknown): val is number;
@@ -179,4 +210,4 @@ declare function randInt(min: number, max: number): number;
179
210
  */
180
211
  declare function randId(): string;
181
212
 
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 };
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 };
package/dist/index.d.ts CHANGED
@@ -120,6 +120,37 @@ declare const axiSettings: AxiEngineConfig;
120
120
  */
121
121
  declare function configure(newConfig: Partial<AxiEngineConfig>): void;
122
122
 
123
+ /**
124
+ * A minimal, type-safe event emitter for a single event.
125
+ * It does not manage state, it only manages subscribers and event dispatching.
126
+ * @template T A tuple representing the types of the event arguments.
127
+ */
128
+ declare class Emitter<T extends any[]> {
129
+ private listeners;
130
+ /**
131
+ * Subscribes a listener to this event.
132
+ * @returns A function to unsubscribe the listener.
133
+ */
134
+ subscribe(listener: (...args: T) => void): () => void;
135
+ /**
136
+ * Manually unsubscribe by listener
137
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
138
+ */
139
+ unsubscribe(listener: (...args: T) => void): boolean;
140
+ /**
141
+ * Dispatches the event to all subscribed listeners.
142
+ */
143
+ emit(...args: T): void;
144
+ /**
145
+ * Clears all listeners.
146
+ */
147
+ clear(): void;
148
+ /**
149
+ * Returns the number of listeners.
150
+ */
151
+ get listenerCount(): number;
152
+ }
153
+
123
154
  declare function isNullOrUndefined(val: unknown): val is null | undefined;
124
155
  declare function isUndefined(val: unknown): val is undefined;
125
156
  declare function isNumber(val: unknown): val is number;
@@ -179,4 +210,4 @@ declare function randInt(min: number, max: number): number;
179
210
  */
180
211
  declare function randId(): string;
181
212
 
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 };
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 };
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,46 @@ function configure(newConfig) {
136
137
  Object.assign(axiSettings, newConfig);
137
138
  }
138
139
 
140
+ // src/emitter.ts
141
+ var Emitter = class {
142
+ constructor() {
143
+ this.listeners = /* @__PURE__ */ new Set();
144
+ }
145
+ /**
146
+ * Subscribes a listener to this event.
147
+ * @returns A function to unsubscribe the listener.
148
+ */
149
+ subscribe(listener) {
150
+ this.listeners.add(listener);
151
+ return () => this.listeners.delete(listener);
152
+ }
153
+ /**
154
+ * Manually unsubscribe by listener
155
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
156
+ */
157
+ unsubscribe(listener) {
158
+ return this.listeners.delete(listener);
159
+ }
160
+ /**
161
+ * Dispatches the event to all subscribed listeners.
162
+ */
163
+ emit(...args) {
164
+ this.listeners.forEach((listener) => listener(...args));
165
+ }
166
+ /**
167
+ * Clears all listeners.
168
+ */
169
+ clear() {
170
+ this.listeners.clear();
171
+ }
172
+ /**
173
+ * Returns the number of listeners.
174
+ */
175
+ get listenerCount() {
176
+ return this.listeners.size;
177
+ }
178
+ };
179
+
139
180
  // src/math.ts
140
181
  function clampNumber(val, min, max) {
141
182
  if (!isNullOrUndefined(min)) val = Math.max(val, min);
@@ -171,6 +212,7 @@ function randId() {
171
212
  }
172
213
  // Annotate the CommonJS export names for ESM import in node:
173
214
  0 && (module.exports = {
215
+ Emitter,
174
216
  areArraysEqual,
175
217
  axiSettings,
176
218
  clampNumber,
package/dist/index.mjs CHANGED
@@ -86,6 +86,46 @@ function configure(newConfig) {
86
86
  Object.assign(axiSettings, newConfig);
87
87
  }
88
88
 
89
+ // src/emitter.ts
90
+ var Emitter = class {
91
+ constructor() {
92
+ this.listeners = /* @__PURE__ */ new Set();
93
+ }
94
+ /**
95
+ * Subscribes a listener to this event.
96
+ * @returns A function to unsubscribe the listener.
97
+ */
98
+ subscribe(listener) {
99
+ this.listeners.add(listener);
100
+ return () => this.listeners.delete(listener);
101
+ }
102
+ /**
103
+ * Manually unsubscribe by listener
104
+ * @returns returns true if an listener has been removed, or false if the listener does not exist.
105
+ */
106
+ unsubscribe(listener) {
107
+ return this.listeners.delete(listener);
108
+ }
109
+ /**
110
+ * Dispatches the event to all subscribed listeners.
111
+ */
112
+ emit(...args) {
113
+ this.listeners.forEach((listener) => listener(...args));
114
+ }
115
+ /**
116
+ * Clears all listeners.
117
+ */
118
+ clear() {
119
+ this.listeners.clear();
120
+ }
121
+ /**
122
+ * Returns the number of listeners.
123
+ */
124
+ get listenerCount() {
125
+ return this.listeners.size;
126
+ }
127
+ };
128
+
89
129
  // src/math.ts
90
130
  function clampNumber(val, min, max) {
91
131
  if (!isNullOrUndefined(min)) val = Math.max(val, min);
@@ -120,6 +160,7 @@ function randId() {
120
160
  return uuidv4();
121
161
  }
122
162
  export {
163
+ Emitter,
123
164
  areArraysEqual,
124
165
  axiSettings,
125
166
  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.5",
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": {