@axi-engine/utils 0.1.0
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 +158 -0
- package/dist/index.d.ts +158 -0
- package/dist/index.js +191 -0
- package/dist/index.mjs +141 -0
- package/package.json +25 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a path that can be provided as a single string
|
|
3
|
+
* or an array of segments.
|
|
4
|
+
* @example
|
|
5
|
+
* 'player/stats/health'
|
|
6
|
+
* ['player', 'stats', 'health']
|
|
7
|
+
*/
|
|
8
|
+
type PathType = string | string[];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generates an array of numbers from 0 to length-1.
|
|
12
|
+
* @param length The desired length of the array.
|
|
13
|
+
* @returns An array of sequential numbers.
|
|
14
|
+
* @example genArray(3); // returns [0, 1, 2]
|
|
15
|
+
*/
|
|
16
|
+
declare function genArray(length: number): number[];
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new array with its elements shuffled in a random order.
|
|
19
|
+
* This function does not mutate the original array.
|
|
20
|
+
* @template T The type of elements in the array.
|
|
21
|
+
* @param array The array to shuffle.
|
|
22
|
+
* @returns A new, shuffled array.
|
|
23
|
+
*/
|
|
24
|
+
declare function shuffleArray<T>(array: T[]): T[];
|
|
25
|
+
/**
|
|
26
|
+
* Checks if the first array is a sequential starting subset of the second array.
|
|
27
|
+
* @param arr1 The potential subset array.
|
|
28
|
+
* @param arr2 The array to check against.
|
|
29
|
+
* @returns `true` if arr1 is a sequential start of arr2, otherwise `false`.
|
|
30
|
+
* @example
|
|
31
|
+
* isSequentialStart([1, 2], [1, 2, 3]); // true
|
|
32
|
+
* isSequentialStart([1, 3], [1, 2, 3]); // false
|
|
33
|
+
*/
|
|
34
|
+
declare function isSequentialStart<T>(arr1: T[], arr2: T[]): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Checks if two arrays contain the same elements, ignoring order.
|
|
37
|
+
* Works for arrays of primitives like strings or numbers.
|
|
38
|
+
* @template T The type of elements in the array.
|
|
39
|
+
* @param arr1 The first array.
|
|
40
|
+
* @param arr2 The second array.
|
|
41
|
+
* @returns `true` if both arrays contain the same elements, otherwise `false`.
|
|
42
|
+
* @example
|
|
43
|
+
* haveSameElements(['a', 'b'], ['b', 'a']); // true
|
|
44
|
+
* haveSameElements([1, 2, 3], [3, 1, 2]); // true
|
|
45
|
+
* haveSameElements(['a', 'b'], ['a', 'c']); // false
|
|
46
|
+
*/
|
|
47
|
+
declare function haveSameElements<T>(arr1?: T[], arr2?: T[]): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Checks if two arrays are strictly equal (same elements in the same order).
|
|
50
|
+
* @template T The type of elements in the array.
|
|
51
|
+
* @param arr1 The first array.
|
|
52
|
+
* @param arr2 The second array.
|
|
53
|
+
* @returns `true` if the arrays are strictly equal, otherwise `false`.
|
|
54
|
+
* @example
|
|
55
|
+
* areArraysEqual(['a', 'b'], ['a', 'b']); // true
|
|
56
|
+
* areArraysEqual(['a', 'b'], ['b', 'a']); // false
|
|
57
|
+
* areArraysEqual([1, 2], [1, 2, 3]); // false
|
|
58
|
+
*/
|
|
59
|
+
declare function areArraysEqual<T>(arr1?: T[], arr2?: T[]): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the last element of an array.
|
|
62
|
+
* @template T The type of elements in the array.
|
|
63
|
+
* @param array The array to query.
|
|
64
|
+
* @returns The last element of the array, or `undefined` if the array is empty.
|
|
65
|
+
*/
|
|
66
|
+
declare function last<T>(array: T[]): T | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a duplicate-free version of an array.
|
|
69
|
+
* @template T The type of elements in the array.
|
|
70
|
+
* @param array The array to process.
|
|
71
|
+
* @returns A new array with only unique elements.
|
|
72
|
+
*/
|
|
73
|
+
declare function unique<T>(array: T[]): T[];
|
|
74
|
+
/**
|
|
75
|
+
* Gets a random element from an array.
|
|
76
|
+
* @template T The type of elements in the array.
|
|
77
|
+
* @param array The array to choose from.
|
|
78
|
+
* @returns A random element from the array, or `undefined` if the array is empty.
|
|
79
|
+
*/
|
|
80
|
+
declare function getRandomElement<T>(array: T[]): T | undefined;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Throws an error if the condition is true.
|
|
84
|
+
* @param conditionForThrow - If true, an error will be thrown.
|
|
85
|
+
* @param exceptionMessage - The message for the error.
|
|
86
|
+
*/
|
|
87
|
+
declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
|
|
88
|
+
|
|
89
|
+
interface AxiEngineConfig {
|
|
90
|
+
pathSeparator: string;
|
|
91
|
+
}
|
|
92
|
+
declare const axiSettings: AxiEngineConfig;
|
|
93
|
+
/**
|
|
94
|
+
* set up global configuration for axi-engine.
|
|
95
|
+
* @param newConfig - configuration object
|
|
96
|
+
*/
|
|
97
|
+
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
98
|
+
|
|
99
|
+
declare function isNullOrUndefined(val: unknown): val is null | undefined;
|
|
100
|
+
declare function isUndefined(val: unknown): val is undefined;
|
|
101
|
+
declare function isNumber(val: unknown): val is number;
|
|
102
|
+
declare function isBoolean(val: unknown): val is boolean;
|
|
103
|
+
declare function isString(val: unknown): val is string;
|
|
104
|
+
/**
|
|
105
|
+
* Type guard to check if a value is a string that ends with '%'.
|
|
106
|
+
* @param val The value to check.
|
|
107
|
+
* @returns `true` if the value is a percentage string.
|
|
108
|
+
*/
|
|
109
|
+
declare function isPercentageString(val: unknown): val is string;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Clamps a number between an optional minimum and maximum value.
|
|
113
|
+
* @param val The number to clamp.
|
|
114
|
+
* @param min The minimum value. If null or undefined, it's ignored.
|
|
115
|
+
* @param max The maximum value. If null or undefined, it's ignored.
|
|
116
|
+
* @returns The clamped number.
|
|
117
|
+
*/
|
|
118
|
+
declare function clampNumber(val: number, min?: number | null, max?: number | null): number;
|
|
119
|
+
/**
|
|
120
|
+
* Calculates a percentage of a given value.
|
|
121
|
+
* @param val The base value.
|
|
122
|
+
* @param percents The percentage to get.
|
|
123
|
+
* @returns The calculated percentage of the value.
|
|
124
|
+
* @example getPercentOf(200, 10); // returns 20
|
|
125
|
+
*/
|
|
126
|
+
declare function getPercentOf(val: number, percents: number): number;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Returns the first key of an object.
|
|
130
|
+
* @param obj The object from which to get the key.
|
|
131
|
+
* @returns The first key of the object as a string.
|
|
132
|
+
*/
|
|
133
|
+
declare function firstKeyOf(obj: any): string;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Ensures that the given path is returned as an array of segments.
|
|
137
|
+
*/
|
|
138
|
+
declare function ensurePathArray(path: PathType, separator?: string): string[];
|
|
139
|
+
/**
|
|
140
|
+
* Ensures that the given path is returned as a single string.
|
|
141
|
+
*/
|
|
142
|
+
declare function ensurePathString(path: PathType, separator?: string): string;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
146
|
+
* @param min The minimum integer (inclusive).
|
|
147
|
+
* @param max The maximum integer (exclusive).
|
|
148
|
+
* @returns A random integer.
|
|
149
|
+
* @example randInt(1, 5); // returns 1, 2, 3, or 4
|
|
150
|
+
*/
|
|
151
|
+
declare function randInt(min: number, max: number): number;
|
|
152
|
+
/**
|
|
153
|
+
* Generates a unique identifier using uuidv4.
|
|
154
|
+
* @returns A unique string ID.
|
|
155
|
+
*/
|
|
156
|
+
declare function randId(): string;
|
|
157
|
+
|
|
158
|
+
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, unique };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a path that can be provided as a single string
|
|
3
|
+
* or an array of segments.
|
|
4
|
+
* @example
|
|
5
|
+
* 'player/stats/health'
|
|
6
|
+
* ['player', 'stats', 'health']
|
|
7
|
+
*/
|
|
8
|
+
type PathType = string | string[];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generates an array of numbers from 0 to length-1.
|
|
12
|
+
* @param length The desired length of the array.
|
|
13
|
+
* @returns An array of sequential numbers.
|
|
14
|
+
* @example genArray(3); // returns [0, 1, 2]
|
|
15
|
+
*/
|
|
16
|
+
declare function genArray(length: number): number[];
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new array with its elements shuffled in a random order.
|
|
19
|
+
* This function does not mutate the original array.
|
|
20
|
+
* @template T The type of elements in the array.
|
|
21
|
+
* @param array The array to shuffle.
|
|
22
|
+
* @returns A new, shuffled array.
|
|
23
|
+
*/
|
|
24
|
+
declare function shuffleArray<T>(array: T[]): T[];
|
|
25
|
+
/**
|
|
26
|
+
* Checks if the first array is a sequential starting subset of the second array.
|
|
27
|
+
* @param arr1 The potential subset array.
|
|
28
|
+
* @param arr2 The array to check against.
|
|
29
|
+
* @returns `true` if arr1 is a sequential start of arr2, otherwise `false`.
|
|
30
|
+
* @example
|
|
31
|
+
* isSequentialStart([1, 2], [1, 2, 3]); // true
|
|
32
|
+
* isSequentialStart([1, 3], [1, 2, 3]); // false
|
|
33
|
+
*/
|
|
34
|
+
declare function isSequentialStart<T>(arr1: T[], arr2: T[]): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Checks if two arrays contain the same elements, ignoring order.
|
|
37
|
+
* Works for arrays of primitives like strings or numbers.
|
|
38
|
+
* @template T The type of elements in the array.
|
|
39
|
+
* @param arr1 The first array.
|
|
40
|
+
* @param arr2 The second array.
|
|
41
|
+
* @returns `true` if both arrays contain the same elements, otherwise `false`.
|
|
42
|
+
* @example
|
|
43
|
+
* haveSameElements(['a', 'b'], ['b', 'a']); // true
|
|
44
|
+
* haveSameElements([1, 2, 3], [3, 1, 2]); // true
|
|
45
|
+
* haveSameElements(['a', 'b'], ['a', 'c']); // false
|
|
46
|
+
*/
|
|
47
|
+
declare function haveSameElements<T>(arr1?: T[], arr2?: T[]): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Checks if two arrays are strictly equal (same elements in the same order).
|
|
50
|
+
* @template T The type of elements in the array.
|
|
51
|
+
* @param arr1 The first array.
|
|
52
|
+
* @param arr2 The second array.
|
|
53
|
+
* @returns `true` if the arrays are strictly equal, otherwise `false`.
|
|
54
|
+
* @example
|
|
55
|
+
* areArraysEqual(['a', 'b'], ['a', 'b']); // true
|
|
56
|
+
* areArraysEqual(['a', 'b'], ['b', 'a']); // false
|
|
57
|
+
* areArraysEqual([1, 2], [1, 2, 3]); // false
|
|
58
|
+
*/
|
|
59
|
+
declare function areArraysEqual<T>(arr1?: T[], arr2?: T[]): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the last element of an array.
|
|
62
|
+
* @template T The type of elements in the array.
|
|
63
|
+
* @param array The array to query.
|
|
64
|
+
* @returns The last element of the array, or `undefined` if the array is empty.
|
|
65
|
+
*/
|
|
66
|
+
declare function last<T>(array: T[]): T | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a duplicate-free version of an array.
|
|
69
|
+
* @template T The type of elements in the array.
|
|
70
|
+
* @param array The array to process.
|
|
71
|
+
* @returns A new array with only unique elements.
|
|
72
|
+
*/
|
|
73
|
+
declare function unique<T>(array: T[]): T[];
|
|
74
|
+
/**
|
|
75
|
+
* Gets a random element from an array.
|
|
76
|
+
* @template T The type of elements in the array.
|
|
77
|
+
* @param array The array to choose from.
|
|
78
|
+
* @returns A random element from the array, or `undefined` if the array is empty.
|
|
79
|
+
*/
|
|
80
|
+
declare function getRandomElement<T>(array: T[]): T | undefined;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Throws an error if the condition is true.
|
|
84
|
+
* @param conditionForThrow - If true, an error will be thrown.
|
|
85
|
+
* @param exceptionMessage - The message for the error.
|
|
86
|
+
*/
|
|
87
|
+
declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
|
|
88
|
+
|
|
89
|
+
interface AxiEngineConfig {
|
|
90
|
+
pathSeparator: string;
|
|
91
|
+
}
|
|
92
|
+
declare const axiSettings: AxiEngineConfig;
|
|
93
|
+
/**
|
|
94
|
+
* set up global configuration for axi-engine.
|
|
95
|
+
* @param newConfig - configuration object
|
|
96
|
+
*/
|
|
97
|
+
declare function configure(newConfig: Partial<AxiEngineConfig>): void;
|
|
98
|
+
|
|
99
|
+
declare function isNullOrUndefined(val: unknown): val is null | undefined;
|
|
100
|
+
declare function isUndefined(val: unknown): val is undefined;
|
|
101
|
+
declare function isNumber(val: unknown): val is number;
|
|
102
|
+
declare function isBoolean(val: unknown): val is boolean;
|
|
103
|
+
declare function isString(val: unknown): val is string;
|
|
104
|
+
/**
|
|
105
|
+
* Type guard to check if a value is a string that ends with '%'.
|
|
106
|
+
* @param val The value to check.
|
|
107
|
+
* @returns `true` if the value is a percentage string.
|
|
108
|
+
*/
|
|
109
|
+
declare function isPercentageString(val: unknown): val is string;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Clamps a number between an optional minimum and maximum value.
|
|
113
|
+
* @param val The number to clamp.
|
|
114
|
+
* @param min The minimum value. If null or undefined, it's ignored.
|
|
115
|
+
* @param max The maximum value. If null or undefined, it's ignored.
|
|
116
|
+
* @returns The clamped number.
|
|
117
|
+
*/
|
|
118
|
+
declare function clampNumber(val: number, min?: number | null, max?: number | null): number;
|
|
119
|
+
/**
|
|
120
|
+
* Calculates a percentage of a given value.
|
|
121
|
+
* @param val The base value.
|
|
122
|
+
* @param percents The percentage to get.
|
|
123
|
+
* @returns The calculated percentage of the value.
|
|
124
|
+
* @example getPercentOf(200, 10); // returns 20
|
|
125
|
+
*/
|
|
126
|
+
declare function getPercentOf(val: number, percents: number): number;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Returns the first key of an object.
|
|
130
|
+
* @param obj The object from which to get the key.
|
|
131
|
+
* @returns The first key of the object as a string.
|
|
132
|
+
*/
|
|
133
|
+
declare function firstKeyOf(obj: any): string;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Ensures that the given path is returned as an array of segments.
|
|
137
|
+
*/
|
|
138
|
+
declare function ensurePathArray(path: PathType, separator?: string): string[];
|
|
139
|
+
/**
|
|
140
|
+
* Ensures that the given path is returned as a single string.
|
|
141
|
+
*/
|
|
142
|
+
declare function ensurePathString(path: PathType, separator?: string): string;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns a random integer between min (inclusive) and max (exclusive).
|
|
146
|
+
* @param min The minimum integer (inclusive).
|
|
147
|
+
* @param max The maximum integer (exclusive).
|
|
148
|
+
* @returns A random integer.
|
|
149
|
+
* @example randInt(1, 5); // returns 1, 2, 3, or 4
|
|
150
|
+
*/
|
|
151
|
+
declare function randInt(min: number, max: number): number;
|
|
152
|
+
/**
|
|
153
|
+
* Generates a unique identifier using uuidv4.
|
|
154
|
+
* @returns A unique string ID.
|
|
155
|
+
*/
|
|
156
|
+
declare function randId(): string;
|
|
157
|
+
|
|
158
|
+
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, unique };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
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
|
+
areArraysEqual: () => areArraysEqual,
|
|
24
|
+
axiSettings: () => axiSettings,
|
|
25
|
+
clampNumber: () => clampNumber,
|
|
26
|
+
configure: () => configure,
|
|
27
|
+
ensurePathArray: () => ensurePathArray,
|
|
28
|
+
ensurePathString: () => ensurePathString,
|
|
29
|
+
firstKeyOf: () => firstKeyOf,
|
|
30
|
+
genArray: () => genArray,
|
|
31
|
+
getPercentOf: () => getPercentOf,
|
|
32
|
+
getRandomElement: () => getRandomElement,
|
|
33
|
+
haveSameElements: () => haveSameElements,
|
|
34
|
+
isBoolean: () => isBoolean,
|
|
35
|
+
isNullOrUndefined: () => isNullOrUndefined,
|
|
36
|
+
isNumber: () => isNumber,
|
|
37
|
+
isPercentageString: () => isPercentageString,
|
|
38
|
+
isSequentialStart: () => isSequentialStart,
|
|
39
|
+
isString: () => isString,
|
|
40
|
+
isUndefined: () => isUndefined,
|
|
41
|
+
last: () => last,
|
|
42
|
+
randId: () => randId,
|
|
43
|
+
randInt: () => randInt,
|
|
44
|
+
shuffleArray: () => shuffleArray,
|
|
45
|
+
throwIf: () => throwIf,
|
|
46
|
+
unique: () => unique
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(index_exports);
|
|
49
|
+
|
|
50
|
+
// src/arrays.ts
|
|
51
|
+
function genArray(length) {
|
|
52
|
+
return Array.from({ length }, (_v, i) => i);
|
|
53
|
+
}
|
|
54
|
+
function shuffleArray(array) {
|
|
55
|
+
const result = [...array];
|
|
56
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
57
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
58
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
function isSequentialStart(arr1, arr2) {
|
|
63
|
+
if (arr1.length > arr2.length) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return arr1.every((element, index) => element === arr2[index]);
|
|
67
|
+
}
|
|
68
|
+
function haveSameElements(arr1, arr2) {
|
|
69
|
+
if (!arr1 && !arr2) return true;
|
|
70
|
+
if (!arr1 || !arr2) return false;
|
|
71
|
+
if (arr1.length !== arr2.length) return false;
|
|
72
|
+
const sortedArr1 = [...arr1].sort();
|
|
73
|
+
const sortedArr2 = [...arr2].sort();
|
|
74
|
+
return sortedArr1.every((value, index) => value === sortedArr2[index]);
|
|
75
|
+
}
|
|
76
|
+
function areArraysEqual(arr1, arr2) {
|
|
77
|
+
if (!arr1 && !arr2) return true;
|
|
78
|
+
if (!arr1 || !arr2) return false;
|
|
79
|
+
if (arr1.length !== arr2.length) return false;
|
|
80
|
+
return arr1.every((value, index) => value === arr2[index]);
|
|
81
|
+
}
|
|
82
|
+
function last(array) {
|
|
83
|
+
return array[array.length - 1];
|
|
84
|
+
}
|
|
85
|
+
function unique(array) {
|
|
86
|
+
return [...new Set(array)];
|
|
87
|
+
}
|
|
88
|
+
function getRandomElement(array) {
|
|
89
|
+
if (array.length === 0) {
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
const index = Math.floor(Math.random() * array.length);
|
|
93
|
+
return array[index];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/assertion.ts
|
|
97
|
+
function throwIf(conditionForThrow, exceptionMessage) {
|
|
98
|
+
if (conditionForThrow) {
|
|
99
|
+
throw new Error(exceptionMessage);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/config.ts
|
|
104
|
+
var defaultConfig = {
|
|
105
|
+
pathSeparator: "/"
|
|
106
|
+
};
|
|
107
|
+
var axiSettings = { ...defaultConfig };
|
|
108
|
+
function configure(newConfig) {
|
|
109
|
+
Object.assign(axiSettings, newConfig);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/guards.ts
|
|
113
|
+
function isNullOrUndefined(val) {
|
|
114
|
+
return val === void 0 || val === null;
|
|
115
|
+
}
|
|
116
|
+
function isUndefined(val) {
|
|
117
|
+
return typeof val === "undefined";
|
|
118
|
+
}
|
|
119
|
+
function isNumber(val) {
|
|
120
|
+
return typeof val === "number";
|
|
121
|
+
}
|
|
122
|
+
function isBoolean(val) {
|
|
123
|
+
return typeof val === "boolean";
|
|
124
|
+
}
|
|
125
|
+
function isString(val) {
|
|
126
|
+
return typeof val === "string";
|
|
127
|
+
}
|
|
128
|
+
function isPercentageString(val) {
|
|
129
|
+
return typeof val === "string" && val.endsWith("%");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/math.ts
|
|
133
|
+
function clampNumber(val, min, max) {
|
|
134
|
+
if (!isNullOrUndefined(min)) val = Math.max(val, min);
|
|
135
|
+
if (!isNullOrUndefined(max)) val = Math.min(val, max);
|
|
136
|
+
return val;
|
|
137
|
+
}
|
|
138
|
+
function getPercentOf(val, percents) {
|
|
139
|
+
return percents / 100 * val;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/misc.ts
|
|
143
|
+
function firstKeyOf(obj) {
|
|
144
|
+
return Object.keys(obj)[0];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/path.ts
|
|
148
|
+
function ensurePathArray(path, separator = axiSettings.pathSeparator) {
|
|
149
|
+
return Array.isArray(path) ? [...path] : path.split(separator);
|
|
150
|
+
}
|
|
151
|
+
function ensurePathString(path, separator = axiSettings.pathSeparator) {
|
|
152
|
+
return !Array.isArray(path) ? path : path.join(separator);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/random.ts
|
|
156
|
+
var import_uuid = require("uuid");
|
|
157
|
+
function randInt(min, max) {
|
|
158
|
+
min = Math.ceil(min);
|
|
159
|
+
max = Math.floor(max);
|
|
160
|
+
return Math.floor(Math.random() * (max - min) + min);
|
|
161
|
+
}
|
|
162
|
+
function randId() {
|
|
163
|
+
return (0, import_uuid.v4)();
|
|
164
|
+
}
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
areArraysEqual,
|
|
168
|
+
axiSettings,
|
|
169
|
+
clampNumber,
|
|
170
|
+
configure,
|
|
171
|
+
ensurePathArray,
|
|
172
|
+
ensurePathString,
|
|
173
|
+
firstKeyOf,
|
|
174
|
+
genArray,
|
|
175
|
+
getPercentOf,
|
|
176
|
+
getRandomElement,
|
|
177
|
+
haveSameElements,
|
|
178
|
+
isBoolean,
|
|
179
|
+
isNullOrUndefined,
|
|
180
|
+
isNumber,
|
|
181
|
+
isPercentageString,
|
|
182
|
+
isSequentialStart,
|
|
183
|
+
isString,
|
|
184
|
+
isUndefined,
|
|
185
|
+
last,
|
|
186
|
+
randId,
|
|
187
|
+
randInt,
|
|
188
|
+
shuffleArray,
|
|
189
|
+
throwIf,
|
|
190
|
+
unique
|
|
191
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// src/arrays.ts
|
|
2
|
+
function genArray(length) {
|
|
3
|
+
return Array.from({ length }, (_v, i) => i);
|
|
4
|
+
}
|
|
5
|
+
function shuffleArray(array) {
|
|
6
|
+
const result = [...array];
|
|
7
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
8
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
9
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
10
|
+
}
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
function isSequentialStart(arr1, arr2) {
|
|
14
|
+
if (arr1.length > arr2.length) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
return arr1.every((element, index) => element === arr2[index]);
|
|
18
|
+
}
|
|
19
|
+
function haveSameElements(arr1, arr2) {
|
|
20
|
+
if (!arr1 && !arr2) return true;
|
|
21
|
+
if (!arr1 || !arr2) return false;
|
|
22
|
+
if (arr1.length !== arr2.length) return false;
|
|
23
|
+
const sortedArr1 = [...arr1].sort();
|
|
24
|
+
const sortedArr2 = [...arr2].sort();
|
|
25
|
+
return sortedArr1.every((value, index) => value === sortedArr2[index]);
|
|
26
|
+
}
|
|
27
|
+
function areArraysEqual(arr1, arr2) {
|
|
28
|
+
if (!arr1 && !arr2) return true;
|
|
29
|
+
if (!arr1 || !arr2) return false;
|
|
30
|
+
if (arr1.length !== arr2.length) return false;
|
|
31
|
+
return arr1.every((value, index) => value === arr2[index]);
|
|
32
|
+
}
|
|
33
|
+
function last(array) {
|
|
34
|
+
return array[array.length - 1];
|
|
35
|
+
}
|
|
36
|
+
function unique(array) {
|
|
37
|
+
return [...new Set(array)];
|
|
38
|
+
}
|
|
39
|
+
function getRandomElement(array) {
|
|
40
|
+
if (array.length === 0) {
|
|
41
|
+
return void 0;
|
|
42
|
+
}
|
|
43
|
+
const index = Math.floor(Math.random() * array.length);
|
|
44
|
+
return array[index];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/assertion.ts
|
|
48
|
+
function throwIf(conditionForThrow, exceptionMessage) {
|
|
49
|
+
if (conditionForThrow) {
|
|
50
|
+
throw new Error(exceptionMessage);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/config.ts
|
|
55
|
+
var defaultConfig = {
|
|
56
|
+
pathSeparator: "/"
|
|
57
|
+
};
|
|
58
|
+
var axiSettings = { ...defaultConfig };
|
|
59
|
+
function configure(newConfig) {
|
|
60
|
+
Object.assign(axiSettings, newConfig);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/guards.ts
|
|
64
|
+
function isNullOrUndefined(val) {
|
|
65
|
+
return val === void 0 || val === null;
|
|
66
|
+
}
|
|
67
|
+
function isUndefined(val) {
|
|
68
|
+
return typeof val === "undefined";
|
|
69
|
+
}
|
|
70
|
+
function isNumber(val) {
|
|
71
|
+
return typeof val === "number";
|
|
72
|
+
}
|
|
73
|
+
function isBoolean(val) {
|
|
74
|
+
return typeof val === "boolean";
|
|
75
|
+
}
|
|
76
|
+
function isString(val) {
|
|
77
|
+
return typeof val === "string";
|
|
78
|
+
}
|
|
79
|
+
function isPercentageString(val) {
|
|
80
|
+
return typeof val === "string" && val.endsWith("%");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/math.ts
|
|
84
|
+
function clampNumber(val, min, max) {
|
|
85
|
+
if (!isNullOrUndefined(min)) val = Math.max(val, min);
|
|
86
|
+
if (!isNullOrUndefined(max)) val = Math.min(val, max);
|
|
87
|
+
return val;
|
|
88
|
+
}
|
|
89
|
+
function getPercentOf(val, percents) {
|
|
90
|
+
return percents / 100 * val;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/misc.ts
|
|
94
|
+
function firstKeyOf(obj) {
|
|
95
|
+
return Object.keys(obj)[0];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/path.ts
|
|
99
|
+
function ensurePathArray(path, separator = axiSettings.pathSeparator) {
|
|
100
|
+
return Array.isArray(path) ? [...path] : path.split(separator);
|
|
101
|
+
}
|
|
102
|
+
function ensurePathString(path, separator = axiSettings.pathSeparator) {
|
|
103
|
+
return !Array.isArray(path) ? path : path.join(separator);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// src/random.ts
|
|
107
|
+
import { v4 as uuidv4 } from "uuid";
|
|
108
|
+
function randInt(min, max) {
|
|
109
|
+
min = Math.ceil(min);
|
|
110
|
+
max = Math.floor(max);
|
|
111
|
+
return Math.floor(Math.random() * (max - min) + min);
|
|
112
|
+
}
|
|
113
|
+
function randId() {
|
|
114
|
+
return uuidv4();
|
|
115
|
+
}
|
|
116
|
+
export {
|
|
117
|
+
areArraysEqual,
|
|
118
|
+
axiSettings,
|
|
119
|
+
clampNumber,
|
|
120
|
+
configure,
|
|
121
|
+
ensurePathArray,
|
|
122
|
+
ensurePathString,
|
|
123
|
+
firstKeyOf,
|
|
124
|
+
genArray,
|
|
125
|
+
getPercentOf,
|
|
126
|
+
getRandomElement,
|
|
127
|
+
haveSameElements,
|
|
128
|
+
isBoolean,
|
|
129
|
+
isNullOrUndefined,
|
|
130
|
+
isNumber,
|
|
131
|
+
isPercentageString,
|
|
132
|
+
isSequentialStart,
|
|
133
|
+
isString,
|
|
134
|
+
isUndefined,
|
|
135
|
+
last,
|
|
136
|
+
randId,
|
|
137
|
+
randInt,
|
|
138
|
+
shuffleArray,
|
|
139
|
+
throwIf,
|
|
140
|
+
unique
|
|
141
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@axi-engine/utils",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Core utility library for Axi Engine, providing common functions for arrays, math, type guards, and more.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"axi-engine",
|
|
8
|
+
"typescript",
|
|
9
|
+
"gamedev",
|
|
10
|
+
"utils"
|
|
11
|
+
],
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"module": "./dist/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
17
|
+
"test": "echo 'No tests yet for @axi-engine/utils'"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"uuid": "^13.0.0"
|
|
24
|
+
}
|
|
25
|
+
}
|