@axi-engine/utils 0.1.7 → 0.1.9
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 +1 -4
- package/dist/arrays.d.ts +72 -0
- package/dist/arrays.d.ts.map +1 -0
- package/dist/arrays.js +115 -0
- package/dist/arrays.js.map +1 -0
- package/dist/assertion.d.ts +31 -0
- package/dist/assertion.d.ts.map +1 -0
- package/dist/assertion.js +41 -0
- package/dist/assertion.js.map +1 -0
- package/dist/config.d.ts +10 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +12 -0
- package/dist/config.js.map +1 -0
- package/dist/constructor-registry.d.ts +40 -0
- package/dist/constructor-registry.d.ts.map +1 -0
- package/dist/constructor-registry.js +52 -0
- package/dist/constructor-registry.js.map +1 -0
- package/dist/emitter.d.ts +32 -0
- package/dist/emitter.d.ts.map +1 -0
- package/dist/emitter.js +43 -0
- package/dist/emitter.js.map +1 -0
- package/dist/guards.d.ts +12 -0
- package/dist/guards.d.ts.map +1 -0
- package/dist/guards.js +24 -0
- package/dist/guards.js.map +1 -0
- package/dist/index.d.ts +12 -293
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -285
- package/dist/index.js.map +1 -0
- package/dist/math.d.ts +17 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.js +26 -0
- package/dist/math.js.map +1 -0
- package/dist/misc.d.ts +7 -0
- package/dist/misc.d.ts.map +1 -0
- package/dist/misc.js +9 -0
- package/dist/misc.js.map +1 -0
- package/dist/path.d.ts +10 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +14 -0
- package/dist/path.js.map +1 -0
- package/dist/random.d.ts +14 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +21 -0
- package/dist/random.js.map +1 -0
- package/dist/types.d.ts +50 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +7 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,293 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* '
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
|
|
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
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Generates an array of numbers from 0 to length-1.
|
|
53
|
-
* @param length The desired length of the array.
|
|
54
|
-
* @returns An array of sequential numbers.
|
|
55
|
-
* @example genArray(3); // returns [0, 1, 2]
|
|
56
|
-
*/
|
|
57
|
-
declare function genArray(length: number): number[];
|
|
58
|
-
/**
|
|
59
|
-
* Creates a new array with its elements shuffled in a random order.
|
|
60
|
-
* This function does not mutate the original array.
|
|
61
|
-
* @template T The type of elements in the array.
|
|
62
|
-
* @param array The array to shuffle.
|
|
63
|
-
* @returns A new, shuffled array.
|
|
64
|
-
*/
|
|
65
|
-
declare function shuffleArray<T>(array: T[]): T[];
|
|
66
|
-
/**
|
|
67
|
-
* Checks if the first array is a sequential starting subset of the second array.
|
|
68
|
-
* @param arr1 The potential subset array.
|
|
69
|
-
* @param arr2 The array to check against.
|
|
70
|
-
* @returns `true` if arr1 is a sequential start of arr2, otherwise `false`.
|
|
71
|
-
* @example
|
|
72
|
-
* isSequentialStart([1, 2], [1, 2, 3]); // true
|
|
73
|
-
* isSequentialStart([1, 3], [1, 2, 3]); // false
|
|
74
|
-
*/
|
|
75
|
-
declare function isSequentialStart<T>(arr1: T[], arr2: T[]): boolean;
|
|
76
|
-
/**
|
|
77
|
-
* Checks if two arrays contain the same elements, ignoring order.
|
|
78
|
-
* Works for arrays of primitives like strings or numbers.
|
|
79
|
-
* @template T The type of elements in the array.
|
|
80
|
-
* @param arr1 The first array.
|
|
81
|
-
* @param arr2 The second array.
|
|
82
|
-
* @returns `true` if both arrays contain the same elements, otherwise `false`.
|
|
83
|
-
* @example
|
|
84
|
-
* haveSameElements(['a', 'b'], ['b', 'a']); // true
|
|
85
|
-
* haveSameElements([1, 2, 3], [3, 1, 2]); // true
|
|
86
|
-
* haveSameElements(['a', 'b'], ['a', 'c']); // false
|
|
87
|
-
*/
|
|
88
|
-
declare function haveSameElements<T>(arr1?: T[], arr2?: T[]): boolean;
|
|
89
|
-
/**
|
|
90
|
-
* Checks if two arrays are strictly equal (same elements in the same order).
|
|
91
|
-
* @template T The type of elements in the array.
|
|
92
|
-
* @param arr1 The first array.
|
|
93
|
-
* @param arr2 The second array.
|
|
94
|
-
* @returns `true` if the arrays are strictly equal, otherwise `false`.
|
|
95
|
-
* @example
|
|
96
|
-
* areArraysEqual(['a', 'b'], ['a', 'b']); // true
|
|
97
|
-
* areArraysEqual(['a', 'b'], ['b', 'a']); // false
|
|
98
|
-
* areArraysEqual([1, 2], [1, 2, 3]); // false
|
|
99
|
-
*/
|
|
100
|
-
declare function areArraysEqual<T>(arr1?: T[], arr2?: T[]): boolean;
|
|
101
|
-
/**
|
|
102
|
-
* Gets the last element of an array.
|
|
103
|
-
* @template T The type of elements in the array.
|
|
104
|
-
* @param array The array to query.
|
|
105
|
-
* @returns The last element of the array, or `undefined` if the array is empty.
|
|
106
|
-
*/
|
|
107
|
-
declare function last<T>(array: T[]): T | undefined;
|
|
108
|
-
/**
|
|
109
|
-
* Creates a duplicate-free version of an array.
|
|
110
|
-
* @template T The type of elements in the array.
|
|
111
|
-
* @param array The array to process.
|
|
112
|
-
* @returns A new array with only unique elements.
|
|
113
|
-
*/
|
|
114
|
-
declare function unique<T>(array: T[]): T[];
|
|
115
|
-
/**
|
|
116
|
-
* Gets a random element from an array.
|
|
117
|
-
* @template T The type of elements in the array.
|
|
118
|
-
* @param array The array to choose from.
|
|
119
|
-
* @returns A random element from the array, or `undefined` if the array is empty.
|
|
120
|
-
*/
|
|
121
|
-
declare function getRandomElement<T>(array: T[]): T | undefined;
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Throws an error if the condition is true.
|
|
125
|
-
* @param conditionForThrow - If true, an error will be thrown.
|
|
126
|
-
* @param exceptionMessage - The message for the error.
|
|
127
|
-
* @throws {Error} if the value is true
|
|
128
|
-
*/
|
|
129
|
-
declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
|
|
130
|
-
/**
|
|
131
|
-
* Throws an error if the value is null, undefined, or an empty array.
|
|
132
|
-
*
|
|
133
|
-
* @template T The type of the value being checked.
|
|
134
|
-
* @param value The value to check.
|
|
135
|
-
* @param exceptionMessage The message for the error.
|
|
136
|
-
* @throws {Error} if the value is null, undefined, or an empty array.
|
|
137
|
-
*
|
|
138
|
-
* @example
|
|
139
|
-
* // Example with a potentially undefined variable
|
|
140
|
-
* const user: { name: string } | undefined = findUser();
|
|
141
|
-
* throwIfEmpty(user, 'User not found');
|
|
142
|
-
* // From here, TypeScript knows `user` is not undefined.
|
|
143
|
-
* console.log(user.name);
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* // Example with an array
|
|
147
|
-
* const items: string[] = getItems();
|
|
148
|
-
* throwIfEmpty(items, 'Items array cannot be empty');
|
|
149
|
-
* // From here, you can safely access items[0] without checking for an empty array again.
|
|
150
|
-
* console.log('First item:', items[0]);
|
|
151
|
-
*/
|
|
152
|
-
declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
|
|
153
|
-
|
|
154
|
-
interface AxiEngineConfig {
|
|
155
|
-
pathSeparator: string;
|
|
156
|
-
}
|
|
157
|
-
declare const axiSettings: AxiEngineConfig;
|
|
158
|
-
/**
|
|
159
|
-
* set up global configuration for axi-engine.
|
|
160
|
-
* @param newConfig - configuration object
|
|
161
|
-
*/
|
|
162
|
-
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* A generic registry for mapping string identifiers to class constructors.
|
|
166
|
-
*
|
|
167
|
-
* This utility is fundamental for building extensible systems like dependency injection containers,
|
|
168
|
-
* factories, and serialization engines where types need to be dynamically resolved.
|
|
169
|
-
*
|
|
170
|
-
* @template T - A base type that all registered constructors must produce an instance of.
|
|
171
|
-
*/
|
|
172
|
-
declare class ConstructorRegistry<T> {
|
|
173
|
-
private readonly items;
|
|
174
|
-
/**
|
|
175
|
-
* Registers a constructor with a unique string identifier.
|
|
176
|
-
*
|
|
177
|
-
* @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
|
|
178
|
-
* @param ctor - The class constructor to register.
|
|
179
|
-
* @returns The registry instance for chainable calls.
|
|
180
|
-
* @throws If a constructor with the same `typeId` is already registered.
|
|
181
|
-
*/
|
|
182
|
-
register(typeId: string, ctor: Constructor<T>): this;
|
|
183
|
-
/**
|
|
184
|
-
* Retrieves a constructor by its identifier.
|
|
185
|
-
*
|
|
186
|
-
* @param typeId - The identifier of the constructor to retrieve.
|
|
187
|
-
* @returns The found class constructor.
|
|
188
|
-
* @throws If no constructor is found for the given `typeId`.
|
|
189
|
-
*/
|
|
190
|
-
get(typeId: string): Constructor<T>;
|
|
191
|
-
/**
|
|
192
|
-
* Checks if a constructor for a given identifier is registered.
|
|
193
|
-
* @param typeId - The identifier to check.
|
|
194
|
-
* @returns `true` if a constructor is registered, otherwise `false`.
|
|
195
|
-
*/
|
|
196
|
-
has(typeId: string): boolean;
|
|
197
|
-
/**
|
|
198
|
-
* Clears all registered constructors from the registry.
|
|
199
|
-
*/
|
|
200
|
-
clear(): void;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* A minimal, type-safe event emitter for a single event.
|
|
205
|
-
* It does not manage state, it only manages subscribers and event dispatching.
|
|
206
|
-
* @template T A tuple representing the types of the event arguments.
|
|
207
|
-
*/
|
|
208
|
-
declare class Emitter<T extends any[]> implements Subscribable<T> {
|
|
209
|
-
private listeners;
|
|
210
|
-
/**
|
|
211
|
-
* Returns the number of listeners.
|
|
212
|
-
*/
|
|
213
|
-
get listenerCount(): number;
|
|
214
|
-
/**
|
|
215
|
-
* Subscribes a listener to this event.
|
|
216
|
-
* @returns A function to unsubscribe the listener.
|
|
217
|
-
*/
|
|
218
|
-
subscribe(listener: (...args: T) => void): () => void;
|
|
219
|
-
/**
|
|
220
|
-
* Manually unsubscribe by listener
|
|
221
|
-
* @returns returns true if an listener has been removed, or false if the listener does not exist.
|
|
222
|
-
*/
|
|
223
|
-
unsubscribe(listener: (...args: T) => void): boolean;
|
|
224
|
-
/**
|
|
225
|
-
* Dispatches the event to all subscribed listeners.
|
|
226
|
-
*/
|
|
227
|
-
emit(...args: T): void;
|
|
228
|
-
/**
|
|
229
|
-
* Clears all listeners.
|
|
230
|
-
*/
|
|
231
|
-
clear(): void;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
declare function isNullOrUndefined(val: unknown): val is null | undefined;
|
|
235
|
-
declare function isUndefined(val: unknown): val is undefined;
|
|
236
|
-
declare function isNumber(val: unknown): val is number;
|
|
237
|
-
declare function isBoolean(val: unknown): val is boolean;
|
|
238
|
-
declare function isString(val: unknown): val is string;
|
|
239
|
-
/**
|
|
240
|
-
* Type guard to check if a value is a string that ends with '%'.
|
|
241
|
-
* @param val The value to check.
|
|
242
|
-
* @returns `true` if the value is a percentage string.
|
|
243
|
-
*/
|
|
244
|
-
declare function isPercentageString(val: unknown): val is string;
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Clamps a number between an optional minimum and maximum value.
|
|
248
|
-
* @param val The number to clamp.
|
|
249
|
-
* @param min The minimum value. If null or undefined, it's ignored.
|
|
250
|
-
* @param max The maximum value. If null or undefined, it's ignored.
|
|
251
|
-
* @returns The clamped number.
|
|
252
|
-
*/
|
|
253
|
-
declare function clampNumber(val: number, min?: number | null, max?: number | null): number;
|
|
254
|
-
/**
|
|
255
|
-
* Calculates a percentage of a given value.
|
|
256
|
-
* @param val The base value.
|
|
257
|
-
* @param percents The percentage to get.
|
|
258
|
-
* @returns The calculated percentage of the value.
|
|
259
|
-
* @example getPercentOf(200, 10); // returns 20
|
|
260
|
-
*/
|
|
261
|
-
declare function getPercentOf(val: number, percents: number): number;
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Returns the first key of an object.
|
|
265
|
-
* @param obj The object from which to get the key.
|
|
266
|
-
* @returns The first key of the object as a string.
|
|
267
|
-
*/
|
|
268
|
-
declare function firstKeyOf(obj: any): string;
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Ensures that the given path is returned as an array of segments.
|
|
272
|
-
*/
|
|
273
|
-
declare function ensurePathArray(path: PathType, separator?: string): string[];
|
|
274
|
-
/**
|
|
275
|
-
* Ensures that the given path is returned as a single string.
|
|
276
|
-
*/
|
|
277
|
-
declare function ensurePathString(path: PathType, separator?: string): string;
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
281
|
-
* @param min The minimum integer (inclusive).
|
|
282
|
-
* @param max The maximum integer (exclusive).
|
|
283
|
-
* @returns A random integer.
|
|
284
|
-
* @example randInt(1, 5); // returns 1, 2, 3, or 4
|
|
285
|
-
*/
|
|
286
|
-
declare function randInt(min: number, max: number): number;
|
|
287
|
-
/**
|
|
288
|
-
* Generates a unique identifier using uuidv4.
|
|
289
|
-
* @returns A unique string ID.
|
|
290
|
-
*/
|
|
291
|
-
declare function randId(): string;
|
|
292
|
-
|
|
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 };
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,SAAS,CAAC;AAG7B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,wBAAwB,CAAC;AACvC,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,285 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
});
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,EAAE;AACF,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,wBAAwB,CAAC;AACvC,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clamps a number between an optional minimum and maximum value.
|
|
3
|
+
* @param val The number to clamp.
|
|
4
|
+
* @param min The minimum value. If null or undefined, it's ignored.
|
|
5
|
+
* @param max The maximum value. If null or undefined, it's ignored.
|
|
6
|
+
* @returns The clamped number.
|
|
7
|
+
*/
|
|
8
|
+
export declare function clampNumber(val: number, min?: number | null, max?: number | null): number;
|
|
9
|
+
/**
|
|
10
|
+
* Calculates a percentage of a given value.
|
|
11
|
+
* @param val The base value.
|
|
12
|
+
* @param percents The percentage to get.
|
|
13
|
+
* @returns The calculated percentage of the value.
|
|
14
|
+
* @example getPercentOf(200, 10); // returns 20
|
|
15
|
+
*/
|
|
16
|
+
export declare function getPercentOf(val: number, percents: number): number;
|
|
17
|
+
//# sourceMappingURL=math.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAIzF;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,UAEzD"}
|
package/dist/math.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isNullOrUndefined } from './guards';
|
|
2
|
+
/**
|
|
3
|
+
* Clamps a number between an optional minimum and maximum value.
|
|
4
|
+
* @param val The number to clamp.
|
|
5
|
+
* @param min The minimum value. If null or undefined, it's ignored.
|
|
6
|
+
* @param max The maximum value. If null or undefined, it's ignored.
|
|
7
|
+
* @returns The clamped number.
|
|
8
|
+
*/
|
|
9
|
+
export function clampNumber(val, min, max) {
|
|
10
|
+
if (!isNullOrUndefined(min))
|
|
11
|
+
val = Math.max(val, min);
|
|
12
|
+
if (!isNullOrUndefined(max))
|
|
13
|
+
val = Math.min(val, max);
|
|
14
|
+
return val;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Calculates a percentage of a given value.
|
|
18
|
+
* @param val The base value.
|
|
19
|
+
* @param percents The percentage to get.
|
|
20
|
+
* @returns The calculated percentage of the value.
|
|
21
|
+
* @example getPercentOf(200, 10); // returns 20
|
|
22
|
+
*/
|
|
23
|
+
export function getPercentOf(val, percents) {
|
|
24
|
+
return (percents / 100) * val;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=math.js.map
|
package/dist/math.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"math.js","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,UAAU,CAAC;AAG3C;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW,EAAE,GAAmB,EAAE,GAAmB;IAC/E,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC;QAAE,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtD,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC;QAAE,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IACtD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW,EAAE,QAAgB;IACxD,OAAO,CAAC,QAAQ,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;AAChC,CAAC"}
|
package/dist/misc.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"misc.d.ts","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,GAAG,UAElC"}
|
package/dist/misc.js
ADDED
package/dist/misc.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,GAAQ;IACjC,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7B,CAAC"}
|