@axi-engine/utils 0.1.6 → 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/README.md +1 -4
- package/dist/index.cjs +380 -0
- package/dist/{index.d.ts → index.d.cts} +94 -43
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +94 -43
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +298 -130
- package/dist/index.mjs.map +1 -0
- package/package.json +38 -33
- package/dist/index.js +0 -239
package/dist/index.js
DELETED
|
@@ -1,239 +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
|
-
Emitter: () => Emitter,
|
|
24
|
-
areArraysEqual: () => areArraysEqual,
|
|
25
|
-
axiSettings: () => axiSettings,
|
|
26
|
-
clampNumber: () => clampNumber,
|
|
27
|
-
configure: () => configure,
|
|
28
|
-
ensurePathArray: () => ensurePathArray,
|
|
29
|
-
ensurePathString: () => ensurePathString,
|
|
30
|
-
firstKeyOf: () => firstKeyOf,
|
|
31
|
-
genArray: () => genArray,
|
|
32
|
-
getPercentOf: () => getPercentOf,
|
|
33
|
-
getRandomElement: () => getRandomElement,
|
|
34
|
-
haveSameElements: () => haveSameElements,
|
|
35
|
-
isBoolean: () => isBoolean,
|
|
36
|
-
isNullOrUndefined: () => isNullOrUndefined,
|
|
37
|
-
isNumber: () => isNumber,
|
|
38
|
-
isPercentageString: () => isPercentageString,
|
|
39
|
-
isSequentialStart: () => isSequentialStart,
|
|
40
|
-
isString: () => isString,
|
|
41
|
-
isUndefined: () => isUndefined,
|
|
42
|
-
last: () => last,
|
|
43
|
-
randId: () => randId,
|
|
44
|
-
randInt: () => randInt,
|
|
45
|
-
shuffleArray: () => shuffleArray,
|
|
46
|
-
throwIf: () => throwIf,
|
|
47
|
-
throwIfEmpty: () => throwIfEmpty,
|
|
48
|
-
unique: () => unique
|
|
49
|
-
});
|
|
50
|
-
module.exports = __toCommonJS(index_exports);
|
|
51
|
-
|
|
52
|
-
// src/arrays.ts
|
|
53
|
-
function genArray(length) {
|
|
54
|
-
return Array.from({ length }, (_v, i) => i);
|
|
55
|
-
}
|
|
56
|
-
function shuffleArray(array) {
|
|
57
|
-
const result = [...array];
|
|
58
|
-
for (let i = result.length - 1; i > 0; i--) {
|
|
59
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
60
|
-
[result[i], result[j]] = [result[j], result[i]];
|
|
61
|
-
}
|
|
62
|
-
return result;
|
|
63
|
-
}
|
|
64
|
-
function isSequentialStart(arr1, arr2) {
|
|
65
|
-
if (arr1.length > arr2.length) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
return arr1.every((element, index) => element === arr2[index]);
|
|
69
|
-
}
|
|
70
|
-
function haveSameElements(arr1, arr2) {
|
|
71
|
-
if (!arr1 && !arr2) return true;
|
|
72
|
-
if (!arr1 || !arr2) return false;
|
|
73
|
-
if (arr1.length !== arr2.length) return false;
|
|
74
|
-
const sortedArr1 = [...arr1].sort();
|
|
75
|
-
const sortedArr2 = [...arr2].sort();
|
|
76
|
-
return sortedArr1.every((value, index) => value === sortedArr2[index]);
|
|
77
|
-
}
|
|
78
|
-
function areArraysEqual(arr1, arr2) {
|
|
79
|
-
if (!arr1 && !arr2) return true;
|
|
80
|
-
if (!arr1 || !arr2) return false;
|
|
81
|
-
if (arr1.length !== arr2.length) return false;
|
|
82
|
-
return arr1.every((value, index) => value === arr2[index]);
|
|
83
|
-
}
|
|
84
|
-
function last(array) {
|
|
85
|
-
return array[array.length - 1];
|
|
86
|
-
}
|
|
87
|
-
function unique(array) {
|
|
88
|
-
return [...new Set(array)];
|
|
89
|
-
}
|
|
90
|
-
function getRandomElement(array) {
|
|
91
|
-
if (array.length === 0) {
|
|
92
|
-
return void 0;
|
|
93
|
-
}
|
|
94
|
-
const index = Math.floor(Math.random() * array.length);
|
|
95
|
-
return array[index];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// src/guards.ts
|
|
99
|
-
function isNullOrUndefined(val) {
|
|
100
|
-
return val === void 0 || val === null;
|
|
101
|
-
}
|
|
102
|
-
function isUndefined(val) {
|
|
103
|
-
return typeof val === "undefined";
|
|
104
|
-
}
|
|
105
|
-
function isNumber(val) {
|
|
106
|
-
return typeof val === "number";
|
|
107
|
-
}
|
|
108
|
-
function isBoolean(val) {
|
|
109
|
-
return typeof val === "boolean";
|
|
110
|
-
}
|
|
111
|
-
function isString(val) {
|
|
112
|
-
return typeof val === "string";
|
|
113
|
-
}
|
|
114
|
-
function isPercentageString(val) {
|
|
115
|
-
return typeof val === "string" && val.endsWith("%");
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// src/assertion.ts
|
|
119
|
-
function throwIf(conditionForThrow, exceptionMessage) {
|
|
120
|
-
if (conditionForThrow) {
|
|
121
|
-
throw new Error(exceptionMessage);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
function throwIfEmpty(value, exceptionMessage) {
|
|
125
|
-
const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
|
|
126
|
-
if (isNullOrUndefined(value) || isArrayAndEmpty) {
|
|
127
|
-
throw new Error(exceptionMessage);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// src/config.ts
|
|
132
|
-
var defaultConfig = {
|
|
133
|
-
pathSeparator: "/"
|
|
134
|
-
};
|
|
135
|
-
var axiSettings = { ...defaultConfig };
|
|
136
|
-
function configure(newConfig) {
|
|
137
|
-
Object.assign(axiSettings, newConfig);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// src/emitter.ts
|
|
141
|
-
var Emitter = class {
|
|
142
|
-
listeners = /* @__PURE__ */ new Set();
|
|
143
|
-
/**
|
|
144
|
-
* Returns the number of listeners.
|
|
145
|
-
*/
|
|
146
|
-
get listenerCount() {
|
|
147
|
-
return this.listeners.size;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Subscribes a listener to this event.
|
|
151
|
-
* @returns A function to unsubscribe the listener.
|
|
152
|
-
*/
|
|
153
|
-
subscribe(listener) {
|
|
154
|
-
this.listeners.add(listener);
|
|
155
|
-
return () => this.listeners.delete(listener);
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Manually unsubscribe by listener
|
|
159
|
-
* @returns returns true if an listener has been removed, or false if the listener does not exist.
|
|
160
|
-
*/
|
|
161
|
-
unsubscribe(listener) {
|
|
162
|
-
return this.listeners.delete(listener);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Dispatches the event to all subscribed listeners.
|
|
166
|
-
*/
|
|
167
|
-
emit(...args) {
|
|
168
|
-
this.listeners.forEach((listener) => listener(...args));
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Clears all listeners.
|
|
172
|
-
*/
|
|
173
|
-
clear() {
|
|
174
|
-
this.listeners.clear();
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// src/math.ts
|
|
179
|
-
function clampNumber(val, min, max) {
|
|
180
|
-
if (!isNullOrUndefined(min)) val = Math.max(val, min);
|
|
181
|
-
if (!isNullOrUndefined(max)) val = Math.min(val, max);
|
|
182
|
-
return val;
|
|
183
|
-
}
|
|
184
|
-
function getPercentOf(val, percents) {
|
|
185
|
-
return percents / 100 * val;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// src/misc.ts
|
|
189
|
-
function firstKeyOf(obj) {
|
|
190
|
-
return Object.keys(obj)[0];
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// src/path.ts
|
|
194
|
-
function ensurePathArray(path, separator = axiSettings.pathSeparator) {
|
|
195
|
-
return Array.isArray(path) ? [...path] : path.split(separator);
|
|
196
|
-
}
|
|
197
|
-
function ensurePathString(path, separator = axiSettings.pathSeparator) {
|
|
198
|
-
return !Array.isArray(path) ? path : path.join(separator);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// src/random.ts
|
|
202
|
-
var import_uuid = require("uuid");
|
|
203
|
-
function randInt(min, max) {
|
|
204
|
-
min = Math.ceil(min);
|
|
205
|
-
max = Math.floor(max);
|
|
206
|
-
return Math.floor(Math.random() * (max - min) + min);
|
|
207
|
-
}
|
|
208
|
-
function randId() {
|
|
209
|
-
return (0, import_uuid.v4)();
|
|
210
|
-
}
|
|
211
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
212
|
-
0 && (module.exports = {
|
|
213
|
-
Emitter,
|
|
214
|
-
areArraysEqual,
|
|
215
|
-
axiSettings,
|
|
216
|
-
clampNumber,
|
|
217
|
-
configure,
|
|
218
|
-
ensurePathArray,
|
|
219
|
-
ensurePathString,
|
|
220
|
-
firstKeyOf,
|
|
221
|
-
genArray,
|
|
222
|
-
getPercentOf,
|
|
223
|
-
getRandomElement,
|
|
224
|
-
haveSameElements,
|
|
225
|
-
isBoolean,
|
|
226
|
-
isNullOrUndefined,
|
|
227
|
-
isNumber,
|
|
228
|
-
isPercentageString,
|
|
229
|
-
isSequentialStart,
|
|
230
|
-
isString,
|
|
231
|
-
isUndefined,
|
|
232
|
-
last,
|
|
233
|
-
randId,
|
|
234
|
-
randInt,
|
|
235
|
-
shuffleArray,
|
|
236
|
-
throwIf,
|
|
237
|
-
throwIfEmpty,
|
|
238
|
-
unique
|
|
239
|
-
});
|