@axi-engine/utils 0.2.9 → 0.2.10

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 CHANGED
@@ -37,6 +37,16 @@ type PathType = string | string[];
37
37
  * console.log(userInstance.timestamp); // Logs the current date
38
38
  */
39
39
  type Constructor<T = {}> = new (...args: any[]) => T;
40
+ /**
41
+ * Represents an object that has a lifecycle and requires explicit destruction.
42
+ */
43
+ interface Destroyable {
44
+ /**
45
+ * Destroys the object, releasing all held resources.
46
+ * After calling this, the object should be considered unusable.
47
+ */
48
+ destroy(): void;
49
+ }
40
50
  /**
41
51
  * Describes an object that can be unsubscribed from.
42
52
  */
@@ -167,6 +177,7 @@ declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts va
167
177
  */
168
178
  declare function throwError(message: string): never;
169
179
 
180
+ /** todo: rename to 'utils config' */
170
181
  interface AxiEngineConfig {
171
182
  pathSeparator: string;
172
183
  }
@@ -231,12 +242,16 @@ interface DataSink {
231
242
  * @param path The path to the value to be deleted.
232
243
  */
233
244
  delete(path: PathType): void;
245
+ /**
246
+ * Deletes all values
247
+ */
248
+ clear(): void;
234
249
  }
235
250
  /**
236
251
  * A full CRUD contract for systems that provide complete data management.
237
252
  * Combines both reading and writing capabilities.
238
253
  */
239
- interface DataStorage extends DataSource, DataSink {
254
+ interface DataStorage extends DataSource, DataSink, Destroyable {
240
255
  }
241
256
 
242
257
  /**
@@ -303,6 +318,7 @@ declare class Emitter<T extends any[]> implements Subscribable<T> {
303
318
  */
304
319
  clear(): void;
305
320
  }
321
+
306
322
  /**
307
323
  * An Emitter that stores the last emitted value.
308
324
  * New subscribers immediately receive the last value upon subscription.
@@ -414,15 +430,18 @@ declare function clampNumber(val: number, min?: number | null, max?: number | nu
414
430
  * Calculates a percentage of a given value.
415
431
  * @param val The base value.
416
432
  * @param percents The percentage to get.
433
+ * @param precision Optional number of decimal places to round the result to.
417
434
  * @returns The calculated percentage of the value.
418
- * @example getPercentOf(200, 10); // returns 20
435
+ * @example getPercentOf(200, 12.5); // returns 25
436
+ * @example getPercentOf(100, 33.333, 2); // returns 33.33
419
437
  */
420
- declare function getPercentOf(val: number, percents: number): number;
438
+ declare function getPercentOf(val: number, percents: number, precision?: number): number;
421
439
 
422
440
  /**
423
441
  * Returns the first key of an object.
424
442
  * @param obj The object from which to get the key.
425
443
  * @returns The first key of the object as a string.
444
+ * @throws {Error} If the argument is not a valid object.
426
445
  */
427
446
  declare function firstKeyOf(obj: any): string;
428
447
 
@@ -447,7 +466,7 @@ declare function randInt(min: number, max: number): number;
447
466
  * Generates a unique identifier using uuidv4.
448
467
  * @returns A unique string ID.
449
468
  */
450
- declare function randId(): string;
469
+ declare function uid(): string;
451
470
 
452
471
  /**
453
472
  * A generic, type-safe wrapper around a Map for managing collections of items by key.
@@ -491,4 +510,4 @@ declare class Registry<K extends PropertyKey, V> {
491
510
  clear(): void;
492
511
  }
493
512
 
494
- export { type AxiEngineConfig, type Constructor, type DataSink, type DataSource, type DataStorage, Emitter, type PathType, Registry, type ScalarType, StateEmitter, type Subscribable, Subscription, type Unsubscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isPercentageString, isPromise, isScalar, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwError, throwIf, throwIfEmpty, unique };
513
+ export { type AxiEngineConfig, type Constructor, type DataSink, type DataSource, type DataStorage, type Destroyable, Emitter, type PathType, Registry, type ScalarType, StateEmitter, type Subscribable, Subscription, type Unsubscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isPercentageString, isPromise, isScalar, isSequentialStart, isString, isUndefined, last, randInt, shuffleArray, throwError, throwIf, throwIfEmpty, uid, unique };
package/dist/index.d.ts CHANGED
@@ -37,6 +37,16 @@ type PathType = string | string[];
37
37
  * console.log(userInstance.timestamp); // Logs the current date
38
38
  */
