@axi-engine/utils 0.1.7 → 0.1.8

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.js DELETED
@@ -1,285 +0,0 @@
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
- isNullOrUndefined: () => isNullOrUndefined,
38
- isNumber: () => isNumber,
39
- isPercentageString: () => isPercentageString,
40
- isSequentialStart: () => isSequentialStart,
41
- isString: () => isString,
42
- isUndefined: () => isUndefined,
43
- last: () => last,
44
- randId: () => randId,
45
- randInt: () => randInt,
46
- shuffleArray: () => shuffleArray,
47
- throwIf: () => throwIf,
48
- throwIfEmpty: () => throwIfEmpty,
49
- unique: () => unique
50
- });
51
- module.exports = __toCommonJS(index_exports);
52
-
53
- // src/arrays.ts
54
- function genArray(length) {
55
- return Array.from({ length }, (_v, i) => i);
56
- }
57
- function shuffleArray(array) {
58
- const result = [...array];
59
- for (let i = result.length - 1; i > 0; i--) {
60
- const j = Math.floor(Math.random() * (i + 1));
61
- [result[i], result[j]] = [result[j], result[i]];
62
- }
63
- return result;
64
- }
65
- function isSequentialStart(arr1, arr2) {
66
- if (arr1.length > arr2.length) {
67
- return false;
68
- }
69
- return arr1.every((element, index) => element === arr2[index]);
70
- }
71
- function haveSameElements(arr1, arr2) {
72
- if (!arr1 && !arr2) return true;
73
- if (!arr1 || !arr2) return false;
74
- if (arr1.length !== arr2.length) return false;
75
- const sortedArr1 = [...arr1].sort();
76
- const sortedArr2 = [...arr2].sort();
77
- return sortedArr1.every((value, index) => value === sortedArr2[index]);
78
- }
79
- function areArraysEqual(arr1, arr2) {
80
- if (!arr1 && !arr2) return true;
81
- if (!arr1 || !arr2) return false;
82
- if (arr1.length !== arr2.length) return false;
83
- return arr1.every((value, index) => value === arr2[index]);
84
- }
85
- function last(array) {
86
- return array[array.length - 1];
87
- }
88
- function unique(array) {
89
- return [...new Set(array)];
90
- }
91
- function getRandomElement(array) {
92
- if (array.length === 0) {
93
- return void 0;
94
- }
95
- const index = Math.floor(Math.random() * array.length);
96
- return array[index];
97
- }
98
-
99
- // src/guards.ts
100
- function isNullOrUndefined(val) {
101
- return val === void 0 || val === null;
102
- }
103
- function isUndefined(val) {
104
- return typeof val === "undefined";
105
- }
106
- function isNumber(val) {
107
- return typeof val === "number";
108
- }
109
- function isBoolean(val) {
110
- return typeof val === "boolean";
111
- }
112
- function isString(val) {
113
- return typeof val === "string";
114
- }
115
- function isPercentageString(val) {
116
- return typeof val === "string" && val.endsWith("%");
117
- }
118
-
119
- // src/assertion.ts
120
- function throwIf(conditionForThrow, exceptionMessage) {
121
- if (conditionForThrow) {
122
- throw new Error(exceptionMessage);
123
- }
124
- }
125
- function throwIfEmpty(value, exceptionMessage) {
126
- const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
127
- if (isNullOrUndefined(value) || isArrayAndEmpty) {
128
- throw new Error(exceptionMessage);
129
- }
130
- }
131
-
132
- // src/config.ts
133
- var defaultConfig = {
134
- pathSeparator: "/"
135
- };
136
- var axiSettings = { ...defaultConfig };
137
- function configure(newConfig) {
138
- Object.assign(axiSettings, newConfig);
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
-
185
- // src/emitter.ts
186
- var Emitter = class {
187
- listeners = /* @__PURE__ */ new Set();
188
- /**
189
- * Returns the number of listeners.
190
- */
191
- get listenerCount() {
192
- return this.listeners.size;
193
- }
194
- /**
195
- * Subscribes a listener to this event.
196
- * @returns A function to unsubscribe the listener.
197
- */
198
- subscribe(listener) {
199
- this.listeners.add(listener);
200
- return () => this.listeners.delete(listener);
201
- }
202
- /**
203
- * Manually unsubscribe by listener
204
- * @returns returns true if an listener has been removed, or false if the listener does not exist.
205
- */
206
- unsubscribe(listener) {
207
- return this.listeners.delete(listener);
208
- }
209
- /**
210
- * Dispatches the event to all subscribed listeners.
211
- */
212
- emit(...args) {
213
- this.listeners.forEach((listener) => listener(...args));
214
- }
215
- /**
216
- * Clears all listeners.
217
- */
218
- clear() {
219
- this.listeners.clear();
220
- }
221
- };
222
-
223
- // src/math.ts
224
- function clampNumber(val, min, max) {
225
- if (!isNullOrUndefined(min)) val = Math.max(val, min);
226
- if (!isNullOrUndefined(max)) val = Math.min(val, max);
227
- return val;
228
- }
229
- function getPercentOf(val, percents) {
230
- return percents / 100 * val;
231
- }
232
-
233
- // src/misc.ts
234
- function firstKeyOf(obj) {
235
- return Object.keys(obj)[0];
236
- }
237
-
238
- // src/path.ts
239
- function ensurePathArray(path, separator = axiSettings.pathSeparator) {
240
- return Array.isArray(path) ? [...path] : path.split(separator);
241
- }
242
- function ensurePathString(path, separator = axiSettings.pathSeparator) {
243
- return !Array.isArray(path) ? path : path.join(separator);
244
- }
245
-
246
- // src/random.ts
247
- var import_uuid = require("uuid");
248
- function randInt(min, max) {
249
- min = Math.ceil(min);
250
- max = Math.floor(max);
251
- return Math.floor(Math.random() * (max - min) + min);
252
- }
253
- function randId() {
254
- return (0, import_uuid.v4)();
255
- }
256
- // Annotate the CommonJS export names for ESM import in node:
257
- 0 && (module.exports = {
258
- ConstructorRegistry,
259
- Emitter,
260
- areArraysEqual,
261
- axiSettings,
262
- clampNumber,
263
- configure,
264
- ensurePathArray,
265
- ensurePathString,
266
- firstKeyOf,
267
- genArray,
268
- getPercentOf,
269
- getRandomElement,
270
- haveSameElements,
271
- isBoolean,
272
- isNullOrUndefined,
273
- isNumber,
274
- isPercentageString,
275
- isSequentialStart,
276
- isString,
277
- isUndefined,
278
- last,
279
- randId,
280
- randInt,
281
- shuffleArray,
282
- throwIf,
283
- throwIfEmpty,
284
- unique
285
- });