39
39
  type Constructor<T = {}> = new (...args: any[]) => T;
40
+ /**
41
+ * Represents an object that has a lifecycle and requires explicit destruction.
42
+ */
43
+ interface Destroyable {
44
+ /**
45
+ * Destroys the object, releasing all held resources.
46
+ * After calling this, the object should be considered unusable.
47
+ */
48
+ destroy(): void;
49
+ }
40
50
  /**
41
51
  * Describes an object that can be unsubscribed from.
42
52
  */
@@ -167,6 +177,7 @@ declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts va
167
177
  */
168
178
  declare function throwError(message: string): never;
169
179
 
180
+ /** todo: rename to 'utils config' */
170
181
  interface AxiEngineConfig {
171
182
  pathSeparator: string;
172
183
  }
@@ -231,12 +242,16 @@ interface DataSink {
231
242
  * @param path The path to the value to be deleted.
232
243
  */
233
244
  delete(path: PathType): void;
245
+ /**
246
+ * Deletes all values
247
+ */
248
+ clear(): void;
234
249
  }
235
250
  /**
236
251
  * A full CRUD contract for systems that provide complete data management.
237
252
  * Combines both reading and writing capabilities.
238
253
  */
239
- interface DataStorage extends DataSource, DataSink {
254
+ interface DataStorage extends DataSource, DataSink, Destroyable {
240
255
  }
241
256
 
242
257
  /**
@@ -303,6 +318,7 @@ declare class Emitter<T extends any[]> implements Subscribable<T> {
303
318
  */
304
319
  clear(): void;
305
320
  }
321
+
306
322
  /**
307
323
  * An Emitter that stores the last emitted value.
308
324
  * New subscribers immediately receive the last value upon subscription.
@@ -414,15 +430,18 @@ declare function clampNumber(val: number, min?: number | null, max?: number | nu
414
430
  * Calculates a percentage of a given value.
415
431
  * @param val The base value.
416
432
  * @param percents The percentage to get.
433
+ * @param precision Optional number of decimal places to round the result to.
417
434
  * @returns The calculated percentage of the value.
418
- * @example getPercentOf(200, 10); // returns 20
435
+ * @example getPercentOf(200, 12.5); // returns 25
436
+ * @example getPercentOf(100, 33.333, 2); // returns 33.33
419
437
  */
420
- declare function getPercentOf(val: number, percents: number): number;
438
+ declare function getPercentOf(val: number, percents: number, precision?: number): number;
421
439
 
422
440
  /**
423
441
  * Returns the first key of an object.
424
442
  * @param obj The object from which to get the key.
425
443
  * @returns The first key of the object as a string.
444
+ * @throws {Error} If the argument is not a valid object.
426
445
  */
427
446
  declare function firstKeyOf(obj: any): string;
428
447
 
@@ -447,7 +466,7 @@ declare function randInt(min: number, max: number): number;
447
466
  * Generates a unique identifier using uuidv4.
448
467
  * @returns A unique string ID.
449
468
  */
450
- declare function randId(): string;
469
+ declare function uid(): string;
451
470
 
452
471
  /**
453
472
  * A generic, type-safe wrapper around a Map for managing collections of items by key.
@@ -491,4 +510,4 @@ declare class Registry<K extends PropertyKey, V> {
491
510
  clear(): void;
492
511
  }
493
512
 
494
- export { type AxiEngineConfig, type Constructor, type DataSink, type DataSource, type DataStorage, Emitter, type PathType, Registry, type ScalarType, StateEmitter, type Subscribable, Subscription, type Unsubscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isPercentageString, isPromise, isScalar, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwError, throwIf, throwIfEmpty, unique };
513
+ export { type AxiEngineConfig, type Constructor, type DataSink, type DataSource, type DataStorage, type Destroyable, Emitter, type PathType, Registry, type ScalarType, StateEmitter, type Subscribable, Subscription, type Unsubscribable, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isPercentageString, isPromise, isScalar, isSequentialStart, isString, isUndefined, last, randInt, shuffleArray, throwError, throwIf, throwIfEmpty, uid, unique };
package/dist/index.js CHANGED
@@ -48,12 +48,12 @@ __export(index_exports, {
48
48
  isString: () => isString,
49
49
  isUndefined: () => isUndefined,
50
50
  last: () => last,
51
- randId: () => randId,
52
51
  randInt: () => randInt,
53
52
  shuffleArray: () => shuffleArray,
54
53
  throwError: () => throwError,
55
54
  throwIf: () => throwIf,
56
55
  throwIfEmpty: () => throwIfEmpty,
56
+ uid: () => uid,
57
57
  unique: () => unique
58
58
  });
59
59
  module.exports = __toCommonJS(index_exports);
@@ -137,7 +137,7 @@ function isPromise(value) {
137
137
  return value != null && typeof value.then === "function";
138
138
  }
139
139
  function isPercentageString(val) {
140
- return typeof val === "string" && val.endsWith("%");
140
+ return isString(val) && val.endsWith("%");
141
141
  }
142
142
 
143
143
  // src/assertion.ts
@@ -147,10 +147,10 @@ function throwIf(condition, exceptionMessage) {
147
147
  }
148
148
  }
149
149
  function throwIfEmpty(value, exceptionMessage) {
150
- const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
151
- if (isNullOrUndefined(value) || isArrayAndEmpty) {
152
- throw new Error(exceptionMessage);
153
- }
150
+ throwIf(
151
+ isNullOrUndefined(value) || Array.isArray(value) && !value.length,
152
+ exceptionMessage
153
+ );
154
154
  }
155
155
  function throwError(message) {
156
156
  throw new Error(message);
@@ -158,7 +158,7 @@ function throwError(message) {
158
158
 
159
159
  // src/config.ts
160
160
  var defaultConfig = {
161
- pathSeparator: "/"
161
+ pathSeparator: "."
162
162
  };
163
163
  var axiSettings = { ...defaultConfig };
164
164
  function configure(newConfig) {
@@ -207,7 +207,7 @@ var Subscription = class {
207
207
  }
208
208
  };
209
209
 
210
- // src/emitter.ts
210
+ // src/emitters/emitter.ts
211
211
  var Emitter = class {
212
212
  listeners = /* @__PURE__ */ new Set();
213
213
  /**
@@ -256,6 +256,8 @@ var Emitter = class {
256
256
  this.listeners.clear();
257
257
  }
258
258
  };
259
+
260
+ // src/emitters/state-emitter.ts
259
261
  var StateEmitter = class extends Emitter {
260
262
  _lastValue;
261
263
  /**
@@ -303,12 +305,17 @@ function clampNumber(val, min, max) {
303
305
  if (!isNullOrUndefined(max)) val = Math.min(val, max);
304
306
  return val;
305
307
  }
306
- function getPercentOf(val, percents) {
307
- return percents / 100 * val;
308
+ function getPercentOf(val, percents, precision) {
309
+ const result = percents / 100 * val;
310
+ if (!isUndefined(precision)) {
311
+ return Number(result.toFixed(precision));
312
+ }
313
+ return result;
308
314
  }
309
315
 
310
316
  // src/misc.ts
311
317
  function firstKeyOf(obj) {
318
+ throwIf(!isObject(obj), `firstKeyOf: Expected an object, got ${typeof obj}`);
312
319
  return Object.keys(obj)[0];
313
320
  }
314
321
 
@@ -327,7 +334,7 @@ function randInt(min, max) {
327
334
  max = Math.floor(max);
328
335
  return Math.floor(Math.random() * (max - min) + min);
329
336
  }
330
- function randId() {
337
+ function uid() {
331
338
  return (0, import_uuid.v4)();
332
339
  }
333
340
 
@@ -410,11 +417,11 @@ var Registry = class {
410
417
  isString,
411
418
  isUndefined,
412
419
  last,
413
- randId,
414
420
  randInt,
415
421
  shuffleArray,
416
422
  throwError,
417
423
  throwIf,
418
424
  throwIfEmpty,
425
+ uid,
419
426
  unique
420
427
  });
package/dist/index.mjs CHANGED
@@ -77,7 +77,7 @@ function isPromise(value) {
77
77
  return value != null && typeof value.then === "function";
78
78
  }
79
79
  function isPercentageString(val) {
80
- return typeof val === "string" && val.endsWith("%");
80
+ return isString(val) && val.endsWith("%");
81
81
  }
82
82
 
83
83
  // src/assertion.ts
@@ -87,10 +87,10 @@ function throwIf(condition, exceptionMessage) {
87
87
  }
88
88
  }
89
89
  function throwIfEmpty(value, exceptionMessage) {
90
- const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
91
- if (isNullOrUndefined(value) || isArrayAndEmpty) {
92
- throw new Error(exceptionMessage);
93
- }
90
+ throwIf(
91
+ isNullOrUndefined(value) || Array.isArray(value) && !value.length,
92
+ exceptionMessage
93
+ );
94
94
  }
95
95
  function throwError(message) {
96
96
  throw new Error(message);
@@ -98,7 +98,7 @@ function throwError(message) {
98
98
 
99
99
  // src/config.ts
100
100
  var defaultConfig = {
101
- pathSeparator: "/"
101
+ pathSeparator: "."
102
102
  };
103
103
  var axiSettings = { ...defaultConfig };
104
104
  function configure(newConfig) {
@@ -147,7 +147,7 @@ var Subscription = class {
147
147
  }
148
148
  };
149
149
 
150
- // src/emitter.ts
150
+ // src/emitters/emitter.ts
151
151
  var Emitter = class {
152
152
  listeners = /* @__PURE__ */ new Set();
153
153
  /**
@@ -196,6 +196,8 @@ var Emitter = class {
196
196
  this.listeners.clear();
197
197
  }
198
198
  };
199
+
200
+ // src/emitters/state-emitter.ts
199
201
  var StateEmitter = class extends Emitter {
200
202
  _lastValue;
201
203
  /**
@@ -243,12 +245,17 @@ function clampNumber(val, min, max) {
243
245
  if (!isNullOrUndefined(max)) val = Math.min(val, max);
244
246
  return val;
245
247
  }
246
- function getPercentOf(val, percents) {
247
- return percents / 100 * val;
248
+ function getPercentOf(val, percents, precision) {
249
+ const result = percents / 100 * val;
250
+ if (!isUndefined(precision)) {
251
+ return Number(result.toFixed(precision));
252
+ }
253
+ return result;
248
254
  }
249
255
 
250
256
  // src/misc.ts
251
257
  function firstKeyOf(obj) {
258
+ throwIf(!isObject(obj), `firstKeyOf: Expected an object, got ${typeof obj}`);
252
259
  return Object.keys(obj)[0];
253
260
  }
254
261
 
@@ -267,7 +274,7 @@ function randInt(min, max) {
267
274
  max = Math.floor(max);
268
275
  return Math.floor(Math.random() * (max - min) + min);
269
276
  }
270
- function randId() {
277
+ function uid() {
271
278
  return uuidv4();
272
279
  }
273
280
 
@@ -349,11 +356,11 @@ export {
349
356
  isString,
350
357
  isUndefined,
351
358
  last,
352
- randId,
353
359
  randInt,
354
360
  shuffleArray,
355
361
  throwError,
356
362
  throwIf,
357
363
  throwIfEmpty,
364
+ uid,
358
365
  unique
359
366
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axi-engine/utils",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
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": {